从COM口读取数据
我有一个小问题。 我尝试从 COM 端口读取数据,但收到“坏”数据,如下所示:
0 FL-1 我2 3 ? 4 米? 5 6 7 8 9 10 ? 11 12 13 d 14 d 15
打开端口的代码是:
_port = new SerialPort(Settings.Default.COM, 9600, Parity.None, 8, StopBits.One);
_port.DataReceived += PortDataReceived;
_port.Open();
读取数据的代码是:
private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
string s = _port.ReadExisting();
_reportBuffer = _reportBuffer + s;
_counter++;
if (_counter == _messageLength)
{
//action
}
}
欢迎任何想法! 提前致谢!
I have a little problem.
I try to read data from COM port and I receive "bad" data like following:
0
Fl- 1
i 2
3
? 4
m? 5
6
7
8
9
10
? 11
12
13
d 14
D 15
The code that open port is:
_port = new SerialPort(Settings.Default.COM, 9600, Parity.None, 8, StopBits.One);
_port.DataReceived += PortDataReceived;
_port.Open();
The code that read data is:
private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
string s = _port.ReadExisting();
_reportBuffer = _reportBuffer + s;
_counter++;
if (_counter == _messageLength)
{
//action
}
}
Any Ideas are welcome!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您确定您接收到的数据不是正在发送的数据,则值得仔细检查波特率、奇偶校验设置等。
如果一切设置正确,但您仍然没有收到所发送的数据发送时,使用终端模拟器打开COM端口。如果您可以看到正确的数据,那么问题就出在您的代码上;如果你不能,问题就出在其他地方。
If you're certain that the data you're receiving isn't what's being sent, it is worth double-checking the baud rate, parity setttings etc.
If everything is set up correctly, and you're still not getting the data that's being sent, use a terminal emulator to open the COM port. If you can see the correct data there, then the problem is with your code; if you can't, the problem is elsewhere.