I have an embedded Linux device that I can communicate with through RS232. I am trying to programmatically control it from an existing .NET application, so I was cheating on the System.IO.Ports.SerialPort class. I am currently reading data from the port using the SerialPortDataRecieved event handler and checking the number of bytes read, and then passing the byte[] this size, THEN dragging that byte[] to Queue<byte[]> for further processing, for example:
public class SerialTalk{ SerialPort s; Queue<byte[]> RecQueue=new Queue<byte[]>;
Then another thread then reads from this queue to perform processing, printing to the terminal, etc. This works fine for printing, but I want to be able to reuse the data I retrieve. The problem is that the number of bytes returned by SerialPort is unpredictable and completely unaware of what information it sends, so the pieces of data that I receive are usually fragmented strings.
What I'm looking for is a simple, efficient multi-threaded way of moving from a piece of a byte[] string to a string , which is sorted by string . One of my ideas was to infinitely infinitely alternate between an infinite stream that would dequeue Dequeue byte[] , turn them into string s, add them to StringBuilder , and then split the string returned from StringBuilder.ToString() along newlines , take the LAST string and use it to create a new StringBuilder next time, then insert all the other lines in the Queue<string> - this way the fragments will be returned together ... but this will not work in the most important case when I wait for find the shell prompt, since this line will never be n Substituted in the second place.
Ideas?
source share