我有一个 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.
发布评论
评论(3)
问题的根源很可能在电线的另一端。为了让 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.
您不能假设当 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.
如果有问题的 Arduino 是 Leandro、Micro 或其他基于 Atmega32u4 的板,您需要将 RTS 和 DTR 设置为高电平,否则您将不会收到任何数据。
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.