/* * This is an example file showing how to send data to the serial port. * * It does not include error checking. */ #include #include #include #define DEVICE "COM1" HANDLE openPort(char *device); void closePort(HANDLE serial); int main() { HANDLE serial; DWORD outBytes; serial = openPort(DEVICE); if (serial == INVALID_HANDLE_VALUE) return(1); WriteFile(serial, "Hello World", 11, &outBytes, NULL); closePort(serial); return(0); } /*************************************************************************** * Serial functions **************************************************************************/ /* Open raw serial port using: * 8 data bits * 1 stop bit * 19200 bps * no parity */ HANDLE openPort(char *device) { HANDLE serial; DCB dcb; serial = CreateFile( device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (serial == INVALID_HANDLE_VALUE) return(serial); dcb.DCBlength = sizeof(dcb); if (!GetCommState(serial, &dcb)) { closePort(serial); return(INVALID_HANDLE_VALUE); } dcb.BaudRate = CBR_19200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fRtsControl = RTS_CONTROL_ENABLE; dcb.fOutxCtsFlow = FALSE; dcb.fOutxDsrFlow = FALSE; dcb.fDsrSensitivity = FALSE; dcb.fOutX = FALSE; dcb.fInX = FALSE; dcb.fTXContinueOnXoff = FALSE; dcb.fParity = FALSE; if (!SetCommState(serial, &dcb)) { closePort(serial); return(INVALID_HANDLE_VALUE); } return(serial); } void closePort(HANDLE serial) { CloseHandle(serial); }