使用SerialPort类的C#串行端口和DCB [英] C# Serial Port and DCB using SerialPort Class

查看:180
本文介绍了使用SerialPort类的C#串行端口和DCB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,需要在Windows XP,Vista或Windows 7中禁用对串行端口或Com端口的奇偶校验检查.
我目前正在使用.NET Framework SerialPort类
我问的原因是因为更改奇偶校验而导致成帧错误
相当快,并且看起来好像是由于com端口正在接收字节而引起的.

注意:我必须模拟9个数据位,而SerialPort类的唯一方法是使用标记或空格奇偶校验. 没有,偶数或奇数奇偶校验将不起作用.

我认为使用DCB将fParity更改为false或0将是解决我的问题的最佳方法.

我知道我的程序基本上可以正常工作,而不是在串行端口监视程序中看到的奇偶校验错误.

示例代码段.

I have an application that I need to disable parity checking for Serial or Com Ports in Windows XP, Vista, or Windows 7.
I am currently using The .NET Framework SerialPort class
the reason I ask is because I get framing errors because I am changing the parity
fairly quickly and looks to be occurring as bytes are being received by the com port.

NOTE: I have to emulate 9 data bits and the only way to do so with the SerialPort Class is to use mark or space parity.
None, even or odd parity will not work.

I think that using DCB to change fParity to false or 0 would be the best way to solve my issue.

I know my program basically works other than the parity errors that I see with a serial port monitoring program.

Example code segments.

Private Poll1()
{
SerialPort1.Parity=Parity.Mark; // turn on wake up bit
SerialPort1.Write(GenPoll,0,2); // , sends the general poll bytes with wake up bit set
SerialPort1.Parity=Parity.Space; // turn off wake up bit 
int numbytes1 = SerialPort1.BytestoRead; // get the number of bytes to read
Byte[] readbuffer1 = new Byte[numbytes1]; creates a Byte array
SerialPort1.Read(readbuffer1,numbytes1); // read the data.
.......
// processing of received data occurs here. I am thinking about creating another sub // method or procedure to process the received data. 
} // end of Poll



发送
的示例 命令到机器



example of sending a
command to the machine

private SendCommand1(Byte[] Command)
{
  SerialPort1.Parity=Parity.Mark;
  SerialPort1.Write(Command,0,1); Address byte
  SerialPort1.Parity=Parity.Space;
  SerialPort1.Write(Command,1,Command.Length-1); command to the machine.
}



[edit]添加了代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

在任何串行通讯中都使用奇偶校验位 1 -通常将其设置为奇数",偶数"或无",然后保留该值.您遇到奇偶错误确实会确认您的方法是错误的.

相反,请检查您的数据位-您是否正在使用7位数据,应该为8位?一些系统以这种方式工作,并使用最高位指示命令/数据.使用该方法可能会获得更好的结果.


1 非常不寻常"是指我从未听说过这样做的系统,但是自80年代末以来我一直在进行串行通信,所以我可能会错过它.
It is very unusual to play with the parity bit in any serial comms1 - normally it is set as Odd, Even or None and left at that. That you are getting parity errors does tend to confirm that your approach is wrong.

Instead, check your databits - are you using seven bit data and it should be eight? Some systems work this way, and use the top bit to indicate command / data. You may have better results using that.


1 By "very unusual" I mean I have never once heard of a system doing this, but I have only been doing serial comms since the late eighties so I might have missed it...


这篇关于使用SerialPort类的C#串行端口和DCB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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