views:

120

answers:

3

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?

+2  A: 

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.

volody
A: 

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

Gabe
A: 

See this

Serial 101

dbasnett
That article is fantastic, but I'm struggling with when to apply the encoding he is writing about. I have attempted it when defining the serial port like so: private SerialPort comport = new SerialPort(); comport.Encoding.GetEncoding("Windows-1252");Adding it there gives me an invalid token error. Next to my read statement. comport.Encoding.GetEncoding("Windows-1252"); string data = comport.ReadExisting();It gives me an error: cannot be access with an instance reference. I don't know exactly where to use this. Could you help me out?
rross
in vb Dim sp As New IO.Ports.SerialPort Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'other serial port settings 'before open sp.Encoding = System.Text.Encoding.GetEncoding("windows-1252") End Sub
dbasnett