串口中并不总是检测到奇偶校验错误
Parity error not always being detected in serial port
我在 netcore 中有一个串行端口 class - 它只是监听端口并尝试检测奇偶校验错误。奇偶校验设置为 space,并且所有传入字节都使用 parity=mark 发送,这会导致奇偶校验错误。
不幸的是,这种情况只被检测到大约 1/3 的次数。我需要这种检测,因为这是协议声明消息开头的方式。
每 1 秒发送一次字节(80 和 81),因此缓冲区应始终有 1 个字节。
我做错了什么?
// 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)'[=10=]' ;
_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) { }
}
}
}
控制台正在输出:
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
我希望每个字节后 "Parity error"。
通过将读取逻辑移动到 SerialError 处理程序中获得了更好的结果。
删除了 Read() 函数和 readThread 线程。
我遇到了完全相同的问题。我只得到 30-50% 的标记字节被标记为奇偶校验错误。就我而言,问题出在我使用的 USB RS-232 串行电缆上。当我通过 DB-9 电缆切换到真正的 RS-232 时,串行错误正确地提高了 100%。
我在 netcore 中有一个串行端口 class - 它只是监听端口并尝试检测奇偶校验错误。奇偶校验设置为 space,并且所有传入字节都使用 parity=mark 发送,这会导致奇偶校验错误。 不幸的是,这种情况只被检测到大约 1/3 的次数。我需要这种检测,因为这是协议声明消息开头的方式。 每 1 秒发送一次字节(80 和 81),因此缓冲区应始终有 1 个字节。
我做错了什么?
// 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)'[=10=]' ;
_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) { }
}
}
}
控制台正在输出:
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
我希望每个字节后 "Parity error"。
通过将读取逻辑移动到 SerialError 处理程序中获得了更好的结果。 删除了 Read() 函数和 readThread 线程。
我遇到了完全相同的问题。我只得到 30-50% 的标记字节被标记为奇偶校验错误。就我而言,问题出在我使用的 USB RS-232 串行电缆上。当我通过 DB-9 电缆切换到真正的 RS-232 时,串行错误正确地提高了 100%。