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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| //---------------------------------------------------------------------------
#include "serie.h"
#include <assert.h>
#include <vcl.h>
//---------------------------------------------------------------------------
serie::serie()
{
connecter = false;
}
serie::~serie(){
}
bool serie::connexion(){
bool retour=true;
hcomm=CreateFile(
"COM1",
GENERIC_WRITE|GENERIC_READ,
0, // accée exclusif
NULL, // attribue de sécurité par défaut
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hcomm==INVALID_HANDLE_VALUE) // Si la connection n'a pas été faite ont retournera false
{
retour = false;
}
else
{
GetCommState(hcomm,&config_serie);
config_serie.BaudRate = 115200;
config_serie.ByteSize =8;
config_serie.Parity = NOPARITY;
config_serie.StopBits = ONESTOPBIT;
SetCommState(hcomm,&config_serie);
connecter=true;
}
return retour;
}
void serie::envoi_trame(char *trame){
if(connecter==true){
unsigned long pt;
WriteFile(hcomm,trame, strlen(trame)+1,&pt,&over); // ecrit sur le port
PurgeComm(hcomm,PURGE_TXCLEAR); // Purge le buffer du port série
}
}
char *serie::lecture_trame(unsigned long taille){
unsigned long pt; char trame[100];
ReadFile(hcomm,&trame, 255,&pt, &over); // ecrit sur le port
PurgeComm(hcomm,PURGE_RXCLEAR); // Purge le buffer du port série
return trame;
}
void serie::deconnexion(){
if(connecter==true){
CloseHandle(hcomm);
}
}
bool serie::WaitCommEvent2(DWORD &EventMask){
bool retour=true;
if (connecter == true){
if (!WaitCommEvent(hcomm,&EventMask,&over)){
ShowMessage(GetLastError());
retour=false;
}
}else{
retour = false;
}
return retour;
}
DWORD WINAPI serie::Thread_reception(LPVOID lpParam){
/* serie * Tserie = (serie *)lpParam;
DWORD EventMask=0;
while(1){
if (!Tserie->WaitCommEvent2(EventMask)){
switch (GetLastError()){
case 87:
case ERROR_IO_PENDING:
break;
default:
int i = 0;
break;
}
}
WaitEvent = WaitForMultipleObjects(2,Tserie->hThreadEvent,FALSE,INFINITE);
if (WaitEvent == 0){
}
if (WaitEvent == 1){
if (EventMask & EV_RXCHAR){
struct_event->t++;
}
}
} */
}
bool serie::init_thread(){
bool retour = true;
SetCommMask(hcomm,EV_RXCHAR); // Sélection de l'évenement pour declancher la lecture
over.hEvent= CreateEvent (
NULL, // Attribu de sécurité
FALSE, // Reset de l'évenement automatiquement
FALSE,// Etat initial de l'évement
NULL);
over.Internal=0;
over.InternalHigh=0;
over.Offset=0;
over.OffsetHigh=0;
if (SetEvent(over.hEvent)){
ShowMessage("Event Reception OK");
hThreadEvent[0] = over.hEvent;
}
hThreadEvent[1] = CreateEvent(NULL,TRUE,FALSE,NULL);
if (SetEvent(hThread_event[1])){
ShowMessage("Event Stop Ok");
ResetEvent(hThreadEvent[1]);
}
hThread = CreateThread(
0,
0,
Thread_reception,
(LPVOID)this,
NULL,
&id_thread);
if (hThread == NULL){
retour = false;
}
return retour;
} |
Partager