Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. // ConsoleApplication6.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <Windows.h>
  7. #include <io.h>
  8. #include <time.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "Header.h"
  12.  
  13. #define cbOutQueue 1024
  14. #define cbInQueue 1024
  15.  
  16. DWORD WINAPI COM_read(DWORD NumberOfBytesToRead, int sec){
  17.     char lpBufforRead[cbInQueue];
  18.     char lpBufforWrite[cbOutQueue];
  19.     DWORD RS_ile; //ile bitow wyslanych
  20.  
  21.     memset(lpBufforRead, '\0', cbInQueue);
  22.     ReadFile(hCom, &lpBufforRead, NumberOfBytesToRead, &RS_ile, 0);
  23.     printf(lpBufforRead);
  24.     WriteFile(hCom, lpBufforRead, NumberOfBytesToRead, &RS_ile, 0);
  25.  
  26.     return 1;
  27.  
  28. }
  29.  
  30.  
  31. int COM_connect (int num){
  32.     // com port
  33.  
  34.     DCB dcb;       //konfiguracja portu
  35.     BOOL fSuccess; //flaga pomocnicza
  36.     char ComName[15];
  37.  
  38.     sprintf_s(ComName, "\\\\.\\COM%d", num);
  39.     printf("COM: %s\n", ComName);
  40.  
  41.     hCom = CreateFileA(ComName, GENERIC_READ | GENERIC_WRITE,
  42.         0,    //exclusive access
  43.         NULL, //default security attributes
  44.         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  45.  
  46.     if (hCom != INVALID_HANDLE_VALUE){
  47.         SetupComm(hCom, cbInQueue, cbOutQueue);
  48.         dcb.DCBlength = sizeof(dcb);
  49.         GetCommState(hCom, &dcb);
  50.  
  51.         dcb.BaudRate = CBR_9600;        // predkosc transmisji
  52.         dcb.ByteSize = 8;               // liczba bitow danych
  53.         dcb.fParity = TRUE;             // sprawdzanie parzystosci
  54.         dcb.Parity = NOPARITY;          // brak bitu parzystosci
  55.         dcb.StopBits = ONESTOPBIT;      // jeden bit stopu
  56.         dcb.EvtChar = 0x0A;
  57.    
  58.         SetCommState(hCom, &dcb);       // zapisz strukture
  59.  
  60.         GetCommMask(hCom, &fdwEvtMask); // maska zglasza przerwania do procesora, tu pobiera
  61.         SetCommMask(hCom, EV_RXCHAR);   // tu ustawia
  62.  
  63.         printf("Polaczono z COM nr %u\n", num);
  64.         connected = 1;
  65.  
  66.         return 1;
  67.     }
  68.  
  69.     else{
  70.         printf("Brak polaczenia z COM. Blad: %d.\n", GetLastError());
  71.         connected = 0;
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
  77. int _tmain(int argc, _TCHAR* argv[])
  78. {
  79.     DWORD nNumberOfBytesToRead;
  80.  
  81.     int COM_nr = 2; //atoi(argv[2]);
  82.  
  83.     COM_connect(COM_nr);
  84.  
  85.     while (connected == 1){
  86.         if (WaitCommEvent(hCom, &fdwEvtMask, NULL)){
  87.             COMSTAT statsread; //status
  88.             ClearCommError(hCom, NULL, &statsread); // sprawdza status
  89.  
  90.             if(statsread.cbInQue != 0){
  91.                 if(statsread.cbInQue > 0){
  92.                     if (statsread.cbInQue > cbInQueue)
  93.                         nNumberOfBytesToRead = cbInQueue;
  94.                     else nNumberOfBytesToRead = statsread.cbInQue;
  95.                 }
  96.             }
  97.  
  98.             time(&timer);
  99.            
  100.             int seconds = difftime(timer, NULL);
  101.  
  102.             COM_read(nNumberOfBytesToRead, seconds);
  103.         }
  104.     }
  105.  
  106.     while (connected == 0){
  107.         time(&timer);
  108.  
  109.         int sec = difftime(timer, NULL);
  110.  
  111.         printf("time: %d\n", sec);
  112.         Sleep(5000);
  113.     }
  114.  
  115.     return 0;
  116. }
  117.  
  118.  
  119.  
  120. //////////////////////////////////////////////
  121. HANDLE hCom;
  122. int connected;
  123. DWORD fdwEvtMask;
  124. time_t timer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement