AForge.Robotics.Lego.Internals.SerialCommunication.SendMessage C# (CSharp) Method

SendMessage() public method

Send message to NXT brick over the communication interface.
public SendMessage ( byte message, int offset, int length ) : bool
message byte Buffer containing the message to send.
offset int Offset of the message in the buffer.
length int Length of the message to send.
return bool
        public bool SendMessage( byte[] message, int offset, int length )
        {
            // check connection status
            if ( !port.IsOpen )
            {
                throw new NullReferenceException( "Serial port is not opened" );
            }

            // check message size
            if ( length > MaxMessageSize )
            {
                throw new ArgumentException( "Too large message" );
            }

            try
            {
                // prepare request buffer
                byte[] requestBuffer = new byte[length + 2];
                requestBuffer[0] = (byte) ( length & 0xFF );
                requestBuffer[1] = (byte) ( ( length & 0xFF00 ) >> 8 );
                Array.Copy( message, offset, requestBuffer, 2, length );
                // send actual request
                port.Write( requestBuffer, 0, requestBuffer.Length );
            }
            catch
            {
                return false;
            }

            return true;
        }

Same methods

SerialCommunication::SendMessage ( byte message ) : bool
SerialCommunication::SendMessage ( byte message, int length ) : bool