Custom baud rate

Alex F picture Alex F · Oct 10, 2011 · Viewed 10k times · Source

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.

Answer

shawty picture shawty · Oct 11, 2011

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