Microcontroller reading

I have an Arduino microcontroller. The microcontroller works (I have an application that shows me that the microcontroller spits out data. The fact is that I implemented some code (from http://www.c-sharpcorner.com/uploadfile/eclipsed4utoo/serialportcommunication12082008111137am/serialportcommunication. aspx ):

Here I initialize 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 a listener:

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.

COM port COM17 and the port opens (the LED on the microcontroller is displayed). Any idea why the operation was timed?

i.e. The program downloaded to the microcontroller is configured to "take" data after 1 second.

+3
3

, , . SerialPort.ReadLine() -, . SerialPort.NewLine, ( "\n" ). , Read().

ErrorReceived, . , , , , Baudrate, Parity, Databits Stopbits.

+2

, DataRcvd , , . , , .. ABCDEFGHI (newlinechar), ABC .

, , .

+1

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

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

Source: https://habr.com/ru/post/1776055/


All Articles