Custom baud rate

妖精的绣舞 提交于 2020-01-01 10:08:13

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7714060/custom-baud-rate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!