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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
//---------------------------------------------------------------------------
__fastcall TThreadCOM::TThreadCOM(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TThreadCOM::CloseComPort(void)
{
EscapeCommFunction(comHandle, CLRDTR);
CloseHandle(comHandle);
}
//---------------------------------------------------------------------------
bool __fastcall TThreadCOM::WriteComPort(AnsiString msg)
{
int Emission_nb_octets = msg.Length();
char *trame_envoi="";
DWORD numWrite;
strcpy(trame_envoi,msg.c_str());
if (WriteFile(comHandle, trame_envoi, Emission_nb_octets, &numWrite, 0) == FALSE) return false;
else return true;
}
//---------------------------------------------------------------------------
HANDLE __fastcall TThreadCOM::OpenComPort(AnsiString COMport, unsigned int comspeed)
{
if(!EndThread && CheckCOMAvailibility(COMport))
{
// Open the COM port
COMport = "\\\\.\\" + COMport;
comHandle = CreateFile(COMport.c_str(),
GENERIC_READ|GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (comHandle == INVALID_HANDLE_VALUE)
return (INVALID_HANDLE_VALUE);
// Get the current settings of the COMM port
success = GetCommState(comHandle, &dcb);
if (!success)
return (INVALID_HANDLE_VALUE);
// Modify the baud rate, etc.
dcb.BaudRate = comspeed;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = 0;
// Apply the new comm port settings
success = SetCommState(comHandle, &dcb);
if (!success)
{
Application->MessageBox(AnsiString("Erreur Windows n°" + IntToStr(GetLastError())).c_str(),"Erreur d'ouverture",MB_OK);
return (INVALID_HANDLE_VALUE);
}
// Change the ReadIntervalTimeout so that
// ReadFile will return immediately. See
// help file
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts( comHandle, &timeouts );
// Set the Data Terminal Ready line
EscapeCommFunction(comHandle, SETDTR);
PurgeComm(comHandle, PURGE_RXCLEAR);
PortOpen = true;
return comHandle;
}
return INVALID_HANDLE_VALUE;
}
//---------------------------------------------------------------------------
void __fastcall TThreadCOM::Execute()
{
DWORD Count;
char Str[1024];
bool error;
/* initialisation of DCB*/
PortOpen = false;
EndThread = false;
while (!EndThread)
{
Sleep(1); //évite d'occuper l'UC à 100%
if(PortOpen && ReadFile(comHandle, Str, 1024, &Count, 0))
{
//if(Count > 0)
{
Form1->Memo1->Lines->Add(IntToStr((int)Count) + " octets reçus");
for(int i=0;i<(int)Count;i++)
Form1->Memo1->Lines->Add("Octet reçu: " + IntToStr((int)Str[i]&255));
Form1->Memo1->Lines->Add("");
}
}
else if(!error)
{
ShowMessage("Erreur de lecture");
error = true;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TThreadCOM::CloseCOM(void)
{
if(!EndThread)
{
EndThread = true;
CloseComPort();
}
}
//---------------------------------------------------------------------------
boolean __fastcall TThreadCOM::CheckCOMAvailibility(AnsiString COMport)
{
COMport = "\\\\.\\" + COMport;
if (OpenCheckComPort(COMport.c_str()) != INVALID_HANDLE_VALUE)
{
EscapeCommFunction(CheckHandle, CLRDTR);
CloseHandle(CheckHandle);
return(true);
}
return(false);
}
//---------------------------------------------------------------------------
void __fastcall TThreadCOM::StopCOM(void)
{
if(!EndThread && !Suspended)
Suspend();
}
//---------------------------------------------------------------------------
void __fastcall TThreadCOM::RestartCOM(void)
{
if(!EndThread && Suspended)
Resume();
}
//---------------------------------------------------------------------------
HANDLE __fastcall TThreadCOM::OpenCheckComPort(AnsiString COMport)
{
unsigned int comspeed = 4800;
// Open the COM port
COMport = "\\\\.\\" + COMport;
CheckHandle = CreateFile(COMport.c_str(),
GENERIC_READ|GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (CheckHandle == INVALID_HANDLE_VALUE)
return (INVALID_HANDLE_VALUE);
// Get the current settings of the COMM port
success = GetCommState(CheckHandle, &dcb);
if (!success)
return (INVALID_HANDLE_VALUE);
// Modify the baud rate, etc.
dcb.BaudRate = comspeed;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = 0;
// Apply the new comm port settings
success = SetCommState(CheckHandle, &dcb);
if (!success)
{
Application->MessageBox(AnsiString("Erreur Windows n°" + IntToStr(GetLastError())).c_str(),"Erreur d'ouverture",MB_OK);
return (INVALID_HANDLE_VALUE);
}
// Change the ReadIntervalTimeout so that
// ReadFile will return immediately. See
// help file
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts( CheckHandle, &timeouts );
// Set the Data Terminal Ready line
EscapeCommFunction(CheckHandle, SETDTR);
PurgeComm(CheckHandle, PURGE_RXCLEAR);
PortOpen = true;
return CheckHandle;
}
//--------------------------------------------------------------------------- |