Reading serial buffer issues

Hey everybody,…

I am trying to read strings from the serial buffer. As I don’t have the actual data outputting hardware (a PBX) I am now testing with a terminal application, pasting the strings there… I hope that is not what’s causing my troubles…

So the first thing I notice is that the received string is always cut down. Why does it not get all the bytes ? it is a string of 664 bytes (if I am not mistaken)

The second thing I notice is that the string is always cut down in different places ! I am always pasting the same string in HyperTerminal, exactly the same string, and the “available bytes” property in my serial control comes out always different… how come ?

It would really help if you would post your code you are using to read the serial buffer, etc. Without knowing this it could be anything. IF you are reading this in the DataAvailable event you should know that event fires when there is data available. If you read all on that event you may be only reading a portion of what is available as it streams in. There are ways around this but we have no idea how your code is handling this.

Post some code, lots of people here will review it and help.

The DataAvailable event doesn’t mean ALL of it’s there. There are a bunch of things to do to ensure you actually have it all, or you might have a partial message, or that you might have 2 messages. Like Joseph said, you should post your code from the DataAvailable event as a start so we can help you.

ok, so after changing the virtual port emulator (com0com was a pain) I think it’s now working fine… I will post the code here…

One thing I still don’t know how to do is check for “0D 0A” bytes… How can I ?

This is basically what’s in DataAvailable

[code]Do Until PBX_SERIAL.BytesAvailable = 0

s=PBX_SERIAL.ReadAll() // read serial port

CrearLogLlamadas(s) // write log.txt

PBXBufferData = PBXBufferData + s

TimertrayIcon.Enabled = true // turns on/off the tray icon

Loop

PBXBufferData = ReplaceLineEndings (PBXBufferData, EndOfLine.Windows)

ArrayLlamadas = PBXBufferData.Split(EndOfLine) //[/code]

Try adding PBX_SERIAL.poll inside your loop.

after the ReadAll() command ?

Yes. This is from one of my projects:

while me.BytesAvailable > 0 dim s as string = me.ReadAll sSerialBuffer = sSerialBuffer + s me.poll wend

I suggest you read up on both in the Language Reference.

seems to be working fine now… Thanks guys!

I will re read the Language reference :slight_smile:

You might want to have a look at this blog which shows how to look for termination characters in the buffer & process each message discretely.

Problem is I don’t know what are the termination characters I should look for… SO I thought “get all the data in this burst, and once it has stopped recieving, parse it”. The PBX should not stop sending data before it is done, and if it does, that will be an exception…

I know it’s not the best approach but not knowing the terminaton chars… I didn’t know any other way around it…

The terminator for our messages is:

Chr(13) + Chr(10)

which, is simply EndOfLine.Windows. Take a look at the end of your serial message, I bet there will be a termination of some sort even if you can’t see it. Look at the entire string in binary, not as text, and I bet you’ll see a terminator.