简体   繁体   English

在串行端口中并不总是检测到奇偶校验错误

[英]Parity error not always being detected in serial port

I have a serial port class in netcore - it just listens to the port and tries to detect parity errors.我在 netcore 中有一个串行端口类 - 它只是侦听端口并尝试检测奇偶校验错误。 Parity is set to space, and all incoming bytes are being sent with parity=mark which should result in a parity error.奇偶校验设置为空间,并且所有传入字节都使用 parity=mark 发送,这将导致奇偶校验错误。 Unfortunately, this is being detected only about a 1/3 of times.不幸的是,这仅被检测到大约 1/3 次。 I need this detection because this is how the protocol states the beginning of a message.我需要这种检测,因为这是协议声明消息开头的方式。 bytes (80 and 81) are being sent every 1 second so the buffer should always have 1 byte.字节(80 和 81)每 1 秒发送一次,因此缓冲区应始终有 1 个字节。

What am I doing wrong?我究竟做错了什么?

// Use this code inside a project created with the Visual C# > Windows Desktop > Console Application template.
// Replace the code in Program.cs with this code.

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;
using SASComms;

public class PortChat
{
    static bool _continue;
    static SASSerialPort _serialPort;
    static object msgsLock = new object();
    static Queue<byte[]> msgs = new Queue<byte[]>();
    static Queue<byte> receiveQeue = new Queue<byte>();
    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new MachineSerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = "COM2";
        _serialPort.BaudRate = 19200;
        _serialPort.ParityReplace = (byte)'\0' ;
        _serialPort.ReadBufferSize = 128;
        _serialPort.Parity = Parity.Space;
        _serialPort.DataBits = 8;
        _serialPort.StopBits = StopBits.One;
        _serialPort.Handshake = Handshake.None;
        // Set the read/write timeouts
        _serialPort.ReadTimeout = 5;
        _serialPort.WriteTimeout = 5;
        _serialPort.ErrorReceived +=  new SerialErrorReceivedEventHandler(sp_SerialErrorReceivedEventHandler);

        _serialPort.Open();
        _continue = true;
        readThread.Start();
    }

    public static void sp_SerialErrorReceivedEventHandler(Object sender, SerialErrorReceivedEventArgs e)
    {
        if (e.EventType == SerialError.RXParity)
        {
            Console.WriteLine("Parity error");
        }
    }

    public static void Read()
    {
        while (_continue)
        {
            try
            {

                    while (_serialPort.BytesToRead > 0)
                    {
                     receiveQeue.Enqueue((byte)_serialPort.ReadByte());
                }

                if (receiveQeue.Count > 0)
                {
                    foreach (byte r in receiveQeue)
                    {
                        Console.Write(r.ToString("X")+" " );
                        Console.WriteLine();
                        }
                    }
                }
                receiveQeue.Clear();
            }
            catch (TimeoutException) { }
        }
    }
}

The console is outputing this:控制台输出如下:

80
Parity error
81
80
Parity error
81
Parity error
80
Parity error
81
80
Parity error
81
80
81
80
81
Parity error
80
Parity error
81
80
Parity error

And I'm expecting "Parity error" after each byte.我期待每个字节后出现“奇偶校验错误”。

Achieved much better result by moving the read logic into the SerialError handler.通过将读取逻辑移动到 SerialError 处理程序中获得了更好的结果。 Removed the Read() function and the readThread thread.删除了 Read() 函数和 readThread 线程。

I had the exact same issue.我有完全相同的问题。 I was getting only 30-50% of Mark bytes being flagged with parity errors.我只有 30-50% 的 Mark 字节被标记为奇偶校验错误。 In my case the problem was the USB RS-232 Serial cable I was using.就我而言,问题出在我使用的 USB RS-232 串行电缆上。 When I switched to true RS-232 over a DB-9 cable the serial errors were raised 100% correctly.当我通过 DB-9 电缆切换到真正的 RS-232 时,串行错误 100% 正确增加。

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

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