Вы можете сделать это с помощью Windows.Devices.SerialCommunication и Windows.Storage.Streams.DataWriter классов:
Классы обеспечивают функциональные возможности, чтобы обнаружить такое последовательное устройство, чтения и записи данных и управления последовательными специфическими свойствами для контроля потока, например, скорость передачи данных, состояние сигнала.
При добавлении следующей возможности для 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();
}
По крайней мере смотреть на [образца] (https: // мс-ВГД. github.io/content/en-US/win10/samples/SerialSample.htm), вы не можете пропустить DataWriter. –