Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication6.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <Windows.h>
- #include <io.h>
- #include <time.h>
- #include <string.h>
- #include <stdlib.h>
- #include "Header.h"
- #define cbOutQueue 1024
- #define cbInQueue 1024
- DWORD WINAPI COM_read(DWORD NumberOfBytesToRead, int sec){
- char lpBufforRead[cbInQueue];
- char lpBufforWrite[cbOutQueue];
- DWORD RS_ile; //ile bitow wyslanych
- memset(lpBufforRead, '\0', cbInQueue);
- ReadFile(hCom, &lpBufforRead, NumberOfBytesToRead, &RS_ile, 0);
- printf(lpBufforRead);
- WriteFile(hCom, lpBufforRead, NumberOfBytesToRead, &RS_ile, 0);
- return 1;
- }
- int COM_connect (int num){
- // com port
- DCB dcb; //konfiguracja portu
- BOOL fSuccess; //flaga pomocnicza
- char ComName[15];
- sprintf_s(ComName, "\\\\.\\COM%d", num);
- printf("COM: %s\n", ComName);
- hCom = CreateFileA(ComName, GENERIC_READ | GENERIC_WRITE,
- 0, //exclusive access
- NULL, //default security attributes
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hCom != INVALID_HANDLE_VALUE){
- SetupComm(hCom, cbInQueue, cbOutQueue);
- dcb.DCBlength = sizeof(dcb);
- GetCommState(hCom, &dcb);
- dcb.BaudRate = CBR_9600; // predkosc transmisji
- dcb.ByteSize = 8; // liczba bitow danych
- dcb.fParity = TRUE; // sprawdzanie parzystosci
- dcb.Parity = NOPARITY; // brak bitu parzystosci
- dcb.StopBits = ONESTOPBIT; // jeden bit stopu
- dcb.EvtChar = 0x0A;
- SetCommState(hCom, &dcb); // zapisz strukture
- GetCommMask(hCom, &fdwEvtMask); // maska zglasza przerwania do procesora, tu pobiera
- SetCommMask(hCom, EV_RXCHAR); // tu ustawia
- printf("Polaczono z COM nr %u\n", num);
- connected = 1;
- return 1;
- }
- else{
- printf("Brak polaczenia z COM. Blad: %d.\n", GetLastError());
- connected = 0;
- }
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- DWORD nNumberOfBytesToRead;
- int COM_nr = 2; //atoi(argv[2]);
- COM_connect(COM_nr);
- while (connected == 1){
- if (WaitCommEvent(hCom, &fdwEvtMask, NULL)){
- COMSTAT statsread; //status
- ClearCommError(hCom, NULL, &statsread); // sprawdza status
- if(statsread.cbInQue != 0){
- if(statsread.cbInQue > 0){
- if (statsread.cbInQue > cbInQueue)
- nNumberOfBytesToRead = cbInQueue;
- else nNumberOfBytesToRead = statsread.cbInQue;
- }
- }
- time(&timer);
- int seconds = difftime(timer, NULL);
- COM_read(nNumberOfBytesToRead, seconds);
- }
- }
- while (connected == 0){
- time(&timer);
- int sec = difftime(timer, NULL);
- printf("time: %d\n", sec);
- Sleep(5000);
- }
- return 0;
- }
- //////////////////////////////////////////////
- HANDLE hCom;
- int connected;
- DWORD fdwEvtMask;
- time_t timer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement