使用 Windows.Devices.SerialCommunication 未收到奇偶校验错误

Not receiving parity error using Windows.Devices.SerialCommunication

我目前正在为使用串行通信的 raspberry pi 开发应用程序。该应用程序是一个使用 Windows.Devices.SerialCommunication 库的 Windows 通用应用程序。它将接收的数据将在特定条件下在奇偶校验位打开和关闭之间切换。到目前为止,我已经订阅了 ErrorReceived 事件,奇偶校验设置为 Space。即使我正在接收通过 (Mark) 上的奇偶校验位传输的数据,ErrorReceived 事件也没有触发。只是为了确认,我制作了一个小型控制台应用程序,该应用程序利用 NET2.0 System.IO.Ports 库 class 并订阅了相同的 ErrorReceived 事件,并且我始终收到错误事件。这是我使用的硬件的限制,还是我可能做错了什么?

示例代码:

        private async void Connect()
    {
        if (Connected) { return; }

        try
        {
            string Selector = SerialDevice.GetDeviceSelector();
            var DeviceInfoCollection = await DeviceInformation.FindAllAsync(Selector);
            if (DeviceInfoCollection.Count == 0) { return; }
            DeviceInformation DeviceInfo = DeviceInfoCollection[0];

            Port = await SerialDevice.FromIdAsync(DeviceInfo.Id);
            if (Port == null)
            {
                Debug.WriteLine("Port failed to create: " + DeviceInfo.Id);
                Connected = false;
            }

            Port.BaudRate = 19200;
            Port.Parity = SerialParity.Space;
            Port.StopBits = SerialStopBitCount.One;
            Port.DataBits = 8;
            Port.ErrorReceived += Port_ErrorReceived;
            Port.PinChanged += Port_PinChanged;

            Connected = true;
            Listen();
        }
        catch (Exception Exc)
        {
            Debug.WriteLine("Exception encountered while trying to connect to serial port: " + Exc.Message);
            Connected = false;
        }
    }

    private async void Listen()
    {
        try
        {
            if (Port != null)
            {
                Reader = new DataReader(Port.InputStream);
                while (true)
                {
                    await ReadAsync();
                }
            }
        }
        catch (TaskCanceledException Exc)
        {
            CloseDevice();
        }
        catch (Exception Exc)
        {
            Debug.WriteLine("Error encountered while reading from the port: " + Exc.Message);
        }
        finally
        {
            if (Reader != null)
            {
                Reader.DetachStream();
                Reader.Dispose();
                Reader = null;
            }

            Connected = false;
        }
    }
    private async Task ReadAsync()
    {
        Token.ThrowIfCancellationRequested();
        using (var ChildTokenSource = CancellationTokenSource.CreateLinkedTokenSource(Token))
        {
            Reader.InputStreamOptions = InputStreamOptions.Partial;
            Task<UInt32> LoadAsyncTask = Reader.LoadAsync(1).AsTask(ChildTokenSource.Token);
            uint BytesRead = await LoadAsyncTask;
            if (BytesRead > 0)
            {
                byte B = Reader.ReadByte(); // Should be getting parity error here
                Debug.WriteLine(B.ToString("X2")); 
                DataQueue.Post(B);
            }
        }
    }

原来是硬件问题。我使用的 serial hat 没有检测到奇偶校验错误。当 运行 在不同的平台上使用不同的串行卡时,相同的代码选择奇偶校验错误就好了。