2016-04-03 44 views
3

通常、C#アプリケーションはそうのようなSystem.IO.Portsを使用します。ユニバーサルWindowsアプリケーションでCOMポートにシリアルデータを書き込む方法は?

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

しかし、ユニバーサルのWindowsアプリケーションは、この方法を使用することはできませんのでSystem.IO.Portsをサポートしていません。誰もUWAのCOMポート経由でシリアルデータを書き込む方法を知っていますか?

+0

、少なくとも[サンプル](HTTPSを見てください。 github.io/content/en-US/win10/samples/SerialSample.htm)、DataWriterを見逃すことはできません。 –

答えて

3

あなたがWindows.Devices.SerialCommunicationWindows.Storage.Streams.DataWriterクラスでこれを行うことができます。

クラスは、このようなシリアルデバイスを検出する機能を提供し、は読み取りおよび書き込みデータを、フロー制御のためのシリアル固有のプロパティを制御する、などボーレート、信号状態の設定。 Package.appxmanifestに次の機能を追加することにより

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

次のコード実行:// MS-IOT:

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(); 
} 
関連する問題