• I have been playing around with the serial port class for about a week and have managed to learn a small number of different ways to read the data in and then act upon it.

    I have just successfully written code to use the serialPort ->ReadByte() method.

    My problem with this method is that no matter how large (number of bytes) the data is, it will only read one byte at a time when data is received.

    I want to read a small data packet which contains three characters.  I have attempted to use the serialPort->Read() method, but I keep getting a null error.

    My code is:

    private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {

    int offset = 0;

    int count = 2;

    array<unsigned char>^ buffer;

    serialPort1->Read(buffer, offset, count);


    Am I using the method correctly?

    I want to return the three characters into an "array" (excuse me if this term is not correct) so that I can access the charatcers individually:

    if( buffer[0]  == 0x02 ) etc etc

    Friday, September 01, 2006 2:31 PM


  • The DataReceived event fires when the serial port stream has one or more characters/bytes available.  Most typically, it gets called and there'll be just one new byte.  You'll need to append to bytes to your array until you 'have enough', then interpret the response.  Try using the BytesToRead property to see how many bytes are available.
    Friday, September 01, 2006 3:42 PM