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

关于c# - 如何使用Windows 10核心读取串行端口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30183836/

10-13 06:57