Search code examples
c++arduinoserial-port

Why does SetCommState write three bytes to the serial?


I am working with C++ and Arduino communication over serial. I am able to write bytes, but I noticed that when I setup my DCB settings and use SetCommState(m_hComm, &m_dcb), it writes three bytes, specifically 11110000 11110000 11110000 to the Arduino. Why is this happening?

Here is my code that I use to set the DCB:

HANDLE m_hComm = CreateFile("COM4", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
m_dcb.DCBlength = sizeof(m_dcb);
m_dcb.BaudRate = CBR_115200;
m_dcb.ByteSize = 8;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.Parity = NOPARITY;
SetCommState(m_hComm, &m_dcb);

The documentation for SetCommState can be found on Microsoft's website:


Solution

  • I figured out that this happens when baudRate is above ~50,000. I found some other people who have had this issue, and there is no answer for them. One suggested it could be a start byte sent or received at a different baud rate.

    Regardless, I fixed it by setting the baud rate to below 50,000.