Reputation: 109
Goodday everybody,
My problem is the following:
I am communicating with serialport to a zigbee like apparatus and this is working however when i send the following in the C# app:
(string is "0023000D6F000076CF27431e" (in picture send twice)
I get a part of the answer from the apparatus(read window from picture):
However when i send "0023000D6F000076CF27431e" direct to the port by terminal i get the apparatus to answer this:
This is the complete en correct answer.
And of course i need the last line (with the 0024 part in it).
I think (i am not very good in programming yet) this part recieve's the data:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
string txt = port.ReadExisting();
Thread.Sleep(10);
List<PlugwiseMessage> msg = reader.Read(Regex.Split(txt, "\r\n"));
DataReceived(sender, new System.EventArgs(), msg);
I googled about ReadExisting but it seem that this is te correct way to go???? Or is it not ??
Can someone explain this behaviour to me ?
Upvotes: 2
Views: 2638
Reputation: 942257
This is by design. ReadExisting() does what it says, it only returns characters that exist in the serial port read buffer. Serial ports are very slow devices, you normally only get a couple of characters for each ReadExisting() call. This is especially tricky when you use a debugger, that slows your program down a lot, giving the serial port driver time to receive additional characters.
You can use ReadLine() instead, you'll get one line of text from the serial terminal output for each call. That works because the device is sending a linefeed as a line terminator (0x0a in the hex dump). Remove the Sleep() call, that only makes things worse.
Upvotes: 2