Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We went through the same process a while ago.</p> <p>The only way to read 'packets' is to have some concept of where the start and end of them are in a stream.</p> <p>From <a href="http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readexisting(VS.80).aspx" rel="noreferrer">msdn</a>:</p> <blockquote> <p>Because the SerialPort class buffers data, and the stream contained in the BaseStream property does not, the two might conflict about how many bytes are available to read. The BytesToRead property can indicate that there are bytes to read, but these bytes might not be accessible to the stream contained in the BaseStream property because they have been buffered to the SerialPort class. </p> </blockquote> <p>We used a backround thread (you could use a BackgroundWorker) to read the data into a buffer. If you can't reliably set the terminating character using the SerialPort.Newline property (because, say, it varies!) then you will need to implement your own packet detection system because you wont be able to use the blocking SerialPort.Readline() method.</p> <p>You could just read into a byte buffer using SerialPort.Read() (or string using the SerialPort.ReadExisting() method) and generate an event when you detect a valid packet in the data. Note that Read() (and I assume ReadExisting() ) empty the SerialPort's buffer, so you'll need to keep the data somewhere else.</p> <p>If you set the SerialPort.ReadTimeout, you can handle the TimeoutException and have an easy way to handle conditions where your device is not transmitting. This is a good way to reset your packet detection if you are using a fixed number of bytes, or some other non-terminated scheme. (use SerialPort.DiscardInBuffer() on a timeout if you don't need partial packets).</p> <p>Good luck </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload