Home » C# Programming

Read/Write Serial Port


I know there are a lot of threads out here already on serial ports and I have been looking through them, but so far, I have been unable to find anything that has gotten my program to work.

I am trying to read in information from a temperature controller device that is attached to the computer via rs232 cable. Below is the code I am trying now...

for(int i = 0; i <300000; i++){}
byte[] myData = newbyte[500];
serialPortTemp.Read(myData, 0, 499);

Bassically, the device requires that I send it the ASCii value for the  STX (start of text), which I looked up to be 2. The for loop is to just kill some time because the instrument isn't very fast in replying.  serialPortTemp is a serial port with 9600 baud rate, even parity, 7 bits, and 2 stop bits (this is all entered in the properties on the design form)

From what I can tell, my program is getting hung up on the serialPortTemp.Read(myData, 0, 499) command.  Can some one please let me know what I am doing wrong? This is my first time working with serial port communication and I'm fairly new to C#, so I'm having a rough time getting this all straight.




5 Answers Found


Answer 1


I think there is no 499 bytes you've sent. It's looking like you have to make the asynch read/write. This msdn's article gives you an explanation of working with SerialPort class: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

Check a look how its reading mechanism is implemented.


With regards...


Answer 2

First of all, to kille time use:




Second, how do you know you have 500 bytes to read? Usually device works like this:




And usually the first part of the response is an integer that tells how many bytes will be sent. If you try to read 500 bytes when 200 are sent, the program will likely hang, because it will keep waiting until byte 201 is sent, which will never happen.


You need to check the documentation of your device to know what to expect.





Answer 3


Welcome to MSDN Forums!


If your question has been solved, please don’t forget mark/vote the replies which helped you, and this will encourage the other community members to join in discussion and help each one.


If there’s anything unclear, please feel free to let me know.





[All-In-One Code Framework]

Sample world! You will get more from this world!

Welcome to the new world!


Answer 4

I was just making an array I thought would be big enough to hold the information-I didn't realize that it would try to read until that limit was reached. That is probably what it giving me trouble. How can I find out how many bytes will be sent? In the manual for the temperature controller it says there are 7 bits and 2 stop bits, but that is all the more I could find on the response of the temperature controller.

Is there a particular command or line of code that is used to capture the length of the response?


Answer 5

Hi kemeyer, I beleive there is no way for you to know in advance how many bytes are to be sent, however if you really can't figure out by the manual or other sources, you can read the response byte by byte, until it stops responding. You can create logic to have time out, so when this time-out elapses, you know you read the last byte. Regrads, Fábio


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure