Advertisement
Guest User

Untitled

a guest
Feb 7th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. // simple232.cpp : Defines the entry point for the console application
  2. #include "windows.h"
  3. #include "stdio.h"
  4. #include "conio.h"
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.    HANDLE hCom = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0,
  9.                             NULL, OPEN_EXISTING, 0, NULL);
  10.    SetupComm(hCom, 128, 128);
  11.  
  12.    DCB dcb;
  13.    GetCommState(hCom, &dcb); dcb.BaudRate    =CBR_9600;  dcb.ByteSize =8;
  14.                              dcb.Parity      =NOPARITY;  dcb.StopBits =ONESTOPBIT;
  15.                              dcb.fAbortOnError = FALSE;
  16.    SetCommState(hCom, &dcb);
  17.  
  18.    COMMTIMEOUTS timeouts;
  19.    GetCommTimeouts (hCom, &timeouts);        
  20.             timeouts.ReadIntervalTimeout       =50;
  21.             timeouts.ReadTotalTimeoutConstant  =50;  timeouts.ReadTotalTimeoutMultiplier  =10; 
  22.             timeouts.WriteTotalTimeoutConstant =50;  timeouts.WriteTotalTimeoutMultiplier =10;  
  23.    SetCommTimeouts (hCom, &timeouts);
  24.  
  25.    char rBuff[128]; int rLen=1, rPos =0; //only first byte used in example
  26.    char sBuff[] ="3A 30 31 30 33 30 30 30 30 30 30 30 34 46 38 0D 0A"; int sLen=sizeof(sBuff);
  27.    unsigned long iBytesRead, iBytesWritten;      printf("program start...(s=send, q=quit)\n");
  28.    while (1)
  29.    {  //READ:!!!!!!!!
  30.       if (ReadFile(hCom, rBuff+rPos, rLen, &iBytesRead, NULL)) if (iBytesRead==1)
  31.          printf("%02X", rBuff[rPos]); //todo: ++rPos ...rPos %=buffSize ...
  32.  
  33.       //WRITE (send) if userKey 'S' .. exit if 'Q'
  34.       if (kbhit()) switch (getch()) {   case 's': case 'S':
  35.                                            printf("\n...sending (%d) bytes", sLen);  
  36.                                            WriteFile(hCom, sBuff, sLen, &iBytesWritten, NULL);
  37.                                            printf(" ...sent (%d) bytes.\n", iBytesWritten);
  38.                                            break;                            
  39.                                         case 'q': case 'Q': return 0;  }
  40.    }
  41.    return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement