从微控制器读取

发布于 2024-10-04 07:00:16 字数 1116 浏览 0 评论 0 原文

我有一个 Arduino 微控制器。微控制器可以工作(我有一个应用程序可以向我显示微控制器“吐出”数据。问题是我已经实现了一些代码(来自 http://www.c-sharpcorner.com/uploadfile/eclipsed4utoo/serialportcommunication12082008111137am/serialportcommunication.aspx):

这里我初始化串行端口

_serialPort = new SerialPort("COM17", 19200, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
_serialPort.Open();

这里我有监听处理程序:

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Thread.Sleep(1000);
    string data = _serialPort.ReadLine();
    this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}

但我收到此错误:操作已超时。位于处理程序中的string data = _serialPort.ReadLine();

。端口是 COM17 并且端口打开(微控制器上的 LED 显示它)

I have a Arduino micro-controller . The micro-controller works(i have an app that shows me the the micro-controller 'spits' out data. The thing is that i have implemented some code ( from http://www.c-sharpcorner.com/uploadfile/eclipsed4utoo/serialportcommunication12082008111137am/serialportcommunication.aspx ) :

Here i initialize the SerialPort

_serialPort = new SerialPort("COM17", 19200, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
_serialPort.Open();

And here i have the listening handler :

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Thread.Sleep(1000);
    string data = _serialPort.ReadLine();
    this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}

But i get this error :The operation has timed out. at string data = _serialPort.ReadLine(); in the handler .

The COM port is COM17 and the port opens ( a led on the micro-controller shows it ). Any idea why the operation timed out ?

i.e. The program loaded into the micro-controller is set to 'shoot' data at 1 second.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

看透却不说透 2024-10-11 07:00:16

问题的根源很可能在电线的另一端。为了让 SerialPort.ReadLine() 完成并且不生成超时错误,您必须传输行结束字符序列。 SerialPort.NewLine 的值,默认为换行控制字符(“\n”)。如果您传输字节而不是字符,那么您应该使用 Read() 代替。

您还应该实现 ErrorReceived 事件,以便可以检测通信错误。当通信参数不匹配时,您将得到这种类型,例如波特率、奇偶校验、数据位和停止位。

The source of the problem is likely on the other end of the wire. To let SerialPort.ReadLine() complete and not generate a time-out error you must transmit a line end character sequence. The value of SerialPort.NewLine, which defaults to the line feed control character ("\n"). If you transmit bytes instead of characters then you should use Read() instead.

You should also implement the ErrorReceived event so you can detect communication errors. The kind you'll get when the communication parameters do not match, like Baudrate, Parity, Databits and Stopbits.

妞丶爷亲个 2024-10-11 07:00:16

您不能假设当 DataRcvd 事件处理程序触发时您已拥有另一端发送的所有内容。您可能正在接收片段,即微控制器发送 ABCDEFGHI(newlinechar) 并且事件处理程序在缓冲区中触发 ABC。

我同意,如果微控制器不发送换行符,那么它永远不会工作。

You can't assume that when the DataRcvd event handler fires that you have everything that was sent by the other end. You likely are receiving pieces i.e. the micro controller sends ABCDEFGHI(newlinechar) and the event handler fires with ABC in the buffer.

I agree that if the micro controller is not sending newline then it will never work.

知你几分 2024-10-11 07:00:16

如果有问题的 Arduino 是 Leandro、Micro 或其他基于 Atmega32u4 的板,您需要将 RTS 和 DTR 设置为高电平,否则您将不会收到任何数据。

_port.Handshake = Handshake.None;
_port.Open();
_port.RtsEnable = true;
_port.DtrEnable = true;

If the Arduino in question is a Leandro, Micro, or other Atmega32u4 based board, you'll need to set RTS and DTR high, otherwise you'll not receive any data.

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