I have a serial device that has a binary output and I capture the data using the following.
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int count = sp.BytesToRead;
byte[] data = new byte[count];
sp.Read(data, 0, data.Length);
file.WriteLine(BitConverter.ToString(data));
}
The data comes through and looks like this…
06-14-F2-A1-64-2D-62-00-1A-31-00-06-14-F3-84-62-59-01-00-1A-31-00-06-14-F3-85-56-52-55-31
1A-31-00-06-14-F4-18-04-2E-62-00-1A-31-00-06-14-F4-E3-27-5B-01-00-1A-31-00-06-14-F4-E4-1C-51-55-31
1A-31-00-06-14-F5-71-4C-59-71-20-1A-31-00-06-14-F5-8E-A5-2E-62-00-1A-31-00-06-14-F5-F4-47-56-55-31-1A-31-00-06-14-F6-10-1A-1A-31-52-24-1A-31-00-06-14-F6-3D-40-19-70-00-1A-31-00-06-14-F6-3E-9C-4C-55-31-1A-33-00-06-14-F6-F6-11-3D-A0-00-17-B0-C8-4E-42-70-AA-00-00-59-51-1E-1A-31-00-06-14-F7-05-4A-2E-62-00-1A-31-00-06-14-F7-83-5C-56-55-31-1A-31-00-06-14-F7-99-04-5A-01-00-1A-31-00-06-14-F7-99-F8-51-55-31-1A-31-00-06-14-F8-7B-EA-2E-62-00-1A-31-00-06-14-F9-00-CE-56-01-00-1A-31-00-06-14-F9-0E-DF-51-55-31-1A-31-00-06-14-F9-F2-8B-2B-62-00-1A-31-00-06-14-FA-15-1F-1D-05-30-1A-31-00-06-14-FA-62-4D-59-01-00-1A-31-00-06-14-FA-63-41-55-55-31-1A-31-00-06-14-FA-6F-6E-1D-67-67-1A-31-00-06-14-FA-EC-50-2E-72-00-1A-31-00-06-14-FB-22-96-38-62-00-1A-31-00-06-14-FB-3B-7A-40-20-43-1A-31-00-06-14-FB-69-2E-2B-62-00-1A-31-00-06-14-FC-62-F1-2D-72-00-1A-31-00-06-14-FC-DF-D1-2E-62-00-1A-31-00-06
The hex isn’t the issue here as I can decode that but a statement I am looking for begins with 1A-31 and then is a set number of bytes long. As you can see the serial stream in this case starts mid flow and so is not a full statement.
How can I look for this marker, discard the beginning and then start processing. Also bear in mind that this will happen multiple times as the readBuffer at some point will truncate the stream and I will need to piece it back together again?
You’re almost there. Your problem is that the data you’re streaming comes in chunks, which misalign with where statements begin and end. I’m going to assume that the end of a statement is found by the 1A-31 that identifies the start of the next statement. If this isn’t true, reinterpret this answer accordingly.
Now, you will not be able to do anything with the very first pieces of data in your example, which contains half a statement. So, let’s start with assuming that the first chunk of data you get indeed starts with 1A-31.
There are now two options:
StatementReceivedevent and send it there, or something like that). Repeat this exercise until the chunk has been entirely processed.port_DataReceivedcall.If the second option was the case, you know that the data for the next
port_DataReceivedwill not begin with 1A-31 (because the temporary buffer is non-empty). However, you can scan to the end of it (until the next 1A-31), prepend the temporary buffer (stored in the previousport_DataReceivedcall) to it, and raiseStatementReceivedand erase the temporary buffer.With a similar approach, you can also deal with statements that require more than 2 chunks of data to be sent; each time you do not encounter a 1A-31, append the received data to the temporary buffer, until the statement is complete.
Finally, if the very first bytes that you read upon startup do not start with 1A-31, you’ll just have to discard those. Can’t do something with half a statement.