串行通讯

发布于 2024-09-30 12:54:50 字数 374 浏览 0 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光倒影 2024-10-07 12:54:50

您可以在设置 SerialPort 的位置发布您的代码吗?您必须确保所有属性都符合您的矩阵开关的预期。例如:

// Setup port
SerialPort serialPort = new SerialPort();
serialPort.PortName = portName; //eg. COM1
serialPort.BaudRate = 9600;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.Handshake = Handshake.None;
serialPort.NewLine = "\r\n";
serialPort.ReadTimeout = 2000;
serialPort.WriteTimeout = 1000;

在端口上调用 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:

// Setup port
SerialPort serialPort = new SerialPort();
serialPort.PortName = portName; //eg. COM1
serialPort.BaudRate = 9600;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.Handshake = Handshake.None;
serialPort.NewLine = "\r\n";
serialPort.ReadTimeout = 2000;
serialPort.WriteTimeout = 1000;

After you have called Open() on your port you can check the open or closed status with the serialPort.IsOpen property.

怕倦 2024-10-07 12:54:50

下载 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.

若水般的淡然安静女子 2024-10-07 12:54:50

我会检查串行端口上的编码属性。看起来默认是 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文