自定义波特率 [英] Custom baud rate

查看:121
本文介绍了自定义波特率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过虚拟 COM 端口与硬件设备通信.主机是 PC Windows OS PC.设备以 921600 波特率工作.此代码有效:

<前>DCB dcb;...dcb.BaudRate = CBR_115200;SetCommState(hPort, &dcb);

一旦我改变了波特率:

<前>dcb.BaudRate = 921600;

SetCommState 失败,最后一个错误 0x57(参数不正确).这是否意味着 Windows API 会阻止除预定义值之外的任何波特率?或者,虚拟 COM 端口可能被配置为允许这个波特率?

虚拟 COM 端口是 CameraLink 连接的一部分.我正在与 CameraLink 板供应商交谈.但我需要知道 Windows 串行通信 API 是否支持自定义波特率.

解决方案

我刚刚为此快速浏览了 MSDN 文档,这里是关于 DCB 结构中的 BaudRate 属性的说明.

BaudRate 通信设备运行的波特率.该成员可以是实际的波特率值,也可以是以下索引之一.CBR_110.CBR_300、CBR_600、CBR_1200、CBR_2400、CBR_4800、CBR_9600、CBR_14400、CBR_19200、CBR_38400、CBR_57600、CBR_115200、CBR_500>,CBR_120p120

所以理论上至少你设置你请求的串口速度应该没有问题.

还进一步说明有些组合是无效的(特别是在对 8250 串行芯片进行编程时)

备注当 DCB 结构用于配置 8250 时,以下限制适用于为 ByteSize 和 StopBits 成员指定的值:数据位数必须为 5 到 8 位.使用 5 个数据位和 2 个停止位是无效的组合,6、7 或 8 个数据位和 1.5 个停止位也是如此.

这让我想知道您遇到的问题是否是某些组合是导致问题的原因,而不仅仅是设置波特率.

也许您的波特率没问题,但是通过选择该波特率,您会使停止位的数量或奇偶校验长度无效,当波特率设置回标准设置时,它们会再次变为有效.

我不知道你处理的硬件,所以如果是这种情况我不能说 100%,我只知道一般的串口编程,但就我个人而言,我的下一步是将波特率设置为什么然后我需要保持原样,尝试块中其他标志的所有不同组合.

DCB 结构的官方 MSDN 页面可以在这里找到:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx

你也可以找到一些帮助的 BuildCommDCB 函数:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363143(v=vs.85).aspx

I am trying to talk with hardware device through virtual COM port. Host computer is PC Windows OS PC. Device is working with 921600 baud rate. This code works:

DCB dcb;
...
dcb.BaudRate =  CBR_115200;
SetCommState(hPort, &dcb);

Once I change baud rate:

dcb.BaudRate =  921600;

SetCommState fails with last error 0x57 (parameter is incorrect). Does this mean that Windows API prevents any baud rate except predefined values? Or maybe, virtual COM port may be configured to allow this baud rate?

Virtual COM port is part of CameraLink connection. I am talking with CameraLink board vendor. But I need to know whether Windows serial communications API support custom baud rates.

解决方案

Iv'e just had a quick trip to the MSDN documents for this, and here's what is said about the BaudRate property in the DCB struct.

BaudRate The baud rate at which the communications device operates. This member can be an actual baud rate value, or one of the following indexes. CBR_110. CBR_300, CBR_600, CBR_1200, CBR_2400, CBR_4800, CBR_9600, CBR_14400, CBR_19200, CBR_38400, CBR_57600, CBR_115200, CBR_128000, CBR_256000

So in theory at least you should have no problem setting the serial port speed your requesting.

It also states further down that there are some combinations that are invalid (Specifically when programming the 8250 serial chip)

Remarks When a DCB structure is used to configure the 8250, the following restrictions apply to the values specified for the ByteSize and StopBits members: The number of data bits must be 5 to 8 bits. The use of 5 data bits with 2 stop bits is an invalid combination, as is 6, 7, or 8 data bits with 1.5 stop bits.

This makes me wonder if the issue you have is that certain combinations are what's causing things, rather than just setting the baud-rate for example.

Maybe your baudrate is fine, but by selecting that baudrate your invalidating the number of stop bits, or the parity length, which when the baudrate is set back to a standard setting then become valid again.

I don't know the hardware your dealing with so I can't say 100% if this is the case, I only know serial port programming in general, but personally, my next step would be to set the baudrate to what I need then leaving that as is, try all the different combinations of other flags in the block.

The official MSDN page for the DCB structure can be found here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx

You may also find the BuildCommDCB function of some help too:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363143(v=vs.85).aspx

这篇关于自定义波特率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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