简体   繁体   中英

Reading Data from serial port in c#

I am trying to read data from the serial port. The data comes off of a scale. I first send a command to start reading the scale.

_serialPort.Write("P");

then after waiting for some time I am trying to read using

temp2 = _serialPort.ReadLine();

The application hangs at this line of code. I have also tried the Read function but am getting the same result. ReadExisting() function returns an empty string.

As the ReadLine() is not timing out or throwing any exceptions I am unable to debug this issue as I have no further information. The port cannot be defective as WriteLine() and ReadExisting() are working.

Any help would be appreciated.

Thanks

Try this behind your recieve button:

string msg = serialObj.ReadExisting();
MessageBox.Show(msg);

With comms, start with a terminal program. That is a program that lets you type commands that are sent out of the serial port to your device, and displays any text that comes back from the device. This will allow you to try out the protocol you've described and see what the device sends back. Once you get the correct response, you know:

  • The device is working
  • The serial cable is correctly wired and working
  • You are using the correct port settings (baud rate, stop bits, parity, etc)
  • You understand what the protocol is (do you send a "P" or a "P" followed by a newline?)

Then you can run your code in the knowledge that if it doesn't work, it's something in your code that is wrong, not any other factors muddying the waters.

Once you send the initial command, you can read the serial port immediately for the response - the call you are using will simply wait until some data is received. If no data is received, it will wait forever (hence your program hang). Any of the above things being incorrect will cause the same symptom of not receiving any data. And changing the way you read the data (eg async reads etc) will not change the fact that there is no data being received.

The other thing to be careful of is port settings. Generally, start with the default settings for most things (as RS232 is used in a pretty standard way by most devices) - a typical beginner mistake is to explicitly set options like the Handshaking approach, but if you get it wrong it'll break the comms. Usually you'd specify a baud rate and 8N1 (8 bits, no parity, 1 stop bit) and leave the other settings alone until you find they need to be set to get anything wto work, or you know (as in their manual states it in black and white) that your device requires something different.

Change your Write function to WriteLine. That may help. In addition, take a look at the sample of this link at MSDN. It is helpful.

you can use that part to send a data to serial port from c#:

int MyInt3 = Convert.ToInt32(textBox3.Text);
byte[] p = BitConverter.GetBytes(MyInt3);
serialPort1.Write(p, 0, 1);

And after that you can use Read() method.

Check that the SerialPort.NewLine property is set correctly. If you receive a CR character only, and the ReadLine expect eg. CRLF, your application will hung.

Does the scale respond to each "P" sent to it, or is "P" a command that tells it to begin sending a continuous stream of data? To use a terminal program, set its baud rate and protocol to the same as the scale (and your program), and use it to send a "P" to the scale in place of doing that with your program. Any response from the scale should appear in the terminal window. I prefer termie or termite (both downloadable) hyperterminal is pretty bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM