Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialPort ReadLine() after Thread.Sleep() goes crazy
    primarykey
    data
    text
    <p>I've been fighting with this issue for a day and I can't find answer for it. I am trying to read data from GPS device trough COM port in Compact Framework C#. I am using SerialPort class (actually my own ComPort class boxing SerialPort, but it adds only two fields I need, nothing special). </p> <p>Anyway, I am running while loop in a separate thread which reads line from the port, analyze NMEA data, print them, catch all exceptions and then I Sleep(200) the thread, because I need CPU for other threads... Without Sleep it works fine, but uses 100% CPU.. When I don't use Sleep after few minutes the output from COM port looks like this:</p> <blockquote> <p>GPGSA,A,3,09,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F<br> GSA,A,3,09,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F<br> A,A,3,09,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F<br> ,18,12,271,24,24,05,020,24,14,04,326,25,11,03,023,*76<br> A,3,09,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F<br> 3,09,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F<br> 09,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F<br> ,12,22,17,15,27,,,,,,,2.6,1.6,2.1*3F </p> </blockquote> <p>as you can see the same message is read few times but <strong>cut</strong>. I wonder what I'm doing wrong... My port configuration:</p> <pre><code>port.ReadBufferSize = 4096; port.BaudRate = 4800; port.DataBits = 8; port.Parity = Parity.None; port.StopBits = StopBits.One; port.NewLine = "\r\n"; port.ReadTimeout = 1000; port.ReceivedBytesThreshold = 100000; </code></pre> <p>And my reading function:</p> <pre><code>private void processGps(){ while (!closing) { //reconnect if needed try { string sentence = port.ReadLine(); //here print the sentence //analyze the sentence (this takes some time 50-100ms) } catch (TimeoutException) { Thread.Sleep(0); } catch (IOException ioex) { //handling IO exception (some info on the screen) } Thread.Sleep(200); } } </code></pre> <p>There is some more stuff in this function like reconnection if the device is lost etc., but it is not called when the GPS is connected properly. I was trying</p> <pre><code>port.DiscardInBuffer(); </code></pre> <p>after some blocks of code (in TimeoutException, after read.)</p> <p>Did anyone had similar problem? I really dont know what I'm doing wrong.. The only way to get rig of it is removing the last Sleep.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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