gpt4 book ai didi

c# - windows 10 core如何读取串口

转载 作者:太空狗 更新时间:2023-10-29 21:33:18 26 4
gpt4 key购买 nike

在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 core如何读取串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com