Guest User

Untitled

a guest
Jan 31st, 2012
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <windows.h>
  2. int main(int argc, char* argv[])
  3. {
  4.     char INBUFFER[2]; // Create Input Buffer
  5.     char PacketOne = '\x01';
  6.  
  7.     DWORD        bytes_read    = 1024;    // Number of bytes read from port
  8.     DWORD        bytes_written = 512;    // Number of bytes written to the port
  9.     HANDLE       comport      = NULL;  // Handle COM port
  10.     int          bStatus;              // Status indicator
  11.     DCB          comSettings;          // Contains various port settings
  12.     COMMTIMEOUTS CommTimeouts;         // Contains various COM timeouts
  13.     // Open COM port
  14.     if ((comport =
  15.          CreateFile("\\\\.\\COM4",                // open com4:
  16.                     GENERIC_READ | GENERIC_WRITE, // for reading and writing
  17.                     0,                            // exclusive access
  18.                     NULL,                         // no security attributes
  19.                     OPEN_EXISTING,
  20.                     FILE_ATTRIBUTE_NORMAL,
  21.                     NULL)) == INVALID_HANDLE_VALUE)
  22.     {
  23.         printf("CreateFile() funktioniert nicht\r\n"); // error processing code goes here
  24.         system("pause");
  25.         exit(0);
  26.     }
  27.  
  28.     CommTimeouts.ReadIntervalTimeout         = 0;     // Timeout in ms
  29.     CommTimeouts.ReadTotalTimeoutMultiplier  = 0;     // Timeout in ms
  30.     CommTimeouts.ReadTotalTimeoutConstant    = 0;   // Timeout in ms
  31.     CommTimeouts.WriteTotalTimeoutMultiplier = 0;     // Timeout in ms
  32.     CommTimeouts.WriteTotalTimeoutConstant   = 65000;   // Timeout in ms
  33.     bStatus = SetCommTimeouts(comport,&CommTimeouts); // Set COM timeouts
  34.     if (bStatus != 0)
  35.     {
  36.         printf("SetCommTimeouts() funktioniert nicht\r\n"); // error processing code goes here
  37.     }
  38.     GetCommState(comport, &comSettings); // Getting COM state
  39.     comSettings.BaudRate = 5;           // COM setting
  40.     comSettings.StopBits = ONESTOPBIT;  // COM setting
  41.     comSettings.ByteSize = 7;           // COM setting
  42.     comSettings.Parity   = EVENPARITY;    // COM setting
  43.     comSettings.fParity  = FALSE;       // COM setting
  44.     bStatus = SetCommState(comport, &comSettings); // Setting COM state
  45.     if (bStatus == 0)
  46.     {
  47.         printf("SetCommState() funktioniert nicht.\r\n"); // error processing code goes here
  48.     }
  49.     while(!kbhit()) // Solange keine Taste gedrückt wird
  50.     {
  51.         /////Kommunikation
  52.         comSettings.BaudRate = 9600;           // COM setting
  53.     bStatus = SetCommState(comport, &comSettings); // Setting COM state
  54.  
  55.         bStatus = ReadFile(comport,&INBUFFER,2,&bytes_read,NULL);
  56.         printf("Receiving: %d <-> %d\r\n", INBUFFER[0], INBUFFER[1]);
  57.  
  58.         system("pause");
  59.  
  60.         bStatus = WriteFile(comport,&PacketOne,1,&bytes_written,NULL);
  61.         printf("Sending: %d\r\n", PacketOne+169);
  62.  
  63.         system("pause");
  64.     }
  65.     CloseHandle(comport);
  66.  return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment