00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #include <windows.h>
00023 #include <stdio.h>
00024
00025
00026 #ifndef ERS_PECHAR
00027 #ifdef _WIN32_WCE
00028 #define ERS_PECHAR (L' ')
00029 #else
00030 #define ERS_PECHAR (' ')
00031 #endif
00032 #endif
00033
00034 #ifdef _WIN32_WCE
00035 #define ERSLIBMAXPORT 9
00036 int ers_initdone[ERSLIBMAXPORT]={0,0,0,0,0,0,0,0,0};
00037 #else
00038 #define ERSLIBMAXPORT 256
00039 int ers_initdone[ERSLIBMAXPORT]={
00040 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00041 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00042 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00043 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00044 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00045 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00046 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00047 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
00048 };
00049 #endif
00050
00051 #define ERS_110 0x00000001
00052 #define ERS_300 0x00000002
00053 #define ERS_600 0x00000003
00054 #define ERS_1200 0x00000004
00055 #define ERS_2400 0x00000005
00056 #define ERS_4800 0x00000006
00057 #define ERS_9600 0x00000007
00058 #define ERS_14400 0x00000008
00059 #define ERS_19200 0x00000009
00060 #define ERS_38400 0x0000000A
00061 #define ERS_56000 0x0000000B
00062 #define ERS_57600 0x0000000C
00063 #define ERS_115200 0x0000000D
00064 #define ERS_128000 0x0000000E
00065 #define ERS_256000 0x0000000F
00066
00067 #define ERS_1 0x00000010
00068 #define ERS_15 0x00000020
00069 #define ERS_2 0x00000030
00070
00071 #define ERS_NO 0x00000100
00072 #define ERS_ODD 0x00000200
00073 #define ERS_EVEN 0x00000300
00074 #define ERS_MARK 0x00000400
00075 #define ERS_SPACE 0x00000500
00076
00077 #define ERS_4 0x00001000
00078 #define ERS_5 0x00002000
00079 #define ERS_6 0x00003000
00080 #define ERS_7 0x00004000
00081 #define ERS_8 0x00005000
00082
00083 #define ERS_DTR_N 0x00010000
00084 #define ERS_DTR_Y 0x00020000
00085 #define ERS_DTR_H 0x00030000
00086
00087 #define ERS_RTS_N 0x00100000
00088 #define ERS_RTS_Y 0x00200000
00089 #define ERS_RTS_H 0x00300000
00090 #define ERS_RTS_T 0x00400000
00091
00092 #define ERS_CTS_Y 0x01000000
00093 #define ERS_CTS_N 0x02000000
00094 #define ERS_DSR_Y 0x04000000
00095 #define ERS_DSR_N 0x08000000
00096
00097 #define ERS_X_Y 0x10000000
00098 #define ERS_X_N 0x20000000
00099
00100
00101 HANDLE ers_hcom[ERSLIBMAXPORT];
00102
00103 #define ERSHCOMn (ers_hcom[n-1])
00104
00105
00106
00107 int ers_check(int n)
00108 {
00109 if(n<1 || n>ERSLIBMAXPORT) return 1;
00110 if(!ers_initdone[n-1]) return 1;
00111 return 0;
00112 }
00113
00114
00115
00116
00117 int ERS_XoffXon(int n, int xoff, int xon)
00118 {
00119 DCB dcb;
00120
00121 if(ers_check(n)) return 1;
00122
00123 GetCommState(ERSHCOMn, &dcb);
00124 dcb.XoffLim=xoff;
00125 dcb.XonLim=xon;
00126 if (!SetCommState(ERSHCOMn, &dcb)) return 2;
00127 return 0;
00128 }
00129
00130
00131 int ERS_Config(int n, unsigned int data)
00132 {
00133 DCB dcb;
00134 int d;
00135 int baud[16]={0,CBR_110,CBR_300,CBR_600,CBR_1200,CBR_2400,CBR_4800,CBR_9600,CBR_14400,CBR_19200,CBR_38400,CBR_56000,CBR_57600,CBR_115200,CBR_128000,CBR_256000};
00136 int stopbit[4]={0,ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS};
00137 int parity[6]={0,NOPARITY,ODDPARITY,EVENPARITY,MARKPARITY,SPACEPARITY};
00138 int bytesize[6]={0,4,5,6,7,8};
00139 int dtr[4]={0,DTR_CONTROL_DISABLE,DTR_CONTROL_ENABLE,DTR_CONTROL_HANDSHAKE};
00140 int rts[5]={0,RTS_CONTROL_DISABLE,RTS_CONTROL_ENABLE,RTS_CONTROL_HANDSHAKE,RTS_CONTROL_TOGGLE};
00141
00142 if(ers_check(n)) return 1;
00143
00144 GetCommState(ERSHCOMn, &dcb);
00145
00146
00147 d= data & 0xF; if (d) dcb.BaudRate=baud[d];
00148
00149
00150 d=(data & 0x30)>>4; if (d) dcb.StopBits=stopbit[d];
00151
00152
00153 d=(data & 0x700)>>8;
00154 if (d) {
00155 dcb.Parity=parity[d];
00156 if(d>1){
00157 dcb.fParity=TRUE;
00158 dcb.fErrorChar=TRUE;
00159 dcb.ErrorChar=ERS_PECHAR;
00160 }else{
00161 dcb.fParity=FALSE;
00162 dcb.fErrorChar=FALSE;
00163 }
00164 }
00165
00166
00167 d=(data & 0x7000)>>12; if (d) dcb.ByteSize=bytesize[d];
00168
00169
00170 d=(data & 0x30000)>>16; if (d) dcb.fDtrControl=dtr[d];
00171
00172
00173 d=(data & 0x700000)>>20; if (d) dcb.fRtsControl=rts[d];
00174
00175
00176 if(data & ERS_CTS_Y) dcb.fOutxCtsFlow=TRUE;
00177 if(data & ERS_CTS_N) dcb.fOutxCtsFlow=FALSE;
00178
00179 if(data & ERS_DSR_Y) dcb.fOutxDsrFlow=TRUE;
00180 if(data & ERS_DSR_N) dcb.fOutxDsrFlow=FALSE;
00181
00182
00183 d=(data & 0x30000000);
00184 if(d==ERS_X_Y){
00185 dcb.fTXContinueOnXoff=FALSE;
00186 dcb.fOutX=TRUE;
00187 dcb.fInX=TRUE;
00188 }else if(d==ERS_X_N){
00189 dcb.fTXContinueOnXoff=FALSE;
00190 dcb.fOutX=FALSE;
00191 dcb.fInX=FALSE;
00192 }
00193
00194 if (!SetCommState(ERSHCOMn, &dcb)) return 2;
00195 return 0;
00196 }
00197
00198
00199 int ERS_RecvTimeOut(int n, int rto)
00200 {
00201 COMMTIMEOUTS ct;
00202
00203 if(ers_check(n)) return 1;
00204 GetCommTimeouts(ERSHCOMn,&ct);
00205
00206 ct.ReadIntervalTimeout=rto;
00207 ct.ReadTotalTimeoutMultiplier=0;
00208 ct.ReadTotalTimeoutConstant=rto;
00209
00210 if(!SetCommTimeouts(ERSHCOMn,&ct)) return 2;
00211 return 0;
00212 }
00213
00214
00215 int ERS_SendTimeOut(int n, int sto)
00216 {
00217 COMMTIMEOUTS ct;
00218
00219 if(ers_check(n)) return 1;
00220 GetCommTimeouts(ERSHCOMn,&ct);
00221
00222 ct.WriteTotalTimeoutMultiplier=0;
00223 ct.WriteTotalTimeoutConstant=sto;
00224
00225 if(!SetCommTimeouts(ERSHCOMn,&ct)) return 2;
00226 return 0;
00227 }
00228
00229 #ifdef _WIN32_WCE
00230
00231
00232 int ERS_Open(int n, int recv_size, int send_size)
00233 {
00234 wchar_t comname[6];
00235
00236 if(n<1 || n>ERSLIBMAXPORT) return 1;
00237 if(ers_initdone[n-1]) return 2;
00238
00239 wsprintf(comname,L"COM%d:",n);
00240
00241 ERSHCOMn=CreateFile(comname,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
00242
00243
00244 if(ERSHCOMn==INVALID_HANDLE_VALUE) return 3;
00245
00246
00247 SetupComm(ERSHCOMn,recv_size,send_size);
00248
00249 ers_initdone[n-1]=1;
00250
00251
00252 ERS_Config(n,ERS_9600|ERS_1|ERS_NO|ERS_8|ERS_X_N|ERS_CTS_N|ERS_DSR_N|ERS_DTR_Y|ERS_RTS_Y);
00253 ERS_RecvTimeOut(n,1000);
00254 ERS_SendTimeOut(n,1000);
00255 return 0;
00256 }
00257 #else
00258
00259
00260 int ERS_Open(int n, int recv_size, int send_size)
00261 {
00262 wchar_t comname[11];
00263
00264 if(n<1 || n>ERSLIBMAXPORT) return 1;
00265 if(ers_initdone[n-1]) return 2;
00266
00267 swprintf_s(comname,11,L"\\\\.\\COM%d",n);
00268
00269 ERSHCOMn=CreateFile(comname,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
00270
00271
00272 if(ERSHCOMn==INVALID_HANDLE_VALUE) return 3;
00273
00274
00275 SetupComm(ERSHCOMn,recv_size,send_size);
00276
00277 ers_initdone[n-1]=1;
00278
00279
00280 ERS_Config(n,ERS_9600|ERS_1|ERS_NO|ERS_8|ERS_X_N|ERS_CTS_N|ERS_DSR_N|ERS_DTR_Y|ERS_RTS_Y);
00281 ERS_RecvTimeOut(n,1000);
00282 ERS_SendTimeOut(n,1000);
00283 return 0;
00284 }
00285 #endif
00286
00287
00288
00289 int ERS_Close(int n)
00290 {
00291 if(ers_check(n)) return 1;
00292 if(!CloseHandle(ERSHCOMn)) return 2;
00293 ers_initdone[n-1]=0;
00294 return 0;
00295 }
00296
00297
00298 void ERS_CloseAll(void)
00299 {
00300 int n;
00301 for(n=1;n<=ERSLIBMAXPORT;n++){
00302 if (ers_initdone[n-1]) ERS_Close(n);
00303 }
00304 }
00305
00306
00307 int ERS_CheckRecv(int n)
00308 {
00309 COMSTAT cs;
00310 DWORD err;
00311 if(ers_check(n)) return 0;
00312 ClearCommError(ERSHCOMn,&err,&cs);
00313 return cs.cbInQue;
00314 }
00315
00316
00317 int ERS_CheckSend(int n)
00318 {
00319 COMSTAT cs;
00320 DWORD err;
00321 if(ers_check(n)) return 0;
00322 ClearCommError(ERSHCOMn,&err,&cs);
00323 return cs.cbOutQue;
00324 }
00325
00326
00327 int ERS_Recv(int n, void *buf, int size)
00328 {
00329 DWORD m;
00330 if(ers_check(n)) return 0;
00331 ReadFile(ERSHCOMn,buf,size,&m,NULL);
00332 return m;
00333 }
00334
00335
00336 int ERS_Getc(int n)
00337 {
00338 DWORD m;
00339 int c=0;
00340 if(ers_check(n)) return 0;
00341 ReadFile(ERSHCOMn,&c,1,&m,NULL);
00342 if(!m)return EOF;
00343 return c;
00344 }
00345
00346
00347 wchar_t ERS_WGetc(int n)
00348 {
00349 DWORD m;
00350 wchar_t c=0;
00351 if(ers_check(n)) return 0;
00352 ReadFile(ERSHCOMn,&c,sizeof(wchar_t),&m,NULL);
00353 if(!m)return EOF;
00354 return c;
00355 }
00356
00357
00358
00359 int ERS_Gets(int n, char *s, int size)
00360 {
00361 DWORD m;
00362 int cnt=0;
00363 char c;
00364
00365 if(ers_check(n)) return 0;
00366 if(!size) return 0;
00367
00368 for(;;){
00369 ReadFile(ERSHCOMn,&c,1,&m,NULL);
00370 if(!m || c=='\r'|| c=='\n' || c=='\0')break;
00371 *s++=c;
00372 cnt++;
00373 if(size>0 && cnt>=size-1)break;
00374 }
00375 *s='\0';
00376 if(m)cnt++;
00377 return cnt;
00378 }
00379
00380
00381
00382 int ERS_WGets(int n, wchar_t *s, int size)
00383 {
00384 DWORD m;
00385 int cnt=0;
00386 wchar_t c;
00387
00388 if(ers_check(n)) return 0;
00389 if(!size) return 0;
00390
00391 for(;;){
00392 ReadFile(ERSHCOMn,&c,sizeof(wchar_t),&m,NULL);
00393 if(!m || c==L'\r'|| c==L'\n' || c==L'\0')break;
00394 *s++=c;
00395 cnt++;
00396 if(size>0 && cnt>=size-1)break;
00397 }
00398 *s=L'\0';
00399 if(m)cnt++;
00400 return cnt;
00401 }
00402
00403
00404 int ERS_Send(int n, void *buf, int size)
00405 {
00406 DWORD m;
00407 if(ers_check(n)) return 0;
00408 WriteFile(ERSHCOMn,buf,size,&m,NULL);
00409 return m;
00410 }
00411
00412
00413 int ERS_Putc(int n, int c)
00414 {
00415 DWORD m;
00416 if(ers_check(n)) return 0;
00417 WriteFile(ERSHCOMn,&c,1,&m,NULL);
00418 return m;
00419 }
00420
00421
00422 int ERS_WPutc(int n, wchar_t c)
00423 {
00424 DWORD m;
00425 if(ers_check(n)) return 0;
00426 WriteFile(ERSHCOMn,&c,sizeof(wchar_t),&m,NULL);
00427 return m/sizeof(wchar_t);
00428 }
00429
00430
00431
00432 int ERS_Puts(int n, char *s)
00433 {
00434 DWORD m;
00435 char c;
00436 int cnt=0;
00437
00438 if (ers_check(n)) return 0;
00439 while(c=*s++){
00440 WriteFile(ERSHCOMn,&c,1,&m,NULL);
00441 if(!m)return cnt;
00442 cnt++;
00443 }
00444 c='\n';
00445 WriteFile(ERSHCOMn,&c,1,&m,NULL);
00446 if(!m)return cnt;
00447 cnt++;
00448 return cnt;
00449 }
00450
00451
00452
00453 int ERS_WPuts(int n, wchar_t *s)
00454 {
00455 DWORD m;
00456 wchar_t c;
00457 int cnt=0;
00458
00459 if (ers_check(n)) return 0;
00460 while((c=*s++)){
00461 WriteFile(ERSHCOMn,&c,sizeof(wchar_t),&m,NULL);
00462 if(!m)return cnt;
00463 cnt++;
00464 }
00465 c=L'\n';
00466 WriteFile(ERSHCOMn,&c,sizeof(wchar_t),&m,NULL);
00467 if(!m)return cnt;
00468 cnt++;
00469 return cnt;
00470 }
00471
00472
00473 int ERS_Printf(int n, char *format, ...)
00474 {
00475 char buf[256];
00476 va_list vl;
00477
00478 va_start(vl,format);
00479 #ifdef _WIN32_WCE
00480 vsprintf(buf,format,vl);
00481 #else
00482 vsprintf_s(buf,sizeof(buf),format,vl);
00483 #endif
00484 return ERS_Puts(n,buf);
00485 }
00486
00487
00488 int ERS_WPrintf(int n, wchar_t *format, ...)
00489 {
00490 wchar_t buf[256];
00491 va_list vl;
00492
00493 va_start(vl,format);
00494
00495 #ifdef _WIN32_WCE
00496 vswprintf(buf,format,vl);
00497 #else
00498 vswprintf_s(buf,256,format,vl);
00499 #endif
00500
00501 return ERS_WPuts(n,buf);
00502 }
00503
00504
00505 int ERS_ClearRecv(int n)
00506 {
00507 if(ers_check(n)) return 1;
00508 PurgeComm(ERSHCOMn,PURGE_RXCLEAR);
00509 return 0;
00510 }
00511
00512
00513 int ERS_ClearSend(int n)
00514 {
00515 if(ers_check(n)) return 1;
00516 PurgeComm(ERSHCOMn,PURGE_TXCLEAR);
00517 return 0;
00518 }
00519
00520
00521 int ERS_BaudRate(int n, int baudrate)
00522 {
00523 DCB dcb;
00524
00525 if(ers_check(n)) return 1;
00526 GetCommState(ERSHCOMn,&dcb);
00527 dcb.BaudRate=baudrate;
00528 if (!SetCommState(ERSHCOMn, &dcb)) return 2;
00529 return 0;
00530 }
00531
00532 #ifndef _WIN32_WCE
00533
00534
00535 int ERS_ConfigDialog(int n)
00536 {
00537 COMMCONFIG cc;
00538 DWORD size;
00539 wchar_t comname[7];
00540
00541 if(ers_check(n)) return 1;
00542
00543 swprintf_s(comname,7,L"COM%d",n);
00544
00545 GetCommConfig(ERSHCOMn,&cc,&size);
00546 CommConfigDialog(comname,HWND_DESKTOP,&cc);
00547 if (!SetCommConfig(ERSHCOMn,&cc,sizeof(cc)))return 2;
00548 return 0;
00549 }
00550 #endif