tanjiajun_34 Posted May 2, 2012 Share Posted May 2, 2012 My device is currently sending a number value through SerialPort to the computer. Here is how it is sending the value.... "210 211 211 210 211 211 210 211 211 210 211 211 210 ....." something like this. How do i read the latest value? The datareceived event is sometimes a bit of slow. Link to comment https://www.neowin.net/forum/topic/1074439-c-serialport-question/ Share on other sites More sharing options...
0 Karl L. Posted May 4, 2012 Share Posted May 4, 2012 I believe it would be helpful if you told us which device you are using and what you are trying to accomplish, specifically. Maybe post a little code that you are having trouble with. Your description is quite vague. While I have never tried interfacing with a serial port in C#, or Windows in general, I will try to help anyway. I worked on a project recently that involved interfacing with an Arduino board over its serial connection. For lack of further details on your part, I'm going to assume that your program will interface fairly similarly. I found this blog post by John Ciacia that describes interacting with the Arduino in C# in great detail. In fact, the demonstration code in his post looks very similar to my own implementation (in C++ for Linux, however). For the record, I had the same problem as you with the data received being somewhat slow. Essentially, you need to know exactly how many characters you need to read from the serial device before starting the read operation, and block until you receive that specific number of characters. In my case, I found it easiest to send and receive only one character at a time. That worked for me because I didn't have exceptionally complex instructions to carry out. Since your device's output sequence seems to be 3 integers followed by a space, that approach may work for you too (but waiting for four characters instead of just one before returning). If you wish, I will post my class so you can look at how I solved the problem. Note, however, that it is in a different language for a different operating system, although the concepts should be fairly similar. PS: Make sure you are initializing the connection with the correct baud rate. You will not be able to get anything done if the baud rate is wrong! Link to comment https://www.neowin.net/forum/topic/1074439-c-serialport-question/#findComment-594843747 Share on other sites More sharing options...
0 tanjiajun_34 Posted May 12, 2012 Author Share Posted May 12, 2012 I manged to get something like that working using SerialPort1.ReadTo(" ") in the data received event. But its not so reliable to me well it still works :) Link to comment https://www.neowin.net/forum/topic/1074439-c-serialport-question/#findComment-594860919 Share on other sites More sharing options...
Question
tanjiajun_34
My device is currently sending a number value through SerialPort to the computer. Here is how it is sending the value....
"210 211 211 210 211 211 210 211 211 210 211 211 210 ....." something like this.
How do i read the latest value? The datareceived event is sometimes a bit of slow.
Link to comment
https://www.neowin.net/forum/topic/1074439-c-serialport-question/Share on other sites
2 answers to this question
Recommended Posts