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
|
HANDLE hcom;
bool fSuccess;
DCB dcb;
COMMTIMEOUTS timeout;
CPortserie::CPortserie()
{
}
CPortserie::~CPortserie()
{
}
void CPortserie::OuvreToi(char* nomPort)
{
hcom = CreateFile( nomPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hcom == INVALID_HANDLE_VALUE) {
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
// return (1);
}
}
void CPortserie::ConfigureToi(DWORD vitesse,BYTE nbBits, BYTE parite, BYTE nbStopBits)
{
// We will build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hcom, &dcb);
if (!fSuccess) {
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
// return (2);
}
// Fill in the DCB: baud=115200 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = vitesse; // set the baud rate
dcb.ByteSize = nbBits; // data size, xmit, and rcv
dcb.Parity = parite; // no parity bit
dcb.StopBits = nbStopBits; // one stop bit
fSuccess = SetCommState(hcom, &dcb);
if (!fSuccess) {
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
//return (3);
}
printf("\n Configuration OK \n\n");
}
void CPortserie::ReglerTimeout()
{
timeout.ReadIntervalTimeout=0;
timeout.ReadTotalTimeoutMultiplier=2;
timeout.ReadTotalTimeoutConstant=100;
timeout.WriteTotalTimeoutMultiplier=0;
timeout.WriteTotalTimeoutConstant=0;
fSuccess = SetCommTimeouts(hcom, &timeout) ; // time-out values
printf("\n Timeout OK \n\n");
}
void CPortserie::Emet(char* MessageEnvoi)
{
DWORD NombredeCaracteresEcrits;
DWORD NombredeCaractereslus;
WriteFile( hcom, MessageEnvoi, strlen(MessageEnvoi), &NombredeCaracteresEcrits, NULL);
printf("\n Message Envoye : %s \n\n",MessageEnvoi);
}
void CPortserie::Recoit(char* MessageReception)
{
DWORD NombredeCaractereslus;
ReadFile( hcom, MessageReception, 2, &NombredeCaractereslus, NULL);
ReadFile( hcom, MessageReception, strlen(MessageReception), &NombredeCaractereslus, NULL);
printf("\n reponse :%s \n\n\n",MessageReception);
} |