2

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

4
  • 3
    Clearly the scale isn't sending anything back. Check the port settings, particularly Handshake. And make it work with another program first, like Putty or HyperTerminal so you can be sure that the connection is good. Commented Jan 2, 2012 at 19:05
  • Accept some answers on your previous questions, please. Commented Jan 2, 2012 at 19:05
  • Can you please give me more information on how to use Putty or Hyperterminal to check the connection is good. Also I checked all the port settings, however am not able to find out how to check Handshake setting. Commented Jan 2, 2012 at 19:08
  • I have another application that reads the same scale. It is able to continuously read the values from the scale. Commented Jan 2, 2012 at 19:30

6 Answers 6

4

Try this behind your recieve button:

string msg = serialObj.ReadExisting();
MessageBox.Show(msg);
Sign up to request clarification or add additional context in comments.

Comments

3

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 (e.g. 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.

Comments

1

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.

3 Comments

I tried using that. However the application is still hanging on the same line.
If that is the case, make sure that your RS232 cable is not damaged. That was the reason for one of my problems long times ago.
I don't think hardware is the issue. I have another application that is able to read the data from the same scale.
0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.