Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



[C#] SerialPort Question


2 replies to this topic - - - - -

#1 tanjiajun_34

    Resident Fanatic

  • 979 posts
  • Joined: 14-June 10

Posted 02 May 2012 - 11:15

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.


#2 xorangekiller

    Delightfully Insane

  • 723 posts
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Wheezy

Posted 04 May 2012 - 01:54

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!

#3 OP tanjiajun_34

    Resident Fanatic

  • 979 posts
  • Joined: 14-June 10

Posted 12 May 2012 - 05:36

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 :)