2016-12-05 24 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

相关问题