读取串口 C#.net 2.0 获取称重机输出时出现问题
我正在尝试在 Windows XP 计算机上使用 C#.net 2.0 中的以下代码从 Sartorius 秤型号 No BS2202S 读取重量:
public string readWeight()
{
string lastError = "";
string weightData = "";
SerialPort port = new SerialPort();
port.PortName = "COM1";
port.BaudRate = 9600;
port.Parity = Parity.Even;
port.DataBits = 7;
port.StopBits = StopBits.One;
port.Handshake = Handshake.RequestToSend;
try {
port.Open();
weightData = port.ReadExisting();
if(weightData == null || weightData.Length == 0) {
lastError = "Unable to read weight. The data returned form weighing machine is empty or null.";
return lastError;
}
}
catch(TimeoutException) {
lastError = "Operation timed out while reading weight";
return lastError;
}
catch(Exception ex) {
lastError = "The following exception occurred while reading data." + Environment.NewLine + ex.Message;
return lastError;
}
finally {
if(port.IsOpen == true) {
port.Close();
port.Dispose();
}
}
return weightData;
}
我能够使用具有相同序列号的超级终端应用程序(随 Windows XP 提供)读取重量上面给出的用于打开端口的端口参数。但从上面的代码片段中,我可以打开端口,并且每次它都返回空数据。
我尝试使用给定的代码这个Stack Overflow线程打开端口,仍然它返回空数据。
请帮助我。
I'm trying to read weight from Sartorius Weighing Scale model No BS2202S using the following code in C#.net 2.0 on a Windows XP machine:
public string readWeight()
{
string lastError = "";
string weightData = "";
SerialPort port = new SerialPort();
port.PortName = "COM1";
port.BaudRate = 9600;
port.Parity = Parity.Even;
port.DataBits = 7;
port.StopBits = StopBits.One;
port.Handshake = Handshake.RequestToSend;
try {
port.Open();
weightData = port.ReadExisting();
if(weightData == null || weightData.Length == 0) {
lastError = "Unable to read weight. The data returned form weighing machine is empty or null.";
return lastError;
}
}
catch(TimeoutException) {
lastError = "Operation timed out while reading weight";
return lastError;
}
catch(Exception ex) {
lastError = "The following exception occurred while reading data." + Environment.NewLine + ex.Message;
return lastError;
}
finally {
if(port.IsOpen == true) {
port.Close();
port.Dispose();
}
}
return weightData;
}
I'm able to read the weight using Hyperterminal application (supplied with Windows XP) with the same serial port parameters given above for opening the port. But from the above code snippet, I can open the port and each time it is returning empty data.
I tried opening port using the code given this Stack Overflow thread, still it returns empty data.
Kindly assist me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这可能已经过时了……但供将来参考……
看看握手。有硬件握手和软件握手两种。您的问题可能是其中之一 - 所以您需要两者都尝试。
对于硬件握手,您可以尝试:
请注意,
我不认为设置某些串行设备可能需要的 DTR 线
软件握手也称为 XON/XOFF,可以使用
OR
您可能仍然需要启用 DTR
设置当所有其他方法都失败时, -不要忘记检查所有这些握手组合。
I know this is probably old now ... but for future reference ...
Look at the handshaking. There is both hardware handshaking and software handshaking. Your problem could be either - so you need to try both.
For hardware handshaking you can try:
Note that
I do not think sets the DTR line which some serial devices might require
Software handshaking is also known as XON/XOFF and can be set with
OR
You may still need to enable DTR
When all else fails - dont forget to check all of these combinations of handshaking.
由于其他人将来可能会遇到此问题,因此握手是一个可选选项。
在大多数天平中,您将看到软件、硬件 2 字符、硬件 1 字符的选项。 Sartorius 天平的默认设置是硬件 2 字符。我通常建议更改为软件。
此外,如果它完全停止工作,通常可以通过使用 9 1 1 参数默认单位来修复。然后重置通讯设置。
有关如何更改设置的示例,请参阅本页的手册:
http://www.dataweigh.com/products/sartorius/cpa-analytical-balances/
Since someone else will probably have trouble with this in the future, hand shaking is a selectable option.
In most of the balances you will see the options for Software, Hardware 2 char, Hardware 1 char. The default setting for the Sartorius balances is Hardware 2 Char. I usually recommend changing to Software.
Also if it stops working all together it can often be fixed by defaulting the unit using the 9 1 1 parameter. And then resetting the communication settings.
An example of how to change the settings can be found on the manual on this page:
http://www.dataweigh.com/products/sartorius/cpa-analytical-balances/