简体   繁体   English

如何使用通用Windows应用程序将串行数据写入COM端口?

[英]How to write serial data to COM ports with Universal Windows Application?

Typically C# applications use System.IO.Ports like so: 通常,C#应用程序使用System.IO.Ports如下所示:

SerialPort port = new SerialPort("COM1"); 
port.Open(); 
port.WriteLine("test");`

But Universal Windows Applications don't support System.IO.Ports so this method cannot be used. 但Universal Windows Applications不支持System.IO.Ports因此无法使用此方法。 Does anyone know how to write serial data through COM ports in a UWA? 有谁知道如何通过UWA中的COM端口写入串行数据?

You can do this with the Windows.Devices.SerialCommunication and Windows.Storage.Streams.DataWriter classes: 您可以使用Windows.Devices.SerialCommunicationWindows.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. 这些类提供了发现此类串行设备, 读取和写入数据以及控制流控制的特定于串行的属性的功能,例如设置波特率,信号状态。

By adding the following capability to Package.appxmanifest : 通过向Package.appxmanifest添加以下功能:

<Capabilities>
  <DeviceCapability Name="serialcommunication">
    <Device Id="any">
      <Function Type="name:serialPort" />
    </Device>
  </DeviceCapability>
</Capabilities>

Then running the following code: 然后运行以下代码:

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 has provided an example of accessing and using a com port, using the class SerialDevice, in a Universal Windows Application, named CustomSerialDeviceAccess. Microsoft提供了一个在通用Windows应用程序中使用SerialDevice类访问和使用com端口的示例,名为CustomSerialDeviceAccess。

Microsoft has posted it on GitHub. 微软已将其发布在GitHub上。 You can find it here: 你可以在这里找到它:

https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CustomSerialDeviceAccess https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CustomSerialDeviceAccess

Microsoft says this about the sample application: 微软说这个示例应用程序:

"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" 在串行设备上注册事件“

References: Microsoft, GitHub 参考文献:Microsoft,GitHub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM