串行通讯
我正在用 C# 开发一个应用程序,需要通过串行通信与矩阵开关进行通信。
string value = "abc";
serialPort1.Open();
serialPort1.WriteTimeout = 500;
serialPort1.WriteLine(value);
serialPort1.Close();
矩阵框的状态应该在 WriteLine(value)
时改变。
这是我的问题。当我发送字符串值时,矩阵框的状态不会改变。但是,当我通过 Putty(通过串行通信)发送相同的字符串时,矩阵框会正确响应。所有串行属性都是相同的(波特率、数据位、端口名称等)。
我应该尝试哪些可能的解决方案?
I am developing an application in C# that needs to communicate with a matrix switch through serial communication.
string value = "abc";
serialPort1.Open();
serialPort1.WriteTimeout = 500;
serialPort1.WriteLine(value);
serialPort1.Close();
The matrix box's state is supposed to change upon WriteLine(value)
.
Here is my problem. When I send the string value, the matrix box's state does not change. However, when I send the same string via Putty (through serial communication), the matrix box responds correctly. All serial properties are identical (BaudRate, DataBits, PortName, etc).
What are some possible solutions I should try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在设置
SerialPort
的位置发布您的代码吗?您必须确保所有属性都符合您的矩阵开关的预期。例如:在端口上调用
Open()
后,您可以使用serialPort.IsOpen
属性检查打开或关闭状态。Can you post your code where you setup your
SerialPort
? You have to make sure all properties are as your matrix switch expects. For instance:After you have called
Open()
on your port you can check the open or closed status with theserialPort.IsOpen
property.下载 PortMon http://technet.microsoft.com/en-us/sysinternals/bb896644 .aspx 并使用 Putty 成功运行串行通信,嗅探 PC 和设备之间的所有数据交换。然后执行您的程序并比较交换日志。这应该会给你答案,你的程序中哪里做错了。
Download PortMon http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx and run successful serial communication using Putty, sniffing all data exchange between PC and device. Then execute your program and compare exchange logs. This should give you the answer, what is done incorrect in your program.
我会检查串行端口上的编码属性。看起来默认是 ASCII。也许您的设备需要 Unicode?
I would check the Encoding property on the serial port. It looks like the default is ASCII. Maybe your device is expecting Unicode?