2016-12-05 64 views
1

我想爲Raspberry Pi創建一個無頭應用程序,它涉及從串口讀取數據。UWP無串連應用程序上的串行連接

來自Microsoft的示例應用程序正常工作,但它有一個用戶界面。

在創建無頭的應用程序,我把所有相關的部分超過如下:

var aqs = SerialDevice.GetDeviceSelector(); 

var dis = await DeviceInformation.FindAllAsync(aqs); 

foreach (var t in dis) 
{ 
    if (t.Id.Contains("FTDI")) 
    { 
     listOfDevices.Add(t); 

    } 

} 

if (listOfDevices.Count == 1) 
{ 
    DeviceInformation entry = listOfDevices[0]; 

    try 
    { 
     serialPort = await SerialDevice.FromIdAsync(entry.Id); 
     serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); 
     serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); 
     serialPort.BaudRate = 9600; 
     serialPort.Parity = SerialParity.None; 
     serialPort.StopBits = SerialStopBitCount.One; 
     serialPort.DataBits = 8; 
     serialPort.Handshake = SerialHandshake.None; 

     ... 

有一個USB-FTDI電纜,其中ID中包含 「FTDI」 如下:

serialPort = await SerialDevice.FromIdAsync(entry.Id); 

程序在程序實例消失並忽略我的斷點之前到達此行。

任何想法?

回答

0

我使用您的代碼並在Package.appxmanifest中添加以下行。

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

它適用於我。

更新:

根據您的項目(您發送給我),因爲你的ListAvailablePorts()是一個異步操作,你需要use the GetDeferral method to get a background task deferral to keep the host process from being suspended or terminated before the asynchronous operation complete。您可以編輯代碼:

BackgroundTaskDeferral _deferral = null; 
    public async void Run(IBackgroundTaskInstance taskInstance) 
    { 
     _deferral = taskInstance.GetDeferral(); 
     // 
     // TODO: Insert code to start one or more asynchronous methods 
     // 
     try 
     { 
      //var u = new USART(); 
      //await u.Initialize(115200, SerialParity.None, SerialStopBitCount.One, 8); 
      listOfDevices = new ObservableCollection<DeviceInformation>(); 
      ListAvailablePorts(); 
     } 

     ... 
    } 

並調用_deferral.Complete()當異步操作完成。

+0

嗨麗塔,我不得不在已經devicecapabilities: \t <能力> \t \t <在DeviceCapability NAME = 「serialcommunication」> \t \t \t <設備Id = 「任何」> \t \t \t \t <功能類型= 「名稱:的SerialPort」/> \t \t \t \t \t \t 但沒有結果。 – Axel

+0

@Axel你介意顯示完整的代碼嗎? –

+0

我怎樣才能發送給你? – Axel