Extra Lines displaying serial data

I am reading serial data from a radio modem (Packet Radio TNC) and getting extra blank lines in the OutputArea. Here is the code in the SerialController.DataAvailable event.

[code]Dim linestr as String = Me.ReadAll(Encodings.ASCII) // Get buffer into a string
OutputArea.AppendText(linestr) // append cleaned string to the OutputArea

'Handle the scroll poistion here, place scroll bar at bottom of window
Dim strNL As String = Chr(133) //Use any ascii char here
OutputArea.Text = ReplaceAll(OutputArea.Text, strNL, “”)
OutputArea.AppendText(strNL)
OutputArea.SelStart = OutputArea.Text.Len
OutputArea.ScrollPosition = OutputArea.LineNumAtCharPos(InStr(OutputArea.Text, strNL))
[/code]
The scroll handling is from another forum post and seems to work fine. I can comment it out and I still get the extra blank lines in my OutputArea text. Slowing down the baud rate makes the problem worse, so I am running at 9600 baud the fastest rate on the TNC. I have looked at the data from the TNC and there are no extra CR/LF in the data. The baud rate change making it worse makes me think there is a timing problem with reading the data from the serial port.

This shows the extra lines. They are completely random, sometimes not occuring for several screens, and occurring in different places in the data stream.

Anyone have any ideas on this issue?

Bob - N4RFC

You may need to use http://documentation.xojo.com/index.php/ReplaceLineEndings to massage the received data.

make sure the packet is completely recieved before you append to the text…
the reception speed should not affect the data content if processed properly

Just for say something: what about flow control? none? xon/xoff? hardware? If so, right cabling, DTR, CTS and that…

Did you tried to capture raw data (use something like this https://realterm.sourceforge.io/ )

In another terminal software, output is correct?

The EndOfLine characters from the TNC are always Carriage Return and Line Feed (hex 0D 0A) . I can turn off the Line Feed and that makes no difference. Using breakpoints I have looked at the data from the TNC and there are no extra EOL sequences.

I have used PUTTY for winders on these devices and it works fine along with Cool Term and Terra Term. (Cool Term is written in XOJO)

The original code was from the Bar Code Reader Serial Port Example that used Lookahead to watch for a Chr(13) (Carriage Return) and that exhibited the same behavior. The Bar Code Reader would send a string with a Carriage Return and then be done. The TNC will send a series of lines with CR/LF EOL sequences. But they are short lines and the Buffer should hold the data. I will look at the settings on the serial port and set the buffer to about 1024 characters and see if that helps. But again, if I was overflowing the buffer I would think that I would lose characters rather than have extra EOL sequences.

I have RTS/CTS hardware flow control active. I would think that failing flow control would miss characters and I have not seen that at all.

Thanks for your input.

I had a similar issue using the DataAvailabe Event with errant EOL characters being inserted even in the middle of a line of incoming data.

I ended up using a timer which reads the data @ every 1/2 second:

[code] //Read ALL available data from the serial port buffer
tempBuffer = Serial1.ReadAll(encodings.ASCII)

//Add the data to incomingData String
incomingData = incomingData + tempBuffer
[/code]
That solved the issue and I’ve never seen the an out of place LF or CR since. This may not suit your case though.

Also, I thought the serial buffer is a big as your available memory, so there should be no overflow issues - at least it is for Windows.

Thanks for the input folks. What I wound up doing was using Lookahead to find the first CR/LF sequence in the buffer using the Instr() function. That gives me a pointer to the CR character. Then I read that number of characters from the buffer plus 1 (for the LF character) and append that to the OutputArea. It seems to work well. I guess I could replace the CR/LF with a Windows EOL, but this is working so I hate to mess with working code!

The buffer on the serial port was set to 4096 so that is a huge buffer for the short strings coming in from the radio packets. Those packets are only 128 characters with long time spans in between packets while the TNC sends an ACK over the 1200 baud half duplex radio link and gets a new packet. If there is a relay station in the connection it can be quite a few seconds before the next packet comes in, so this is not really speedy data!

I printed the length of the string from the ReadAll from the serial and found that I was typically reading only 2 to 7 characters at a time from the port. Looking for the CR/LF at least gets me one whole line from the serial buffer into the OutputArea.

Dim buline as Integer = Instr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) If buline >0 Then buline = buline+ 1 OutputArea.AppendText(Me.Read(buline,Encodings.ASCII)) 'Handle the scroll position here, place scroll bar at bottom of window Dim strNL As String = Chr(133) //Use any ascii char here OutputArea.Text = ReplaceAll(OutputArea.Text, strNL, "") OutputArea.AppendText(strNL) OutputArea.SelStart = OutputArea.Text.Len OutputArea.ScrollPosition = OutputArea.LineNumAtCharPos(InStr(OutputArea.Text, strNL)) End If
Let’s call this one FIXED! Thanks again!

Bob - N4RFC

Glad you got it sorted. :slight_smile: