1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "error.hpp"
#include "serie.hpp"
using namespace std;
serie::serie(string path, int speed, int parity, int nbrBits, int stopBits,
int fluxCtrl)
{
BOOL res;
DCB dcb = {0};
serie::m_h = CreateFile(path.c_str(), GENERIC_READ | GENERIC_WRITE,
0, // no sharing
0, // can't inherit this handle
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (serie::m_h== INVALID_HANDLE_VALUE)
throw error::error("Impossible d'ouvrir le port serie",-10);
if (!GetCommState(serie::m_h, &dcb))
throw error::error("Ne peut pas recuperer les options du port serie ",-11);
/*remise a zero du device control block*/
memset(&dcb, 0, sizeof(dcb));
/*configuration de la connection*/
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = speed; // speed, 4800 bps for NMAE 187
dcb.fBinary = TRUE; // because the win32 docs say so
dcb.fParity = (parity==0);
/*a gerer */
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.ByteSize = nbrBits; // char size -- 8 bits
dcb.Parity =parity; // parity
dcb.StopBits = stopBits;
// set the new options
res = SetCommState(m_h, &dcb);
if (!res)
throw error::error("Ne peut pas mettre les options du port serie ",-12);
COMMTIMEOUTS timeouts;
memset(&timeouts, 0, sizeof(timeouts));
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 1300;
res = SetCommTimeouts(m_h, &timeouts);
if (!res)
throw error::error("Ne peut pas configurer le timeout du port serie",-13);
res = SetupComm(m_h, 1200,1200);
if (!res)
throw error::error("Ne peut pas initialiser le port serie ",-14);
}
char serie::get_Byte()
{
char c;
DWORD num_read = 0;
OVERLAPPED ol;
memset(&ol, 0, sizeof(ol));
HANDLE hd;
hd = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hd == 0)
throw error::error("Impossible d'obtenir un event ",-15);
ol.hEvent = hd;
BOOL res = ReadFile(m_h, &c, 1, &num_read, &ol);
if (!res && GetLastError() != ERROR_IO_PENDING)
throw error::error("Ne peut pas lire les données sur le port serie ",-16);
else if (!res) {
// read waiting to finish
res = WaitForSingleObject(ol.hEvent, 1300);
switch (res) {
case WAIT_TIMEOUT:
throw error::error("Le timeout est depasse",-17);
break;
case WAIT_OBJECT_0:
res = GetOverlappedResult(m_h, &ol, &num_read, FALSE);
break;
default:
throw error::error("N'a pas pu lire durant le temps imparti ",-18);
}
}
else
// fall through and check num_read
if (num_read != 1)
std::cout << "error" << std::endl;
return c;
}
string serie::get_line()
{
char c;
string trame ="";
while((c = get_Byte())!='\n')
trame += c;
return trame;
}
serie::~serie()
{
if(CloseHandle(m_h)!=0)
throw error::error("N'a pas pu fermer la communication avec la liaison serie",-19);
cout << "close"<<endl;
} |
Partager