I've used a com port serial device for years to read a serial message, but a recent change in provider meant I had to update the
hardware. Originally I used hs.OpenComPortTerm in Startup.vb, in rawmode (as some characters are non-ascii) and the serial device sent a
termination character that I could detect.
This meant I could buffer the whole message and then process the data in a vb.net script.
Unfortunately the new hardware does not send a termination character or a fixed length message. It also sends a checksum at the
end of the message so I can't check for a particular byte or anything specific.
An example c# code snippet supplied with the new hardware unit simply uses a timeout exception of one second to get the entire message.
Its only 30 or so bytes at 1200 baud.
I've only a limited knowledge of c# but in case it helps the relavent section of code looks like this:
When I now use OpenComPort in statup.vb my vb.net script is called for every read character instead of filling its buffer and then calling
my script only once. Again my script is not in C#
Sure, I can buffer these characters in to a string or an array however very regularly I keep missing the start of the message, or
sometimes get multiple characters or corrupted messages.
I suspect that the script sometimes doesn't execute quickly enough to grab everything as it used to do with hs.OpenComPortTerm
I figured I could use System.IO.Ports in some fashion but don't know how to pick up the event of data arriving
on the comport....or is there a timeout or a better way of solving this problem?
Open to ideas, as this one has me truly stumped?
hardware. Originally I used hs.OpenComPortTerm in Startup.vb, in rawmode (as some characters are non-ascii) and the serial device sent a
termination character that I could detect.
This meant I could buffer the whole message and then process the data in a vb.net script.
Unfortunately the new hardware does not send a termination character or a fixed length message. It also sends a checksum at the
end of the message so I can't check for a particular byte or anything specific.
An example c# code snippet supplied with the new hardware unit simply uses a timeout exception of one second to get the entire message.
Its only 30 or so bytes at 1200 baud.
I've only a limited knowledge of c# but in case it helps the relavent section of code looks like this:
Code:
--SNIP-- private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { // Read all the current buffer into the array for (int c = 0; c != serialPort.BytesToRead; c++) { buffer[pointer] = serialPort.ReadByte(); pointer++; } // If no more bytes then a timeout will occur } catch (TimeoutException x) { // Now parse the received data ... ... --SNIP--
my script only once. Again my script is not in C#
Sure, I can buffer these characters in to a string or an array however very regularly I keep missing the start of the message, or
sometimes get multiple characters or corrupted messages.
I suspect that the script sometimes doesn't execute quickly enough to grab everything as it used to do with hs.OpenComPortTerm
I figured I could use System.IO.Ports in some fashion but don't know how to pick up the event of data arriving
on the comport....or is there a timeout or a better way of solving this problem?
Open to ideas, as this one has me truly stumped?
Comment