如何使用通用 Windows 应用程序将串行数据写入 COM 端口?
How to write serial data to COM ports with Universal Windows Application?
通常 C# 应用程序使用 System.IO.Ports
像这样:
SerialPort port = new SerialPort("COM1");
port.Open();
port.WriteLine("test");`
但通用 Windows 应用程序不支持 System.IO.Ports
因此无法使用此方法。有谁知道如何通过 UWA 中的 COM 端口写入串行数据?
您可以使用 Windows.Devices.SerialCommunication and Windows.Storage.Streams.DataWriter 类:
The classes provide functionality to discover such serial device, read and write data, and control serial-specific properties for flow control, such as setting baud rate, signal states.
通过向 Package.appxmanifest
添加以下功能:
<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
然后运行下面的代码:
using Windows.Devices.SerialCommunication;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
//...
string selector = SerialDevice.GetDeviceSelector("COM3");
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
if(devices.Count > 0)
{
DeviceInformation deviceInfo = devices[0];
SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
serialDevice.BaudRate = 9600;
serialDevice.DataBits = 8;
serialDevice.StopBits = SerialStopBitCount.Two;
serialDevice.Parity = SerialParity.None;
DataWriter dataWriter = new DataWriter(serialDevice.OutputStream);
dataWriter.WriteString("your message here");
await dataWriter.StoreAsync();
dataWriter.DetachStream();
dataWriter = null;
}
else
{
MessageDialog popup = new MessageDialog("Sorry, no device found.");
await popup.ShowAsync();
}
Microsoft 提供了一个访问和使用 com 端口的示例,使用 class SerialDevice,在通用 Windows 应用程序中,名为 CustomSerialDeviceAccess。
Microsoft 已将其发布在 GitHub 上。你可以在这里找到它:
https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CustomSerialDeviceAccess
Microsoft 关于示例应用程序的说明:
"This sample allows the user to configure and communicate with a Serial
device. You can choose one of four scenarios:
Connect/Disconnect using Device Selection list; Configure the Serial
device; Communicate with the Serial device; Register for Events on the
Serial device"
参考资料:微软,GitHub
通常 C# 应用程序使用 System.IO.Ports
像这样:
SerialPort port = new SerialPort("COM1");
port.Open();
port.WriteLine("test");`
但通用 Windows 应用程序不支持 System.IO.Ports
因此无法使用此方法。有谁知道如何通过 UWA 中的 COM 端口写入串行数据?
您可以使用 Windows.Devices.SerialCommunication and Windows.Storage.Streams.DataWriter 类:
The classes provide functionality to discover such serial device, read and write data, and control serial-specific properties for flow control, such as setting baud rate, signal states.
通过向 Package.appxmanifest
添加以下功能:
<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
然后运行下面的代码:
using Windows.Devices.SerialCommunication;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
//...
string selector = SerialDevice.GetDeviceSelector("COM3");
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
if(devices.Count > 0)
{
DeviceInformation deviceInfo = devices[0];
SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
serialDevice.BaudRate = 9600;
serialDevice.DataBits = 8;
serialDevice.StopBits = SerialStopBitCount.Two;
serialDevice.Parity = SerialParity.None;
DataWriter dataWriter = new DataWriter(serialDevice.OutputStream);
dataWriter.WriteString("your message here");
await dataWriter.StoreAsync();
dataWriter.DetachStream();
dataWriter = null;
}
else
{
MessageDialog popup = new MessageDialog("Sorry, no device found.");
await popup.ShowAsync();
}
Microsoft 提供了一个访问和使用 com 端口的示例,使用 class SerialDevice,在通用 Windows 应用程序中,名为 CustomSerialDeviceAccess。
Microsoft 已将其发布在 GitHub 上。你可以在这里找到它:
https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CustomSerialDeviceAccess
Microsoft 关于示例应用程序的说明:
"This sample allows the user to configure and communicate with a Serial device. You can choose one of four scenarios:
Connect/Disconnect using Device Selection list; Configure the Serial device; Communicate with the Serial device; Register for Events on the Serial device"
参考资料:微软,GitHub