How do you read the serial port with the Windows 10 kernel

using python on a Raspberry PI. I use similar code as shown below to read data from the serial port:

baud = 9600 # baud rate port = '/dev/ttyACM0' # serial URF port on this computer ser = serial.Serial(port, baud) ser.timeout = 0 var message = ser.read(9); 

Essentially, I just want to be able to read the message from the serial port and perform an action based on that message.

How can this be achieved with Windows 10 Core and C #, can someone point me in the right direction or provide me some sample code?

Thanks to Advance for the help you received.

+6
source share
3 answers

it turns out that the serial port on PI is not yet supported, which is very frustrating: https://www.raspberrypi.org/forums/viewtopic.php?t=109047&p=751638

Here is a supported method:

 serialPort = await SerialDevice.FromIdAsync(comPortId); serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); serialPort.BaudRate = 115200; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; serialPort.DataBits = 7; serialPort.Handshake = SerialHandshake.None; serialPort.IsRequestToSendEnabled = true; dataReaderObject = new DataReader(serialPort.InputStream); // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available dataReaderObject.InputStreamOptions = InputStreamOptions.Partial; // Create a task object to wait for data on the serialPort.InputStream loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken); // Launch the task and wait UInt32 bytesRead = await loadAsyncTask; if (bytesRead > 0) { try { var msg = dataReaderObject.ReadString(bytesRead); } catch (Exception ex ) { } } 
+4
source

It is supported as with Windows 10 IoT Core version 10.0.10586.0. See: https://ms-iot.imtqy.com/content/en-US/win10/samples/SerialSample.htm

+3
source

just use the SerialPort class

 using System.IO.Ports; SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); port.Open(); byte b = port.ReadByte(); // etc 
+1
source

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


All Articles