March 17, 2005

You always should initialize the whole DCB

It is a bad practise to only get the last state of the DCB, change only the baudrate and then set it as the DCB for your communication app. Here is the code which most apps use:

  GetCommState(hCom, &dcb);
  dcb.BaudRate = CBR_57600;     // set the baud rate
  dcb.ByteSize = 8;             // data size, xmit, and rcv
  dcb.Parity = NOPARITY;        // no parity bit
  dcb.StopBits = ONESTOPBIT;    // one stop bit
  SetCommState(hCom, &dcb);

The problem is also, that this piece of code is from the offical MS website: Configuring a Communications Resource

But using this “technic” can be very problematic if an other app also has used this port with very different setting (for example it may have set the ‘fNULL’ meber to TRUE). In this case, the serial communication might behave in a very strange way… Also the other app has enabled some special kind of hardware-handshake, or has enabled XOn/Off transmission control…

Therefor: please do not use “GetCommState”!!! Instead initialize the whole DCB structure with the correct settings for your application!


Posted 2 years, 10 months ago on March 17, 2005
The trackback url for this post is http://blog.kalmbachnet.de/bblog/trackback.php/32/

Comments have now been turned off for this post