TAG:
#include <windows.h> #include <stdio.h> int main() { HANDLE h1; unsigned char cmd1; unsigned long write; h1 = CreateFile("com1", GENERIC_READ|GENERIC_WRITE, 0,NULL, OPEN_EXISTING,0,NULL); if(h1 == INVALID_HANDLE_value) { printf("Failed to open port1\\n"); return FALSE; } /////setup comm parameter COMMTIMEOUTS cto = { 2, 1, 1, 0, 0 }; if(!SetCommTimeouts(h1,&cto)) { printf("SetCommTimeouts failed"); return FALSE; } { DCB dcb; memset(&dcb,0,sizeof(dcb)); GetCommState( h1, &dcb); dcb.DCBlength = sizeof(dcb); dcb.BaudRate = CBR_38400 ; dcb.fBinary = TRUE; dcb.fParity = TRUE; dcb.fOutxCtsFlow = 0; dcb.Parity = ODDPARITY; dcb.StopBits = ONESTOPBIT; dcb.ByteSize = 8; if(!SetCommState(h1,&dcb)) { printf("SetCommState failed\\n"); return FALSE; } } cmd1 = 0x30; WriteFile(h1,&cmd1,1,&write,NULL); cmd1 = 0x11; WriteFile(h1,&cmd1,1,&write,NULL); cmd1 = 0x41; WriteFile(h1,&cmd1,1,&write,NULL); for(;;) { do { ReadFile (h1,&cmd1,1,&write,NULL); }while(write==0); printf("x ",cmd1); } /* do { ReadFile (h1,&cmd1,1,&write,NULL); }while(write==0); printf("x ",cmd1); do { ReadFile (h1,&cmd1,1,&write,NULL); }while(write==0); printf("x ",cmd1); */ cmd1 = 0xaa; while(1) WriteFile(h1,&cmd1,1,&write,NULL); /* cmd1 = 0x12; WriteFile(h1,&cmd1,1,&write,NULL); cmd1 = 0x55; WriteFile(h1,&cmd1,1,&write,NULL); cmd1 = 0x61; WriteFile(h1,&cmd1,1,&write,NULL); cmd1 = 0x61; WriteFile(h1,&cmd1,1,&write,NULL); cmd1 = 0xcc; WriteFile(h1,&cmd1,1,&write,NULL); */ return TRUE; } (iwgh) |