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