C# 应用程序中的 SerialPort 问题
我用 C# 创建了一个应用程序,它使用以下命令将 11 字节的数据发送到串行端口:
port = new SerialPort("COM1");
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.ReadTimeout = 1000;
port.WriteTimeout = 1000;
port.Open();
byte[] buffer = new byte[11];
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 3;
buffer[4] = 2;
buffer[5] = 4;
buffer[6] = 1;
buffer[7] = 20;
buffer[8] = 50;
buffer[9] = 0;
buffer[10] = 120;
port.Write(buffer, 0, 11);
然后我编写了另一个应用程序来测试前一个应用程序。我想检查 11 个字节是否正确发送。在此应用程序中,我使用:
using (SerialPort port = new SerialPort("COM1"))
{
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
for (; ; )
{
byte[] b = new byte[11];
port.Read(b, 0, 11);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 11; ++i)
{
sb.Append(b[i]);
sb.Append(" ");
}
Console.WriteLine(sb.ToString());
}
}
接收字节。问题是,在发送这样的内容后:
0 0 0 3 2 4 1 20 50 0 120
我收到:
0 0 0 0 0 0 0 0 0 0 0
0 0 3 2 1 4 20 50 0 120 0
为什么会发生这种情况?我的代码中存在什么样的错误? 谢谢
I made an application in C# which sends 11 byte of data to a serial port using:
port = new SerialPort("COM1");
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.ReadTimeout = 1000;
port.WriteTimeout = 1000;
port.Open();
byte[] buffer = new byte[11];
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 3;
buffer[4] = 2;
buffer[5] = 4;
buffer[6] = 1;
buffer[7] = 20;
buffer[8] = 50;
buffer[9] = 0;
buffer[10] = 120;
port.Write(buffer, 0, 11);
Then I wrote another application to test the previous one. I would like to check if the 11 bytes were correctly sent. In this application I use:
using (SerialPort port = new SerialPort("COM1"))
{
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
for (; ; )
{
byte[] b = new byte[11];
port.Read(b, 0, 11);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 11; ++i)
{
sb.Append(b[i]);
sb.Append(" ");
}
Console.WriteLine(sb.ToString());
}
}
to receive bytes. The problem is that, after sending something like this:
0 0 0 3 2 4 1 20 50 0 120
I receive:
0 0 0 0 0 0 0 0 0 0 0
0 0 3 2 1 4 20 50 0 120 0
Why does it happen? What kind of error is there in my code?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有检查
port.Read()
的结果。它返回读取的字节数,而不是请求的字节数。然后输出循环需要使用这个结果作为上限。尽管您已经在发送端设置了超时,但您也需要在读取器上设置超时。
You're not checking the result from
port.Read()
. It returns the number of bytes read, not the number of bytes requested. The output loop then needs to use this result as the upper limit.Although you've setup timeouts on the sending side, you'll also need them on the reader too.
要检查我的电脑上的串行连接发生了什么,我使用两个工具:
com0com:模拟器这会在电脑上创建两个可以相互通信的串行端口。我遇到的唯一缺点是,您应该将 COM 端口设置为低于 10。
免费串行端口监视器:它嗅探 com 端口,并可以显示线路上运行的每个字节。我遇到的唯一缺点是有时会出现断开连接的问题,并且只有在重新启动整个电脑后才会重新启动。因此,如果您使用它启动会话,请确保在关闭测试应用程序中的连接之前不会断开连接或意外关闭应用程序。
To check what happens on a serial connection on my pc i use two tools:
com0com: A emulator that creates two serial ports on a pc which can communicate with each other. The only drawback i encountered, is that you should set for both a COM port below 10.
Free Serial Port Monitor: It sniffs on a com port and can show every byte that runs over the line. The only drawback i encountered, is that it sometimes has problems to disconnect and will only start again after a restart of the whole pc. So if you start a session with it, be sure you don't disconnect or accidentally close the application before you close the connection within your testing application.