rross
rross

Reputation: 2276

Reading Binary data from a Serial Port

I previously have been reading NMEA data from a GPS via a serial port using C#. Now I'm doing something similar, but instead of GPS from a serial. I'm attempting to read a KISS Statement from a TNC. I'm using this event handler.

comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

Here is port_DataReceived.

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string data = comport.ReadExisting();

        sBuffer = data;

        try
        {
            this.Invoke(new EventHandler(delegate { ProcessBuffer(sBuffer); }));
        }
        catch { }
    }

The problem I'm having is that the method is being called several times per statement. So the ProcessBuffer method is being called with only a partial statment. How can I read the whole statement?

Upvotes: 0

Views: 5256

Answers (3)

dbasnett
dbasnett

Reputation: 11773

See this

Serial 101

Upvotes: 0

Gabe
Gabe

Reputation: 86848

volody is right: You have to look for the FEND (0xC0) and only try to process the buffer when you see it.

Upvotes: 0

volody
volody

Reputation: 7199

Serial communication allows to break data flow into messages by using timeout. But following KISS TNC there no such functionality is presented in this protocol.

Each frame is both preceded and followed by a special FEND (Frame End) character, analogous to an HDLC flag. No CRC or checksum is provided. In addition, no RS-232C handshaking signals are employed.

My suggestion is to break data stream into messages by decoding Frame End characters.

Upvotes: 2

Related Questions