Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // simple232.cpp : Defines the entry point for the console application
- #include "windows.h"
- #include "stdio.h"
- #include "conio.h"
- int main(int argc, char* argv[])
- {
- HANDLE hCom = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0,
- NULL, OPEN_EXISTING, 0, NULL);
- SetupComm(hCom, 128, 128);
- DCB dcb;
- GetCommState(hCom, &dcb); dcb.BaudRate =CBR_9600; dcb.ByteSize =8;
- dcb.Parity =NOPARITY; dcb.StopBits =ONESTOPBIT;
- dcb.fAbortOnError = FALSE;
- SetCommState(hCom, &dcb);
- COMMTIMEOUTS timeouts;
- GetCommTimeouts (hCom, &timeouts);
- timeouts.ReadIntervalTimeout =50;
- timeouts.ReadTotalTimeoutConstant =50; timeouts.ReadTotalTimeoutMultiplier =10;
- timeouts.WriteTotalTimeoutConstant =50; timeouts.WriteTotalTimeoutMultiplier =10;
- SetCommTimeouts (hCom, &timeouts);
- char rBuff[128]; int rLen=1, rPos =0; //only first byte used in example
- char sBuff[] ="3A 30 31 30 33 30 30 30 30 30 30 30 34 46 38 0D 0A"; int sLen=sizeof(sBuff);
- unsigned long iBytesRead, iBytesWritten; printf("program start...(s=send, q=quit)\n");
- while (1)
- { //READ:!!!!!!!!
- if (ReadFile(hCom, rBuff+rPos, rLen, &iBytesRead, NULL)) if (iBytesRead==1)
- printf("%02X", rBuff[rPos]); //todo: ++rPos ...rPos %=buffSize ...
- //WRITE (send) if userKey 'S' .. exit if 'Q'
- if (kbhit()) switch (getch()) { case 's': case 'S':
- printf("\n...sending (%d) bytes", sLen);
- WriteFile(hCom, sBuff, sLen, &iBytesWritten, NULL);
- printf(" ...sent (%d) bytes.\n", iBytesWritten);
- break;
- case 'q': case 'Q': return 0; }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement