简体   繁体   English

在C#中从串行端口读取数据

[英]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. ReadExisting()函数返回一个空字符串。

As the ReadLine() is not timing out or throwing any exceptions I am unable to debug this issue as I have no further information. 由于ReadLine()不会超时或引发任何异常,因此我无法调试此问题,因为我没有更多信息。 The port cannot be defective as WriteLine() and ReadExisting() are working. 由于WriteLine()ReadExisting()正在工作,因此端口不能有缺陷。

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?) 您了解协议是什么(您发送的是“ P”还是“ P”后跟换行符?)

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. 通常,从大多数设备的默认设置开始(因为大多数设备以相当标准的方式使用RS232)-一个典型的初学者错误是显式设置诸如握手方法之类的选项,但是如果弄错了,它将破坏通讯科。 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. 通常,您需要指定一个波特率和8N1(8位,无奇偶校验,1个停止位),然后不理会其他设置,直到发现需要对其进行设置才能使其正常工作,或者您知道 (如其手动状态) (黑色和白色)表示您的设备需要使用其他内容。

Change your Write function to WriteLine. 将您的Write函数更改为WriteLine。 That may help. 这可能有所帮助。 In addition, take a look at the sample of this link at MSDN. 此外,请查看MSDN上此链接的样本。 It is helpful. 这很有帮助。

you can use that part to send a data to serial port from c#: 您可以使用该部分将数据从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. 之后,您可以使用Read()方法。

Check that the SerialPort.NewLine property is set correctly. 检查是否正确设置了SerialPort.NewLine属性。 If you receive a CR character only, and the ReadLine expect eg. 如果仅收到一个CR字符,并且ReadLine应为例如。 CRLF, your application will hung. CRLF,您的应用程序将挂起。

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? 秤是否响应发送给它的每个“ P”,还是“ P”是一个告诉其开始发送连续数据流的命令? 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. 要使用终端程序,请将其波特率和协议设置为与标尺(和您的程序)相同的值,并使用它向标尺发送“ P”,以代替对您的程序执行此操作。 Any response from the scale should appear in the terminal window. 秤的任何响应应显示在终端窗口中。 I prefer termie or termite (both downloadable) hyperterminal is pretty bad. 我更喜欢白蚁或白蚁(均可下载)超级终端,这是非常糟糕的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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