windows 自定义波特率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7714060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom baud rate
提问by Alex F
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:
我正在尝试通过虚拟 COM 端口与硬件设备交谈。主机是 PC Windows OS PC。设备以 921600 波特率工作。此代码有效:
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?
SetCommState 因最后一个错误而失败0x57
(参数不正确)。这是否意味着 Windows API 会阻止除预定义值之外的任何波特率?或者,虚拟 COM 端口可能被配置为允许这个波特率?
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.
虚拟 COM 端口是 CameraLink 连接的一部分。我正在与 CameraLink 板供应商交谈。但我需要知道 Windows 串行通信 API 是否支持自定义波特率。
采纳答案by shawty
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.
我刚刚为此快速浏览了 MSDN 文档,这里是关于 DCB 结构中 BaudRate 属性的内容。
BaudRateThe 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
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_602005CBR_120
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)
它还进一步说明有些组合是无效的(特别是在对 8250 串行芯片进行编程时)
RemarksWhen 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.
备注当 DCB 结构用于配置 8250 时,以下限制适用于为 ByteSize 和 StopBits 成员指定的值: 数据位数必须为 5 到 8 位。使用 5 个数据位和 2 个停止位是无效的组合,就像 6、7 或 8 个数据位和 1.5 个停止位一样。
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.
我不知道你处理的硬件,所以如果是这种情况我不能说 100%,我只知道一般的串口编程,但就我个人而言,我的下一步是将波特率设置为我需要的保持原样,尝试块中其他标志的所有不同组合。
The official MSDN page for the DCB structure can be found here:
DCB 结构的官方 MSDN 页面可以在这里找到:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
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:
您也可以找到一些帮助的 BuildCommDCB 函数:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363143(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363143(v=vs.85).aspx