00001 #include <windows.h> 00002 #include <stdio.h> 00003 #include "winuser.h" 00004 #include "LSerie.h" 00005 00007 // Construction/Destruction 00009 00012 LSerie::LSerie() 00013 { 00014 hcom = NULL; //Fichier de sortie sur le port COM | The file stream use for acces to the serial port. 00015 bufferSize = 8192; // Buffer d'entrée sortie. 00016 currentPort = -1 ; 00017 } 00018 00019 LSerie::~LSerie() 00020 { 00021 closeCom(); 00022 } 00023 00024 bool LSerie::open(int numPort, long speedInBaud) 00025 { 00026 return open(numPort, speedInBaud, 8, 0, 1); 00027 } 00028 00029 00030 /************************************************************************************ 00031 **** Fonction: Open 00032 **** Derniére modif: 21/05/2005 00033 **** Développeur: Alexandre Fonton 00034 **** Rôle: Ouvre le port RS232 choisi. 00035 **** Recoit: 00036 **** Renvoie: Faux si l'ouverture du port a échoué, sinon Vrai. 00037 *************************************************************************************/ 00038 00039 bool LSerie::open(int numPort, long speedInBaud, int nbBit, int parity, float nbStopBit) 00040 { 00041 char buf[13]; 00042 buf[0]=0; 00043 00044 00045 //--- Vérification des paramétres passés à la fonction: 00046 if(numPort<1 || numPort>256) // Vérification que le numéro de port est compri entre 1 et 9. 00047 return false; 00048 00049 if(speedInBaud<1) // Vérification de la vitesse de communication 00050 return false; 00051 00052 if(nbBit<5 || nbBit > 9) 00053 return false; 00054 00055 if(parity<0 || parity > 2) 00056 return false; 00057 00058 if(nbStopBit<1 || nbStopBit > 2) 00059 return false; 00060 00061 00062 //--- Création d'une chaine de caractère contenant le nom du port série, ex : COM1. 00063 00064 sprintf(buf, "\\\\.\\COM%i", numPort); 00065 00066 //--- Ouverture du port série en lecture / écriture. 00067 hcom=CreateFile(buf, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING , 0, 0); 00068 if (hcom==0 || hcom==INVALID_HANDLE_VALUE) 00069 { 00070 std::string err = getErrorMsg() ; 00071 return false; 00072 } 00073 00074 //--- Définit la durée du TimeOut pour la reception. 00075 setTimeOut(100); 00076 00077 //--- Dimensionne les buffers d'entrée et sortie. 00078 if ( !SetupComm(hcom, bufferSize, bufferSize) ) 00079 return false; 00080 00081 //--- Récupére l'objet de configuration du format des bytes. 00082 if ( !GetCommState(hcom, &dcb)) 00083 return false; 00084 00085 //Vitesse de communication 00086 dcb.BaudRate = speedInBaud; 00087 00088 //Nombre de bit par byte: 00089 dcb.ByteSize = nbBit; 00090 00091 //Nombre de bit de stop: 1, 1.5 ou 2 00092 if(nbStopBit == 1) 00093 dcb.StopBits = ONESTOPBIT; 00094 if(nbStopBit == 1.5) 00095 dcb.StopBits = ONE5STOPBITS; 00096 if(nbStopBit == 2) 00097 dcb.StopBits = TWOSTOPBITS; 00098 00099 //Vérification de la parité: 0 pas de vérif, 1 pair, 2 impaire. 00100 if(parity == 0) 00101 dcb.Parity = NOPARITY; 00102 if(parity == 1) 00103 dcb.Parity = ODDPARITY; 00104 if(parity == 2) 00105 dcb.Parity = EVENPARITY; 00106 00107 // ajoute le handle pour scol 00108 comport = hcom; 00109 00110 //--- Stocke l'objet de configuration du format des bytes. 00111 if (!SetCommState(hcom, &dcb)) 00112 return false; 00113 else 00114 return true; 00115 } 00116 00117 00118 /************************************************************************************ 00119 **** Fonction: CloseCom 00120 **** Derniére mofif: 01/07/2004 00121 **** Développeur: Alexandre Fonton 00122 **** Rôle: Ferme le port RS232 actuellement ouvert. 00123 **** Recoit: Rien 00124 **** Renvoie: Rien 00125 *************************************************************************************/ 00126 00127 void LSerie::closeCom() 00128 { 00129 if(hcom) 00130 { 00131 CloseHandle(hcom); 00132 hcom = NULL; 00133 } 00134 } 00135 00136 /************************************************************************************ 00137 **** Fonction: SetTimeout 00138 **** Derniére mofif: 01/07/2004 00139 **** Développeur: Alexandre Fonton 00140 **** Rôle: Définit la durée maximum d'attente d'une information (TimeOut). 00141 **** Recoit: Temps d'attente en ms. 00142 **** Renvoie: Faux si l'opération a échoué, sinon Vrai. 00143 *************************************************************************************/ 00144 00145 bool LSerie::setTimeOut(DWORD ms) 00146 { 00147 //--- Vérification des paramétres passés à la fonction: 00148 if( ms<0) 00149 return false; 00150 00151 ct.ReadIntervalTimeout = 0; 00152 ct.ReadTotalTimeoutMultiplier = 0; // pas de multipicateur de timeout en fonction du nombre de caractères demandés. 00153 ct.ReadTotalTimeoutConstant = ms; 00154 ct.WriteTotalTimeoutMultiplier = 0; 00155 ct.WriteTotalTimeoutConstant = ms; 00156 if ( !SetCommTimeouts(hcom, &ct) ) //Configuration du Time Out 00157 return false; 00158 return false; 00159 //MSDN: The SetCommTimeouts function sets the time-out parameters for all read and write operations on a specified communications device. 00160 } 00161 00162 /********************************************************************** 00163 **** Fonction: SetBaud 00164 **** Derniére mofif: 01/07/2004 00165 **** Développeur: Alexandre Fonton 00166 **** Rôle: Définit la vitesse de communication (ex:9600 baud). 00167 **** Recoit: Vitesse de communication en baud. 00168 **** Renvoie: Faux si l'opération a échoué, sinon Vrai. 00169 ***********************************************************************/ 00170 00171 bool LSerie::setSpeed(DWORD baudrate) 00172 { 00173 //--- Vérification des paramétres passés à la fonction: 00174 if( baudrate<1) 00175 return false; 00176 00177 00178 if (!GetCommState(hcom, &dcb)) 00179 return FALSE; 00180 00181 dcb.BaudRate = baudrate; 00182 00183 if (!SetCommState(hcom, &dcb)) 00184 return FALSE; 00185 else 00186 return TRUE; 00187 00188 00189 00190 00191 //MSDN: The SetCommState function configures a communications device according to the specifications in a device-control block (a DCB structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues. 00192 } 00193 00194 00195 00196 /********************************************************************** 00197 **** Fonction: SendData(string) 00198 **** Derniére mofif: 22/05/2005 00199 **** Développeur: Alexandre Fonton 00200 **** Rôle: Envoie une chaine de caractères contenu dans un type "string" sur le port RS232 actuellement ouvert. 00201 **** Recoit: Un pointeur sur une chaine de caractéres. 00202 **** Renvoie: -1 si l'opération a échoué, sinon le nombre de caractères envoyés. 00203 ***********************************************************************/ 00204 00205 int LSerie::sendData(string* data) 00206 { 00207 //--- Vérification des paramétres passés à la fonction: 00208 if( data == NULL ) 00209 return false; 00210 00211 return sendData((DWORD)data->size(), (LPBYTE)data->data()); 00212 } 00213 00214 00215 00216 /********************************************************************** 00217 **** Fonction: SendData(DWORD lg, LPBYTE data) 00218 **** Derniére mofif: 01/07/2004 00219 **** Développeur: Alexandre Fonton 00220 **** Rôle: Envoie une série d'octets sur le port RS232 actuellement ouvert. 00221 **** Recoit: Un pointeur sur une chaine de caractéres (data) et sa taille (lg). 00222 **** Renvoie: -1 si l'opération a échoué, sinon le nombre de caractères envoyés. 00223 ***********************************************************************/ 00224 int LSerie::sendData(DWORD lg, LPBYTE data) 00225 { 00226 DWORD result=0; 00227 00228 //--- Vérification des paramétres passés à la fonction: 00229 if( lg<0 || data==NULL) 00230 return false; 00231 00232 00233 if ( !WriteFile(hcom, data, lg, &result, 0) ) 00234 return -1; 00235 else 00236 return (int)result; 00237 00238 //MSDN: The WriteFile function writes data to a file and is designed for both synchronous 00239 // and asynchronous operation. The function starts writing data to the file at the 00240 // position indicated by the file pointer. After the write operation has been completed 00241 // , the file pointer is adjusted by the number of bytes actually written, except when 00242 // the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for 00243 // overlapped input and output (I/O), the application must adjust the position of the 00244 // file pointer after the write operation is finished. 00245 // This function is designed for both synchronous and asynchronous operation. 00246 // The WriteFileEx function is designed solely for asynchronous operation. 00247 // It lets an application perform other processing during a file write operation. 00248 00249 } 00250 00251 00252 00253 00254 00255 00256 00257 /********************************************************************** 00258 **** Fonction: ReceiveData(string* data) 00259 **** Derniére mofif: 22/05/2005 00260 **** Développeur: Alexandre Fonton 00261 **** Rôle: Lecture du buffer de reception du port RS232 actuellement ouvert. 00262 **** Recoit: Un pointeur sur une string. 00263 **** Renvoie: -1 si l'opération a échoué, sinon le nombre de caractères recu. 00264 ***********************************************************************/ 00265 int LSerie::receiveData(string* data) 00266 { char buffer[1025]; 00267 int nbChar=0; 00268 00269 //--- Vérification des paramétres passés à la fonction: 00270 if( data==NULL) 00271 return false; 00272 00273 nbChar = receiveData(1024, (LPBYTE)buffer); 00274 buffer[nbChar] = 0; // caractère de fin de chaine. 00275 data->assign(buffer); 00276 return nbChar; 00277 } 00278 00279 00280 /********************************************************************** 00281 **** Fonction: ReceiveData 00282 **** Derniére mofif: 01/07/2004 00283 **** Développeur: Alexandre Fonton 00284 **** Rôle: Lecture du buffer de reception du port RS232 actuellement ouvert. 00285 **** Recoit: Un pointeur sur une chaine de caractéres (data) et sa taille maximale possible (lg). 00286 **** Renvoie: -1 si l'opération a échoué, sinon le nombre de caractères recu. 00287 ***********************************************************************/ 00288 int LSerie::receiveData(DWORD lg, LPBYTE data) 00289 { 00290 DWORD result=0; 00291 00292 //--- Vérification des paramétres passés à la fonction: 00293 if( lg<0 || data==NULL) 00294 return false; 00295 00296 if (ReadFile(hcom, data, lg, &result, 0)) 00297 { 00298 return (int)result; 00299 } 00300 else 00301 return -1; 00302 00303 //MSDN: The ReadFile function reads data from a file, starting at the position indicated 00304 // by the file pointer. After the read operation has been completed, the file pointer 00305 // is adjusted by the number of bytes actually read, unless the file handle is 00306 // created with the overlapped attribute. If the file handle is created for 00307 // overlapped input and output (I/O), the application must adjust the position of 00308 // the file pointer after the read operation. 00309 // This function is designed for both synchronous and asynchronous operation. 00310 // The ReadFileEx function is designed solely for asynchronous operation. It lets 00311 // an application perform other processing during a file read operation. 00312 00313 } 00314 00315 00316 /**************************** SetRts(val) ************************************************** 00317 **** Positionne RTS au niveau val (0 ou 1) */ 00318 bool LSerie::setRts(bool val) 00319 { 00320 if(val) 00321 { 00322 if(EscapeCommFunction(hcom, SETRTS) == TRUE ) 00323 return true; 00324 } 00325 else 00326 { 00327 if(EscapeCommFunction(hcom, CLRRTS) == TRUE ) 00328 return true; 00329 } 00330 00331 return false; 00332 } 00333 00334 00335 /**************************** SetTxd(val) ************************************************** 00336 **** Positionne Txd au niveau val (0 ou 1) */ 00337 bool LSerie::setTxd(bool val) 00338 { 00339 if(val) 00340 { 00341 if( EscapeCommFunction(hcom, SETBREAK) == TRUE ) 00342 return true; 00343 } 00344 else 00345 { 00346 if( EscapeCommFunction(hcom, CLRBREAK) == TRUE ) 00347 return true; 00348 } 00349 return false; 00350 } 00351 00352 00353 00354 /**************************** SetDtr(val) ************************************************** 00355 **** Positionne DTR au niveau val (0 ou 1) */ 00356 bool LSerie::setDtr(bool val) 00357 { 00358 if(val) 00359 { 00360 if( EscapeCommFunction(hcom, SETDTR) == TRUE ) 00361 return true; 00362 } 00363 else 00364 { 00365 if( EscapeCommFunction(hcom, CLRDTR) == TRUE ) 00366 return false; 00367 } 00368 return false; 00369 } 00370 00371 00372 00373 /********************** GetCts() ***********************/ 00374 bool LSerie::getCts() 00375 { 00376 DWORD result; 00377 GetCommModemStatus(hcom, &result); 00378 if(result & MS_CTS_ON) 00379 return true; 00380 else 00381 return false; 00382 } 00383 00384 /********************** GetDtr() ***********************/ 00385 bool LSerie::getDtr() 00386 { 00387 DWORD result; 00388 GetCommModemStatus(hcom, &result); 00389 if(result & MS_DSR_ON) 00390 return true; 00391 else 00392 return false; 00393 } 00394 00395 /********************** GetRi() ***********************/ 00396 bool LSerie::getRi() 00397 { 00398 DWORD result; 00399 GetCommModemStatus(hcom, &result); 00400 if(result & MS_RING_ON) 00401 return true; 00402 else 00403 return false; 00404 } 00405 00406 00407 /********************** GetCd() ***********************/ 00408 bool LSerie::getCd() 00409 { int err=0; 00410 DWORD result; 00411 err = GetCommModemStatus(hcom, &result); 00412 if(result & MS_RLSD_ON) 00413 return true; 00414 else 00415 return false; 00416 } 00417 00418 00419 00420 00421 string LSerie::getErrorMsg() 00422 { 00423 LPVOID lpMsgBuf; 00424 string sErreur = ""; 00425 00426 if ( FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 00427 NULL, GetLastError(), 00428 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 00429 (LPTSTR) &lpMsgBuf, 0, NULL )) 00430 { 00431 sErreur.assign((LPCTSTR)lpMsgBuf); 00432 } 00433 00434 return sErreur; 00435 } 00436 00437 00438 00439 00440 00441 00442 00443 bool LSerie::isConnect() 00444 { 00445 DWORD result=0; 00446 00447 if ( !WriteFile(hcom, "A\r", 2, &result, 0) ) 00448 return false; 00449 else 00450 return true; 00451 }
1.6.3