#include int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM1"; hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); if (hCom == INVALID_HANDLE_VALUE) { // Handle the error. printf ("CreateFile failed with error %d.\n", GetLastError()); return (1); } // Build on the current configuration, and skip setting the size // of the input and output buffers with SetupComm. // SetupComm(hCom, 49152, 49152); COMMTIMEOUTS timout; timout.ReadIntervalTimeout = 0; timout.ReadTotalTimeoutMultiplier = 0; timout.ReadTotalTimeoutConstant = 0; timout.WriteTotalTimeoutMultiplier = 0; timout.WriteTotalTimeoutConstant = 0; SetCommTimeouts(hCom, &timout); fSuccess = GetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. printf ("GetCommState failed with error %d.\n", GetLastError()); return (2); } // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit. dcb.BaudRate = CBR_2400; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = TWOSTOPBITS; // one stop bit dcb.fDtrControl = DTR_CONTROL_DISABLE; fSuccess = SetCommState(hCom, &dcb); fSuccess = GetCommState(hCom, &dcb); printf("DWORD0: %d\n", dcb.DCBlength); printf("DWORD1: 0x%08x\n", dcb.BaudRate); printf("DWORD2: 0x%08x\n", *((unsigned long *)(&dcb)+2)); printf("DWORD3: 0x%08x (reserved:2,xon:2)\n", *((unsigned long *)(&dcb)+3)); printf("DWORD4: 0x%08x (xoff:2,bytesize,parity)\n", *((unsigned long *)(&dcb)+4)); printf("DWORD5: 0x%08x (Stopbits,xon,xoff,err)\n", *((unsigned long *)(&dcb)+5)); printf("DWORD6: 0x%08x (eof,ext,reserved:2)\n", *((unsigned long *)(&dcb)+6)); if (!fSuccess) { // Handle the error. printf ("SetCommState failed with error %d.\n", GetLastError()); return (3); } printf ("Serial port %s successfully reconfigured.\n", pcCommPort); char d[10] = "C",junk[10]; COMSTAT stat1; DWORD err, count =0; int i, j, done = 0; PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); ClearCommError(hCom, &err, &stat1); for (j = 0; j < 1000; j++) { PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); WriteFile(hCom, &d, 1, &count, NULL); for(i = 0; i < 1; i++) { ClearCommError(hCom, &err, &stat1); if (stat1.cbInQue) { printf("Found %d chars in %d(%d) trys!\n", stat1.cbInQue, i, j); done = 1; break; } } if (done) { break; } } return (0); }