Cod*_*kie 6 c# core raspberry-pi win-universal-app windows-10
在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);
Run Code Online (Sandbox Code Playgroud)
基本上我只是希望能够从串口读取消息并根据该消息执行操作.
如何使用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 ) {
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15056 次 |
最近记录: |