通常、C#アプリケーションは次のSystem.IO.Ports
ように使用します。
SerialPort port = new SerialPort("COM1");
port.Open();
port.WriteLine("test");`
ただし、ユニバーサルWindowsアプリケーションはサポートしSystem.IO.Ports
ていないため、この方法は使用できません。UWAのCOMポートを介してシリアルデータを書き込む方法を知っている人はいますか?
あなたがこれを行うことができます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();
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加