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

标签 c# raspberry-pi windows-10 win-universal-app

在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/

相关文章:

c# - 从 Java 调用 Unity 函数

C# 创建窗口——定义父窗口

python - 接受按钮按下后,无需再按Enter

raspberry-pi - 使用运动在树莓派上进行离线网络摄像头流

driver - Windows 10 中是否删除了 NDIS 5.x 驱动程序的兼容性?

delphi - 处理 Windows 10 上 DPI(文本大小)的运行时更改

c# - WCF Duplex - 如何找到客户端的回调 URL?

arduino - 在 Raspberry Pi、Arduino 和 JavaScript 之间使用 MQTT

sql - 如何将带有深色主题的 SQL Management Studio 2017 与 Windows 10 高对比度主题结合使用?

c# - 如何强制网格在更改时立即将值传播到数据源?