简体   繁体   English

UWP 中串行设备通信的问题

[英]Problems with serial device communication in UWP

I have a peculiar problem.我有一个特殊的问题。

I am trying to communicate with a peripheral unit that requires serial communication in a UWP project.我正在尝试与需要在 UWP 项目中进行串行通信的外围设备进行通信。 I am using Windows.Devices.SerialCommunication .我正在使用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.一个用于打开端口,另一个用于向外设发送消息。

One handler is:一个处理程序是:

    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.当我停止程序时,灯会重新亮起。

I made a .NET Framework app that works perfectly.我制作了一个完美运行的 .NET Framework 应用程序。 It is fully functional.它功能齐全。 I used System.IO.Ports there.我在那里使用了System.IO.Ports I noticed something:我注意到一件事:

If I extract and run only this part of the code in .NET Framework app:如果我在 .NET Framework 应用程序中仅提取并运行这部分代码:

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();

Nothing more.而已。

And run the UWP app again, the port opens perfectly, the lamp is red, and the device responds to messages.并再次运行UWP应用,端口完美打开,灯为红色,设备响应消息。 I can switch off the device, and initialize it from the UWP app as many times as I want to.我可以关闭设备,并根据需要多次从 UWP 应用程序对其进行初始化。 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).当我重新启动计算机时,我无法再次从 UWP 应用程序初始化设备(直到我从 .NET Framework 应用程序运行上述代码块)。

If you want to know, the peripheral is Bill to Bill unit made by Suzo Happ.如果您想知道,外围设备是 Suzo Happ 制造的 Bill to Bill 设备。

I didn't make any mistakes regarding property initialization in UWP.关于 UWP 中的属性初始化,我没有犯任何错误。

I think this is the same issue I'm having.我认为这与我遇到的问题相同。 I repost here a description of the cause and a possible solution:我在这里重新发布原因描述和可能的解决方案:

The UWP SerialDevice class currently only allows you to set "ReadTimeout", which under the hood, sets the "ReadIntervalTimeout" of the actual serial device ( https://learn.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_commtimeouts ). UWP SerialDevice 类目前只允许您设置“ReadTimeout”,它在后台设置实际串行设备的“ReadIntervalTimeout”( https://learn.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.还有两个其他超时值会显着影响读取操作行为:1) ReadTotalTimeoutMultiplier 和 2) ReadTotalTimeoutConstant。

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

The overall effect of this is that your UWP application's serial device read behavior is undefined and cannot reliably be used.这样做的总体效果是您的 UWP 应用程序的串行设备读取行为未定义且无法可靠地使用。 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 应用程序无法控制此行为,不同串口的行为会有所不同,甚至可能每次执行 UWP 应用程序时都会有所不同。

The UWP SerialDevice class either needs to UWP SerialDevice 类需要

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. 1) 允许用户设置这两个其他读取超时值(首选),或 2) 在打开串行设备时将这两个其他超时值初始化为已知值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM