SerialDevice.FromIdAsync() 产生一个空串口 [英] SerialDevice.FromIdAsync() yields a null serial port

查看:41
本文介绍了SerialDevice.FromIdAsync() 产生一个空串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试连接到串行端口时,我的代码发生了一些奇怪的事情.使用 Serial Sample 时,一切正常,并且第一次尝试时连接.但是,使用我的代码,串行端口返回空值.

I have something strange happening with my code when trying to connect to a serial port. When using Serial Sample, everything works just fine and it connects on the first try. However, with my code the serial port returns a null.

我找到了一种解决方法,即在 do 循环中嵌套写入串行端口的调用,直到串行端口不再为空并且可以正常工作为止.平均需要 500 次循环才能连接,而且它始终处于连接状态.我知道这很危险,因为如果它始终为空,则会出现无限循环,所以我尝试将 do 循环切换为 for 循环.对于 for 循环,它总是返回 null,即使我迭代它 50,000 次也是如此.有没有其他人遇到过这个问题并找到了更好的解决方案?我正在连接到 Arduino Uno.

I have found a workaround by nested the call to write the serial port in a do loop until the serial port is no longer null and it works. It takes, on average, 500 loops to connect and it has always connected. I know it's dangerous because there is the infinite loop if it's always null, so I tried switching the do loop to a for loop. With a for loop, it always returns back as null, even when I iterate it 50,000 times. Has anyone else encountered this and found a better solution? I'm connecting to an Arduino Uno.

我不只是使用串行示例代码的原因是因为我想使用下拉菜单并使用端口名称而不是设备 ID 名称.我的代码如下.谢谢.

The reason I'm not just using the Serial Sample code is because I want to use a drop down menu and use the port name instead of the device id name. My code is below. Thanks.

    //lists com ports
    private async void listPorts()
    {
        try
        {
            string aqs = SerialDevice.GetDeviceSelector();
            var dlist = await DeviceInformation.FindAllAsync(aqs);
            status.Text = "Select a device and connect";

            for (int i = 0; i < dlist.Count; i++)
            {
                var port = await SerialDevice.FromIdAsync(dlist[0].Id);
                ConnectDevices.Items.Add(port.PortName);
            }

            comConnect.IsEnabled = true;
            ConnectDevices.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            status.Text = ex.Message;
        }
    }

    //connects to the selected com port
    private async void comConnect_Click(object sender, RoutedEventArgs e)
    {
        _key = 0;
        status2.Text = "";
        string aqs = SerialDevice.GetDeviceSelector(ConnectDevices.SelectedItem.ToString());
        var dlist = await DeviceInformation.FindAllAsync(aqs);
        if (dlist.Count <= 0)
        {
            status.Text = "No devices found.";
            return;
        }
        try
        {
            do
            {
                serialPort = await SerialDevice.FromIdAsync(dlist[0].Id);
                status.Text = "Connecting to serial port...";
            } while (serialPort == null);

            // Configure serial settings
            serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.BaudRate = 38400;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
            serialPort.DataBits = 8;
            serialPort.Handshake = SerialHandshake.None;
            status.Text = "Serial port configured successfully.";

我已将以下内容添加到 package.appmanifest

I have added the following to package.appmanifest

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

推荐答案

在我的项目中,在 package.appmanifest 添加以下代码后,串口工作正常.

In my project, the serial port work correctly after I add the following code to package.appmanifest.

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

串口变量应该是serialPort".

The serial port variable should be "serialPort".

这篇关于SerialDevice.FromIdAsync() 产生一个空串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆