#include #include #include #include #include COMMTIMEOUTS gTimeoutsDefault = { 0x01, 0, 0, 0, 0 }; #define AMOUNT_TO_READ 512 #define EVENTFLAGS_DEFAULT EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING | EV_RLSD #define FLAGCHAR_DEFAULT '\n' #define ASCII_XON 0x11 #define ASCII_XOFF 0x13 #define NUM_READSTAT_HANDLES 0x04 #define STATUS_CHECK_TIMEOUT 500 #define MAX_READ_BUFFER 2048 #define SHOWTIMEOUTS FALSE #define STRINGPAD 2 HANDLE hCommPort; int main(int argc, char *argv[]) { OVERLAPPED osWrite = {0}; char gszPort[1000]; DCB dcb = {0}; unsigned char lpBuf[1024]; DWORD dwWrite; HANDLE hThread = NULL; DWORD dwRead; BOOL bReturnCode; int i; int x; srand( (unsigned)time( NULL ) ); osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osWrite.hEvent == NULL) { printf("CreateEvent (overlapped write hEvent)"); } strcpy(gszPort,"COM2:"); printf("Openiong COM port =%s\n",gszPort); hCommPort = CreateFile( gszPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, // FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, FILE_ATTRIBUTE_NORMAL, 0); if (hCommPort == INVALID_HANDLE_VALUE) { printf("CreateFile"); return NULL; } printf("Setting up the COM Port\n"); dcb.DCBlength = sizeof(dcb); if (!GetCommState(hCommPort, &dcb)) { printf("GetCommState"); return(-1); } dcb.BaudRate = CBR_19200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if (EVENTFLAGS_DEFAULT & EV_RXFLAG) { dcb.EvtChar = FLAGCHAR_DEFAULT; } else { dcb.EvtChar = '\0'; } dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fOutxCtsFlow = FALSE; dcb.fOutxDsrFlow = FALSE; dcb.fDsrSensitivity = FALSE; dcb.fOutX = FALSE; dcb.fInX = FALSE; dcb.fTXContinueOnXoff = FALSE; dcb.XonChar = ASCII_XON; dcb.XoffChar = ASCII_XOFF; dcb.XonLim = 0; dcb.XoffLim = 0; dcb.fParity = TRUE; if (!SetCommState(hCommPort, &dcb)) { printf("SetCommState"); } if (!SetCommTimeouts(hCommPort, &(gTimeoutsDefault))) { printf("SetCommTimeouts"); } x=0; while(TRUE) { Sleep(80); lpBuf[0] = 2; lpBuf[1] = rand()%25; lpBuf[2] = rand()%25; lpBuf[3] = rand()%250; lpBuf[4] = rand()%250; lpBuf[5] = 0; lpBuf[6] = 0xFF; lpBuf[7] = 0xFF; for(i=0;i<5;i++) { lpBuf[5] += lpBuf[i]; } x++; if(0 == (x%80)) { lpBuf[5] = 0x20; } WriteFile(hCommPort, lpBuf, 8, &dwWrite, NULL); if(0 == (x%100)) { lpBuf[0] = 6; WriteFile(hCommPort, lpBuf, 1, &dwWrite, NULL); } else { if(0 == (x%600)) { lpBuf[0] = 1; lpBuf[1] = 1; lpBuf[2] = 0xFF; lpBuf[3] = 0xFF; WriteFile(hCommPort, lpBuf, 4, &dwWrite, NULL); } } } return(0); }