windows10核如何读取串口

How do you read the serial port with windows 10 core

在Raspberry PI上使用Python,我使用类似于下面显示的代码从串口读取数据:

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);

本质上我只是希望能够读取串行端口的消息并根据该消息执行操作。

如何使用 Windows 10 Core 和 c# 来实现,谁能指出正确的方向或提供代码示例?

原来PI上的串口还不支持,很郁闷:https://www.raspberrypi.org/forums/viewtopic.php?t=109047&p=751638

支持的方式如下:

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 ) {
                }
}            

从 Windows 10 IoT Core 版本 10.0.10586.0 开始支持。

参见:https://github.com/microsoft/Windows-iotcore-samples/tree/develop/Samples/SerialUART