00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00015
00034 #ifdef WIN32
00035 #include "StdAfx.h"
00036 #endif
00037
00038 #include "SGTramas.h"
00039
00040 #ifdef _DEBUG
00041 #undef THIS_FILE
00042 static char THIS_FILE[]=__FILE__;
00043 #define new DEBUG_NEW
00044 #endif
00045
00046
00048
00050
00062 CSGTramas::CSGTramas()
00063 {
00064
00065 }
00066
00067
00080 CSGTramas::~CSGTramas()
00081 {
00082
00083 }
00084
00085
00087
00088
00089
00090
00091
00093
00105 int CSGTramas::SGReset() {
00106
00107 unsigned char trama[2];
00108 int status;
00109 int iContTimeout=0;
00110 int nBytesRead=0;
00111
00112 trama[0]=TRAMA_RST_CAB;
00113 this->SendData((const char *)trama,1);
00114 char* lpBuffer = new char[10];
00115
00116 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00117 nBytesRead = this->ReadData(lpBuffer, 10);
00118 Sleep(10);
00119 iContTimeout+=10;
00120 }
00121
00122 if( nBytesRead == 0 ) {
00123 status=0;
00124 }
00125 else {
00126 if( lpBuffer[0] == TRAMA_RRST_CAB )
00127 status=1;
00128 else
00129 status=-1;
00130 }
00131
00132
00133 delete []lpBuffer;
00134 return status;
00135
00136 }
00137
00139
00140
00141
00142
00143
00145
00146
00157 int CSGTramas::SGIncrementAddress() {
00158
00159 unsigned char trama[2];
00160 int status;
00161 int iContTimeout=0;
00162 int nBytesRead=0;
00163
00164 trama[0]=TRAMA_INC_CAB;
00165 this->SendData((const char *)trama,1);
00166 char* lpBuffer = new char[10];
00167
00168 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00169 nBytesRead = this->ReadData(lpBuffer, 10);
00170 Sleep(10);
00171 iContTimeout+=10;
00172 }
00173
00174 if( nBytesRead == 0 ) {
00175 status=0;
00176 }
00177 else {
00178 if( lpBuffer[0] == TRAMA_RINC_CAB )
00179 status=1;
00180 else
00181 status=-1;
00182 }
00183
00184
00185 delete []lpBuffer;
00186 return status;
00187
00188 }
00189
00190
00192
00193
00194
00195
00196
00198
00212 int CSGTramas::SGReadData(int *valor) {
00213
00214 unsigned char trama[2];
00215 int status;
00216 int iContTimeout=0;
00217 int nBytesRead=0;
00218
00219 trama[0]=TRAMA_RD_CAB;
00220 this->SendData((const char *)trama,1);
00221 unsigned char* lpBuffer = new unsigned char[10];
00222
00223 memset(lpBuffer,0,10);
00224 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00225 nBytesRead = this->ReadData(lpBuffer, 10);
00226 Sleep(11);
00227 iContTimeout+=1;
00228 }
00229
00230 if( nBytesRead == 0 ) {
00231 status=0;
00232 }
00233 else {
00234 if( lpBuffer[0] == TRAMA_RRD_CAB ) {
00235 int bms=lpBuffer[1];
00236 int BMS=lpBuffer[2];
00237 *valor =(unsigned int) ((BMS<<8) | bms);
00238 status=1;
00239 }
00240 else
00241 status=-1;
00242 }
00243
00244
00245 delete []lpBuffer;
00246 return status;
00247
00248 }
00249
00251
00252
00253
00254
00255
00256
00258
00259
00271 int CSGTramas::SGLoadConfig() {
00272
00273 unsigned char trama[2];
00274 int status;
00275 int iContTimeout=0;
00276 int nBytesRead=0;
00277
00278 trama[0]=TRAMA_CONF_CAB;
00279 this->SendData((const char *)trama,1);
00280 char* lpBuffer = new char[10];
00281
00282 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00283 nBytesRead = this->ReadData(lpBuffer, 10);
00284 Sleep(10);
00285 iContTimeout+=10;
00286 }
00287
00288 if( nBytesRead == 0 ) {
00289 status=0;
00290 }
00291 else {
00292 if( lpBuffer[0] == TRAMA_RCONF_CAB )
00293 status=1;
00294 else
00295 status=-1;
00296 }
00297
00298
00299 delete []lpBuffer;
00300 return status;
00301
00302 }
00303
00304
00306
00307
00308
00309
00310
00312
00328 int CSGTramas::SGProg(int valor) {
00329
00330 unsigned char trama[4];
00331 int status;
00332 int iContTimeout=0;
00333 int nBytesRead=0;
00334
00335 trama[0]=TRAMA_PROG_CAB;
00336 trama[1]=(char)(valor & 0x00FF);
00337 trama[2]=(char)((valor>>8)&0x00FF);
00338
00339 this->SendData((const char *)trama,3);
00340 char* lpBuffer = new char[10];
00341
00342 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00343 nBytesRead = this->ReadData(lpBuffer, 10);
00344 Sleep(10);
00345 iContTimeout+=10;
00346 }
00347
00348 if( nBytesRead == 0 ) {
00349 status=0;
00350 }
00351 else {
00352 if( lpBuffer[0] == TRAMA_RPROG_CAB )
00353 status=1;
00354 else
00355 status=-1;
00356 }
00357
00358
00359 delete []lpBuffer;
00360 return status;
00361
00362 }
00363
00365
00366
00367
00368
00369
00371
00386 int CSGTramas::SGLoadData(int valor) {
00387
00388 unsigned char trama[2];
00389 int status;
00390 int iContTimeout=0;
00391 int nBytesRead=0;
00392
00393 trama[0]=TRAMA_DATA_CAB;
00394 trama[1]=(char)(valor & 0x00FF);
00395 trama[2]=(char)((valor>>8)&0x00FF);
00396
00397 this->SendData((const char *)trama,3);
00398 char* lpBuffer = new char[10];
00399
00400 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00401 nBytesRead = this->ReadData(lpBuffer, 10);
00402 Sleep(10);
00403 iContTimeout+=10;
00404 }
00405
00406 if( nBytesRead == 0 ) {
00407 status=0;
00408 }
00409 else {
00410 if( lpBuffer[0] == TRAMA_RDATA_CAB )
00411 status=1;
00412 else
00413 status=-1;
00414 }
00415
00416
00417 delete []lpBuffer;
00418 return status;
00419
00420 }
00421
00423
00424
00425
00426
00427
00429
00441 int CSGTramas::SGBeginPEC() {
00442
00443 unsigned char trama[2];
00444 int status;
00445 int iContTimeout=0;
00446 int nBytesRead=0;
00447
00448 trama[0]=TRAMA_BEGIN_CAB;
00449 this->SendData((const char *)trama,1);
00450 char* lpBuffer = new char[10];
00451
00452 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00453 nBytesRead = this->ReadData(lpBuffer, 10);
00454 Sleep(10);
00455 iContTimeout+=10;
00456 }
00457
00458 if( nBytesRead == 0 ) {
00459 status=0;
00460 }
00461 else {
00462 if( lpBuffer[0] == TRAMA_RBEG_CAB )
00463 status=1;
00464 else
00465 status=-1;
00466 }
00467
00468
00469 delete []lpBuffer;
00470 return status;
00471
00472 }
00473
00475
00476
00477
00478
00479
00481
00482
00493 int CSGTramas::SGEndp() {
00494
00495 unsigned char trama[2];
00496 int status;
00497 int iContTimeout=0;
00498 int nBytesRead=0;
00499
00500 trama[0]=TRAMA_SENDP_CAB;
00501 this->SendData((const char *)trama,1);
00502 char* lpBuffer = new char[10];
00503
00504 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00505 nBytesRead = this->ReadData(lpBuffer, 10);
00506 Sleep(10);
00507 iContTimeout+=10;
00508 }
00509
00510 if( nBytesRead == 0 ) {
00511 status=0;
00512 }
00513 else {
00514 if( lpBuffer[0] == TRAMA_RENDP_CAB )
00515 status=1;
00516 else
00517 status=-1;
00518 }
00519
00520
00521 delete []lpBuffer;
00522 return status;
00523
00524 }
00525
00526
00528
00529
00530
00531
00532
00534
00547 int CSGTramas::SGBorrarPic() {
00548
00549 unsigned char trama[2];
00550 int status;
00551 int iContTimeout=0;
00552 int nBytesRead=0;
00553
00554 trama[0]=TRAMA_SBORRAR_CAB;
00555 this->SendData((const char *)trama,1);
00556 char* lpBuffer = new char[10];
00557
00558 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00559 nBytesRead = this->ReadData(lpBuffer, 10);
00560 Sleep(10);
00561 iContTimeout+=10;
00562 }
00563
00564 if( nBytesRead == 0 ) {
00565 status=0;
00566 }
00567 else {
00568 if( lpBuffer[0] == TRAMA_RBORRAR_CAB )
00569 status=1;
00570 else
00571 status=-1;
00572 }
00573
00574
00575 delete []lpBuffer;
00576 return status;
00577
00578 }
00579
00580
00582
00583
00584
00585
00586
00588
00604 int CSGTramas::SGProgBloque(int *bloque) {
00605
00606 unsigned char trama[17];
00607 int status;
00608 int iContTimeout=0;
00609 int nBytesRead=0;
00610 int cont=1,i;
00611
00612 if( bloque == NULL )
00613 return -1;
00614
00615 trama[0]=TRAMA_SPROG_BLOQUE;
00616 for(i=0;i<8;i++) {
00617 trama[cont]= (char)(bloque[i] & 0x00FF);
00618 cont++;
00619 trama[cont]= (char)((bloque[i]>>8)&0x00FF);
00620 cont++;
00621 }
00622
00623 this->SendData((const char *)trama,17);
00624 char* lpBuffer = new char[10];
00625
00626 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00627 nBytesRead = this->ReadData(lpBuffer, 10);
00628 Sleep(1);
00629 iContTimeout+=1;
00630 }
00631
00632 if( nBytesRead == 0 ) {
00633 status=0;
00634 }
00635 else {
00636 if( lpBuffer[0] == TRAMA_RPROG_BLOQUE )
00637 status=1;
00638 else
00639 status=-1;
00640 }
00641
00642
00643 delete []lpBuffer;
00644 return status;
00645
00646 }
00647
00649
00650
00651
00652
00653
00655
00656
00673 int CSGTramas::SGStore(int dir, int valor) {
00674
00675 unsigned char trama[4];
00676 int status;
00677 int iContTimeout=0;
00678 int nBytesRead=0;
00679
00680 trama[0]=TRAMA_STORE_CAB;
00681 trama[1]=(dir & 0x00FF);
00682 trama[2]=(char)((dir>>8)&0x00FF);
00683 trama[3]=(char)valor;
00684
00685 this->SendData((const char *)trama,4);
00686 char* lpBuffer = new char[10];
00687
00688 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00689 nBytesRead = this->ReadData(lpBuffer, 10);
00690 Sleep(10);
00691 iContTimeout+=10;
00692 }
00693
00694 if( nBytesRead == 0 ) {
00695 status=0;
00696 }
00697 else {
00698 if( lpBuffer[0] == TRAMA_RSTORE_CAB )
00699 status=1;
00700 else
00701 status=-1;
00702 }
00703
00704
00705 delete []lpBuffer;
00706 return status;
00707
00708 }
00709
00710
00712
00713
00714
00715
00716
00718
00719
00736 int CSGTramas::SGLoad(int dir, int *valor) {
00737
00738 unsigned char trama[3];
00739 int status;
00740 int iContTimeout=0;
00741 int nBytesRead=0;
00742
00743 trama[0]=TRAMA_LOAD_CAB;
00744 trama[1]=(dir & 0x00FF);
00745 trama[2]=(char)((dir>>8)&0x00FF);
00746
00747 this->SendData((const char *)trama,3);
00748 char* lpBuffer = new char[10];
00749
00750 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00751 nBytesRead = this->ReadData(lpBuffer, 10);
00752 Sleep(10);
00753 iContTimeout+=10;
00754 }
00755
00756 if( nBytesRead == 0 ) {
00757 status=0;
00758 }
00759 else {
00760 if( lpBuffer[0] == TRAMA_RLOAD_CAB ) {
00761 status=1;
00762 *valor=lpBuffer[1];
00763 }
00764 else
00765 status=-1;
00766 }
00767
00768
00769 delete []lpBuffer;
00770 return status;
00771
00772 }
00773
00774
00776
00777
00778
00779
00780
00781
00783
00784
00799 int CSGTramas::SGLoadDataEE(int valor) {
00800
00801 unsigned char trama[2];
00802 int status;
00803 int iContTimeout=0;
00804 int nBytesRead=0;
00805
00806 trama[0]=TRAMA_LEE_CAB;
00807 trama[1]=(char)(valor & 0x00FF);
00808
00809
00810 this->SendData((const char *)trama,2);
00811 char* lpBuffer = new char[10];
00812
00813 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00814 nBytesRead = this->ReadData(lpBuffer, 10);
00815 Sleep(10);
00816 iContTimeout+=10;
00817 }
00818
00819 if( nBytesRead == 0 ) {
00820 status=0;
00821 }
00822 else {
00823 if( lpBuffer[0] == TRAMA_RLEE_CAB )
00824 status=1;
00825 else
00826 status=-1;
00827 }
00828
00829
00830 delete []lpBuffer;
00831 return status;
00832
00833
00834 }
00835
00836
00838
00839
00840
00841
00842
00843
00845
00846
00862 int CSGTramas::SGReadDataEE(int *valor) {
00863
00864 unsigned char trama[2];
00865 int status;
00866 int iContTimeout=0;
00867 int nBytesRead=0;
00868
00869 trama[0]=TRAMA_RDEE_CAB;
00870 this->SendData((const char *)trama,1);
00871 unsigned char* lpBuffer = new unsigned char[10];
00872
00873 memset(lpBuffer,0,10);
00874 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00875 nBytesRead = this->ReadData(lpBuffer, 10);
00876 Sleep(11);
00877 iContTimeout+=1;
00878 }
00879
00880 if( nBytesRead == 0 ) {
00881 status=0;
00882 }
00883 else {
00884 if( lpBuffer[0] == TRAMA_RRDEE_CAB ) {
00885 *valor = lpBuffer[1];
00886 status=1;
00887 }
00888 else
00889 status=-1;
00890 }
00891
00892
00893 delete []lpBuffer;
00894 return status;
00895
00896
00897 }
00898
00900
00901
00902
00903
00904
00906
00907
00918 int CSGTramas::SGPing() {
00919
00920 unsigned char trama[2];
00921 int status;
00922 int iContTimeout=0;
00923 int nBytesRead=0;
00924
00925 trama[0]=TRAMA_PING_CAB;
00926
00927 this->SendData((const char *)trama,1);
00928 unsigned char* lpBuffer = new unsigned char[10];
00929
00930 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00931 nBytesRead = this->ReadData(lpBuffer, 10);
00932 Sleep(10);
00933 iContTimeout+=10;
00934 }
00935
00936 if( nBytesRead == 0 ) {
00937 status=0;
00938 }
00939 else {
00940 if( lpBuffer[0] == TRAMA_PONG_CAB )
00941 status=1;
00942 else
00943 status=-1;
00944 }
00945
00946
00947 delete []lpBuffer;
00948 return status;
00949
00950 }
00951
00953
00954
00955
00956
00957
00959
00960
00979 int CSGTramas::SGId(unsigned char *is, unsigned char *im, unsigned char *ipv) {
00980
00981 unsigned char trama[2];
00982 int status;
00983 int iContTimeout=0;
00984 int nBytesRead=0;
00985
00986
00987 *is=0;
00988 *im=0;
00989 *ipv=0;
00990
00991 trama[0]=TRAMA_ID_CAB;
00992
00993 this->SendData((const char *)trama,1);
00994 char* lpBuffer = new char[10];
00995
00996 while( iContTimeout < TIMEOUT && nBytesRead == 0 ) {
00997 nBytesRead = this->ReadData(lpBuffer, 10);
00998 Sleep(10);
00999 iContTimeout+=10;
01000 }
01001
01002 if( nBytesRead == 0 ) {
01003 status=0;
01004 }
01005 else {
01006 if( lpBuffer[0] == TRAMA_RID_CAB ) {
01007 status=1;
01008 *is=lpBuffer[1];
01009 *im=lpBuffer[2];
01010 *ipv=lpBuffer[3];
01011 }
01012 else
01013 status=-1;
01014 }
01015
01016
01017 delete []lpBuffer;
01018 return status;
01019
01020 }
01021
01022
01023 void CSGTramas::SGTramasIPVtoString(unsigned char im, unsigned char ipv, char *cad) {
01024
01025 int placa;
01026
01027
01028
01029 if ((ipv&0xF0) == 0) {
01030 strcpy(cad,PLACA_USER_CAD);
01031 return;
01032 }
01033
01034
01035
01036 placa = ((im<<4)&0xFF0) | ((ipv>>4)&0x0F);
01037
01038 switch(placa) {
01039 case PLACA_XPIC:
01040 strcpy(cad,PLACA_XPIC_CAD);
01041 break;
01042 case PLACA_CT6811:
01043 strcpy(cad,PLACA_CT6811_CAD);
01044 break;
01045 case PLACA_GPBOT:
01046 strcpy(cad,PLACA_GPBOT_CAD);
01047 break;
01048 case PLACA_SKYPIC:
01049 strcpy(cad,PLACA_SKYPIC_CAD);
01050 break;
01051 default:
01052 strcpy(cad,"XXX");
01053 }
01054
01055 }
01056
01057 void CSGTramas::SGTramasIMtoString(unsigned char im, char *cad) {
01058
01059 switch(im) {
01060 case MICRO_PIC16F877A:
01061 strcpy(cad,MICRO_PIC16F877A_CAD);
01062 break;
01063 case MICRO_68HC11E2:
01064 strcpy(cad,MICRO_68HC11E2_CAD);
01065 break;
01066 case MICRO_68HC08:
01067 strcpy(cad,MICRO_68HC08_CAD);
01068 break;
01069 case MICRO_PIC16F876:
01070 strcpy(cad,MICRO_PIC16F876_CAD);
01071 break;
01072 default:
01073 strcpy(cad,"XXXX");
01074
01075 }
01076
01077 }
01078
01079 void CSGTramas::SGTramasIStoString(unsigned char is, char *cad)
01080 {
01081 switch(is) {
01082 case SG_NULL:
01083 strcpy(cad,SG_NULL_CAD);
01084 break;
01085 case SG_GENERIC:
01086 strcpy(cad,SG_GENERIC_CAD);
01087 break;
01088 case SG_SERVOS8:
01089 strcpy(cad,SG_SERVOS8_CAD);
01090 break;
01091 case SG_PICP:
01092 strcpy(cad,SG_PICP_CAD);
01093 break;
01094 default:
01095 strcpy(cad,"XXXX");
01096 }
01097 }
01098
01099