UWP 中的串行设备通信问题 [英] Problems with serial device communication in UWP

查看:37
本文介绍了UWP 中的串行设备通信问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特殊的问题.

我正在尝试与需要在 UWP 项目中进行串行通信的外围设备进行通信.我正在使用 Windows.Devices.SerialCommunication.

I am trying to communicate with a peripheral unit that requires serial communication in a UWP project. I am using Windows.Devices.SerialCommunication.

出于演示的目的,我创建了一个新页面,其中包含两个按钮和两个不同的点击处理程序.一个用于打开端口,另一个用于向外围设备发送消息.

For purpose of demonstration, I made a new page that has two buttons, with two different click handlers. One for opening the port, and the other for sending messages to the peripheral.

一个处理程序是:

    SerialDevice device;
    
    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
         string selector = SerialDevice.GetDeviceSelector("COM7");
         DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
             if (devices.Any())
             {
                 DeviceInformation deviceInfo = devices.First();
                 device = await SerialDevice.FromIdAsync(deviceInfo.Id);
                 //*********************
                 device.BaudRate = 9600;
                 device.DataBits = 8;
                 device.Parity = SerialParity.None;
                 device.StopBits = SerialStopBitCount.One;
                 device.ReadTimeout = device.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                 device.Handshake = SerialHandshake.None;
             }
             _dataReader = new DataReader(device.InputStream);
             _dataWriter = new DataWriter(device.OutputStream);
    }

当我启用电源时,外围设备上有一个红灯.当//*********上面的那行代码被执行时,灯就关闭了.外围设备不会响应任何消息.当我停止程序时,灯又亮了.

Peripheral has a red light on it when I enable the power supply. When the line above //********* is executed, the light is switched off. The peripheral doesn't respond to any messages then. When I stop the program, the light switches back on.

我制作了一个运行良好的 .NET Framework 应用程序.它功能齐全.我在那里使用了 System.IO.Ports.我注意到一件事:

I made a .NET Framework app that works perfectly. It is fully functional. I used System.IO.Ports there. I noticed something:

如果我在 .NET Framework 应用程序中仅提取并运行这部分代码:

If I extract and run only this part of the code in .NET Framework app:

SerialPort comPort = new SerialPort();            
_ComPort.PortName = PortName;
_ComPort.BaudRate = BaudRate;
_ComPort.DataBits = 8;
_ComPort.Parity = Parity.None;
_ComPort.StopBits = StopBits.One;
_ComPort.DataReceived += new SerialDataReceivedEventHandler(_ComPort_DataReceived);
_ComPort.Open();

仅此而已.

并再次运行UWP应用,端口完美打开,灯为红色,设备响应消息.我可以关闭设备,并根据需要多次从 UWP 应用程序初始化它.当我重新启动计算机时,我无法再次从 UWP 应用程序初始化设备,(直到我从 .NET Framework 应用程序运行上述代码块).

And run the UWP app again, the port opens perfectly, the lamp is red, and the device responds to messages. I can switch off the device, and initialize it from the UWP app as many times as I want to. When I restart my computer, I can't initialize the device from the UWP app again, (until I run the said block of code from .NET Framework app).

如果你想知道,外围设备是 Suzo Happ 制造的 Bill to Bill 单元.

If you want to know, the peripheral is Bill to Bill unit made by Suzo Happ.

我在 UWP 中的属性初始化方面没有犯任何错误.

I didn't make any mistakes regarding property initialization in UWP.

推荐答案

我认为这与我遇到的问题相同.我在这里重新发布原因描述和可能的解决方案:

I think this is the same issue I'm having. I repost here a description of the cause and a possible solution:

UWP SerialDevice 类目前只允许您设置ReadTimeout",它在后台设置ReadIntervalTimeout".的实际串行设备(https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_commtimeouts).还有两个超时值会显着影响读取操作行为:1) ReadTotalTimeoutMultiplier 和 2) ReadTotalTimeoutConstant.

The UWP SerialDevice class currently only allows you to set "ReadTimeout", which under the hood, sets the "ReadIntervalTimeout" of the actual serial device (https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_commtimeouts). There are two other timeout values which dramatically affect the read operations behavior: 1) ReadTotalTimeoutMultiplier and 2) ReadTotalTimeoutConstant.

UWP SerialDevice 类不允许用户设置这两个其他读取超时值,更糟糕的是,UWP SerialDevice 类在打开串行设备时不会将这两个其他超时值设置为已知值.这意味着另外两个超时值将是串行驱动程序使用的任何默认值,或者更糟的是,某些串行端口应用程序碰巧将这两个值设置为另一个应用程序上次执行时的任何值.

The UWP SerialDevice class does not allow the user to set these two other read timeout values, and even worse, the UWP SerialDevice class does not set these two other timeout values to known values when the serial device is opened. This means that the two other timeout values will be whatever default value the serial driver uses, or worse still, whatever value some serial port application happened to set these two values to be when the other application was last executed.

这样做的总体效果是您的 UWP 应用程序的串行设备读取行为未定义且无法可靠使用.例如,如果这两个超时值碰巧设置为一种方式,则读取操作可能会永远阻塞等待要读取的第一个数据字节,但如果其他超时值碰巧设置为不同的方式,则读取操作可能会立即返回,根本不会读取任何数据.目前,UWP 应用程序无法控制这种行为,并且不同的串口行为会有所不同,甚至可能在每次执行 UWP 应用程序时都不同.

The overall effect of this is that your UWP application's serial device read behavior is undefined and cannot reliably be used. For example, if these two other timeout values happen to be set one way, then a read operation may block forever waiting on the first byte of data to be read, but if the other timeout values happen to be set a different way, then the read operation may return immediately, with no data read at all. Currently, a UWP application cannot control this behavior, and the behavior will be different across different serial ports, and even perhaps different every time the UWP application is executed.

UWP SerialDevice 类需要

The UWP SerialDevice class either needs to

1)允许用户设置这两个其他读取超时值(首选),或者2)当串口设备打开时,将另外两个超时值初始化为已知值.

1)Allow the user to set these two other read timeout values (preferred), OR 2)Initialize these two other timeout values to known values when the serial device is opened.

这篇关于UWP 中的串行设备通信问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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