从COM口读取数据

发布于 2024-12-27 23:30:54 字数 658 浏览 1 评论 0原文

我有一个小问题。 我尝试从 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 技术交流群。

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

发布评论

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

评论(1

ぽ尐不点ル 2025-01-03 23:30:54

如果您确定您接收到的数据不是正在发送的数据,则值得仔细检查波特率、奇偶校验设置等。

如果一切设置正确,但您仍然没有收到所发送的数据发送时,使用终端模拟器打开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.

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