teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Thu May 24 00:42:23 2018 +0000
Revision:
29:823a9da3696b
Parent:
25:a6da63ed025b
Child:
30:8a06a85d8807
Vers?o com reset e bootloader corrigido aparentemente 23/05/2018

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brunofgc 0:1c0a769988ee 1 #include "diversos.h"
brunofgc 18:1eefda1f7736 2 // - 09/01/2018
brunofgc 0:1c0a769988ee 3 bool eventosRTC::segundos;
brunofgc 0:1c0a769988ee 4 bool eventosRTC::minutos;
brunofgc 0:1c0a769988ee 5 bool eventosRTC::rotina10Segundos;
brunofgc 18:1eefda1f7736 6 char eventosRTC::rotinaEnvioDeDados;
brunofgc 0:1c0a769988ee 7 bool eventosRTC::rotina1hora;
brunofgc 0:1c0a769988ee 8 bool eventosRTC::rotina1segundo;
brunofgc 0:1c0a769988ee 9 WatchdogTimer diversos::wdt;
brunofgc 0:1c0a769988ee 10 char commands::buffer[tamBufferCommands];
brunofgc 0:1c0a769988ee 11
brunofgc 0:1c0a769988ee 12
brunofgc 0:1c0a769988ee 13 uint32_t diversos::memAvailable(){
brunofgc 0:1c0a769988ee 14 char stackVariable;
brunofgc 0:1c0a769988ee 15 char *heap;
brunofgc 0:1c0a769988ee 16 uint32_t result;
brunofgc 0:1c0a769988ee 17 heap = (char*)malloc(1);
brunofgc 0:1c0a769988ee 18 result = (uint32_t) ((&stackVariable) - heap);
brunofgc 0:1c0a769988ee 19 free(heap);
brunofgc 0:1c0a769988ee 20 return result;
brunofgc 0:1c0a769988ee 21 }
brunofgc 0:1c0a769988ee 22
brunofgc 0:1c0a769988ee 23 void WatchdogTimer::kick(float s) {
brunofgc 0:1c0a769988ee 24 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
brunofgc 0:1c0a769988ee 25 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
brunofgc 0:1c0a769988ee 26 LPC_WDT->WDTC = s * (float)clk;
brunofgc 0:1c0a769988ee 27 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
brunofgc 0:1c0a769988ee 28 kick();
brunofgc 0:1c0a769988ee 29 }
brunofgc 0:1c0a769988ee 30 // "kick" or "feed" the dog - reset the watchdog timer
brunofgc 0:1c0a769988ee 31 // by writing this required bit pattern
brunofgc 0:1c0a769988ee 32 void WatchdogTimer::kick() {
brunofgc 0:1c0a769988ee 33 LPC_WDT->WDFEED = 0xAA;
brunofgc 0:1c0a769988ee 34 LPC_WDT->WDFEED = 0x55;
brunofgc 0:1c0a769988ee 35 }
brunofgc 0:1c0a769988ee 36
brunofgc 0:1c0a769988ee 37 /*double diversos::stringToFloat(char *p,uint16_t max){
brunofgc 0:1c0a769988ee 38 uint16_t caractere=0;
brunofgc 0:1c0a769988ee 39 double iPart=0.0,dPart=0.0,f=0.0,mult=0.1;
brunofgc 0:1c0a769988ee 40 while(*p != '.') {
brunofgc 0:1c0a769988ee 41 caractere++;
brunofgc 0:1c0a769988ee 42 if(caractere>(max-1)){return 0.0;}
brunofgc 0:1c0a769988ee 43 iPart = (iPart*10) + (double)((*(p++)-'0'));
brunofgc 0:1c0a769988ee 44 }
brunofgc 0:1c0a769988ee 45 p++;
brunofgc 0:1c0a769988ee 46 while(*p!= '\0') {
brunofgc 0:1c0a769988ee 47 if(caractere>(max-1)){return 0.0;}
brunofgc 0:1c0a769988ee 48 dPart += (double)(((*(p++)-'0'))*mult);
brunofgc 0:1c0a769988ee 49 mult /= 10;
brunofgc 0:1c0a769988ee 50 }
brunofgc 0:1c0a769988ee 51 f = iPart + dPart;
brunofgc 0:1c0a769988ee 52 return f;
brunofgc 0:1c0a769988ee 53 }*/
brunofgc 0:1c0a769988ee 54
brunofgc 0:1c0a769988ee 55 /*double diversos::stringToFloat(char *p,uint16_t max){
brunofgc 0:1c0a769988ee 56 uint16_t caractere=0;
brunofgc 0:1c0a769988ee 57 double iPart=0.0,dPart=0.0,f=0.0,mult=0.1;
brunofgc 0:1c0a769988ee 58 while(p[caractere] != '.'){
brunofgc 0:1c0a769988ee 59 caractere++;
brunofgc 0:1c0a769988ee 60 if(caractere>(max-1)){return 0.0;}
brunofgc 0:1c0a769988ee 61 iPart = (iPart*10) + (((uint8_t)p[caractere])-30);
brunofgc 0:1c0a769988ee 62 }
brunofgc 0:1c0a769988ee 63 caractere++;
brunofgc 0:1c0a769988ee 64 while(p[caractere]!= '\0'){
brunofgc 0:1c0a769988ee 65 if(caractere>(max-1)){return 0.0;}
brunofgc 0:1c0a769988ee 66 dPart += (((uint8_t)p[caractere])-30)*mult;
brunofgc 0:1c0a769988ee 67 mult /= 10;
brunofgc 0:1c0a769988ee 68 }
brunofgc 0:1c0a769988ee 69 f = iPart + dPart;
brunofgc 0:1c0a769988ee 70 return f;
brunofgc 0:1c0a769988ee 71 }*/
brunofgc 0:1c0a769988ee 72
brunofgc 3:9598af355293 73 void diversos::strReplace(char *str, char *find, char *replace){
brunofgc 3:9598af355293 74 char aux[100];
brunofgc 3:9598af355293 75 char *ptr;
brunofgc 3:9598af355293 76 char replaced = 0;
brunofgc 3:9598af355293 77 char maxIterations = 0;
brunofgc 3:9598af355293 78 replaced = 0;
brunofgc 3:9598af355293 79 strcpy(aux,"");
brunofgc 3:9598af355293 80 ptr = strtok(str,find);
brunofgc 3:9598af355293 81 while((ptr!=NULL)&&(maxIterations<9)){
brunofgc 3:9598af355293 82 replaced = 1;
brunofgc 3:9598af355293 83 maxIterations++;
brunofgc 3:9598af355293 84 strcat(aux,ptr);
brunofgc 3:9598af355293 85 strcat(aux,replace);
brunofgc 3:9598af355293 86 ptr = strtok(NULL,find);
brunofgc 3:9598af355293 87 pc.printf("Tirando <%s> em <%s>.\r\n",find,str);
brunofgc 3:9598af355293 88 }
brunofgc 3:9598af355293 89 if(replaced){
brunofgc 3:9598af355293 90 aux[strlen(aux)-1]=0;
brunofgc 3:9598af355293 91 strcpy(str,aux);
brunofgc 3:9598af355293 92 }
brunofgc 3:9598af355293 93 //--------------------
brunofgc 3:9598af355293 94 }
brunofgc 3:9598af355293 95
brunofgc 25:a6da63ed025b 96 bool isxdigit(char c){
brunofgc 25:a6da63ed025b 97 if((c>47)&&(c<58)){return true;}
brunofgc 25:a6da63ed025b 98 if((c>64)&&(c<71)){return true;}
brunofgc 25:a6da63ed025b 99 if((c>96)&&(c<103)){return true;}
brunofgc 25:a6da63ed025b 100 return false;
brunofgc 25:a6da63ed025b 101 }
brunofgc 25:a6da63ed025b 102
brunofgc 25:a6da63ed025b 103 void diversos::urldecode2(char *dst, const char *src){
brunofgc 25:a6da63ed025b 104 char a, b;
brunofgc 25:a6da63ed025b 105 while (*src) {
brunofgc 25:a6da63ed025b 106 if ((*src == '%') &&
brunofgc 25:a6da63ed025b 107 ((a = src[1]) && (b = src[2])) &&
brunofgc 25:a6da63ed025b 108 (isxdigit(a) && isxdigit(b))) {
brunofgc 25:a6da63ed025b 109 if (a >= 'a')
brunofgc 25:a6da63ed025b 110 a -= 'a'-'A';
brunofgc 25:a6da63ed025b 111 if (a >= 'A')
brunofgc 25:a6da63ed025b 112 a -= ('A' - 10);
brunofgc 25:a6da63ed025b 113 else
brunofgc 25:a6da63ed025b 114 a -= '0';
brunofgc 25:a6da63ed025b 115 if (b >= 'a')
brunofgc 25:a6da63ed025b 116 b -= 'a'-'A';
brunofgc 25:a6da63ed025b 117 if (b >= 'A')
brunofgc 25:a6da63ed025b 118 b -= ('A' - 10);
brunofgc 25:a6da63ed025b 119 else
brunofgc 25:a6da63ed025b 120 b -= '0';
brunofgc 25:a6da63ed025b 121 *dst++ = 16*a+b;
brunofgc 25:a6da63ed025b 122 src+=3;
brunofgc 25:a6da63ed025b 123 } else if (*src == '+') {
brunofgc 25:a6da63ed025b 124 *dst++ = ' ';
brunofgc 25:a6da63ed025b 125 src++;
brunofgc 25:a6da63ed025b 126 } else {
brunofgc 25:a6da63ed025b 127 *dst++ = *src++;
brunofgc 25:a6da63ed025b 128 }
brunofgc 25:a6da63ed025b 129 }
brunofgc 25:a6da63ed025b 130 *dst++ = '\0';
brunofgc 25:a6da63ed025b 131 }
brunofgc 25:a6da63ed025b 132
brunofgc 4:13ff9c81dc10 133 void diversos::processaPulsosEDs(){
brunofgc 6:d4ebbaaba295 134 if(ED1){pulsosEDs[0]++;}
brunofgc 6:d4ebbaaba295 135 if(ED2){pulsosEDs[1]++;}
brunofgc 6:d4ebbaaba295 136 if(ED3){pulsosEDs[2]++;}
brunofgc 4:13ff9c81dc10 137 }
brunofgc 4:13ff9c81dc10 138
brunofgc 0:1c0a769988ee 139 void diversos::progressBar(uint32_t progresso,uint32_t total){
brunofgc 0:1c0a769988ee 140 int i,j;
brunofgc 0:1c0a769988ee 141 char maxTam = 20;
brunofgc 0:1c0a769988ee 142 char engrenagem[4]={179,0x2F,0x2D,0x5C};
brunofgc 0:1c0a769988ee 143
brunofgc 0:1c0a769988ee 144 j=((double)progresso/(double)total)*maxTam;
brunofgc 0:1c0a769988ee 145 pc.printf("%c%c%c<",179,engrenagem[(unsigned int)progresso%4],179);
brunofgc 0:1c0a769988ee 146 for(i=0;i<maxTam;i++){
brunofgc 0:1c0a769988ee 147 if(i<=j){
brunofgc 0:1c0a769988ee 148 pc.printf("%c",178);
brunofgc 0:1c0a769988ee 149 }else{
brunofgc 0:1c0a769988ee 150 pc.printf("%c",176);
brunofgc 0:1c0a769988ee 151 }
brunofgc 0:1c0a769988ee 152 }
brunofgc 0:1c0a769988ee 153 pc.printf("> %3.0f%% Progresso %5lu de %5lu\r ",((double)progresso/(double)total)*100,progresso,total);
brunofgc 0:1c0a769988ee 154 }
brunofgc 0:1c0a769988ee 155
brunofgc 13:b9183b4bc049 156
brunofgc 0:1c0a769988ee 157 void commands::exec(uint8_t idConnection){
brunofgc 0:1c0a769988ee 158 char *ptr,*pCharFile,*ptrComando;
brunofgc 0:1c0a769988ee 159 char msg[100];
brunofgc 0:1c0a769988ee 160 uint16_t currentCur,totalLengthCommands;
brunofgc 0:1c0a769988ee 161 static uint16_t fileCheckSum16BIT,checkSum16BIT_Lido;
brunofgc 7:ae9c47f62946 162
brunofgc 0:1c0a769988ee 163 totalLengthCommands = strlen(commands::buffer);
brunofgc 0:1c0a769988ee 164 ptrComando = strtok(commands::buffer,";");
brunofgc 0:1c0a769988ee 165
brunofgc 0:1c0a769988ee 166 do{
brunofgc 2:55b7b466e742 167
brunofgc 2:55b7b466e742 168
brunofgc 2:55b7b466e742 169
brunofgc 0:1c0a769988ee 170 ptr=strstr(ptrComando,"DELETE_SENT_FILES");
brunofgc 11:631bea162800 171 if(ptr) {
brunofgc 0:1c0a769988ee 172 sdCard::deleteSentFiles = true;
brunofgc 0:1c0a769988ee 173 }
brunofgc 0:1c0a769988ee 174
brunofgc 0:1c0a769988ee 175 ptr=strstr(ptrComando,"SET_RTC:");
brunofgc 0:1c0a769988ee 176 if(ptr) {
brunofgc 0:1c0a769988ee 177 ptr = strtok(ptr,":");
brunofgc 0:1c0a769988ee 178 ptr = strtok(NULL,";");
brunofgc 0:1c0a769988ee 179 set_time(atoi(ptr));
brunofgc 0:1c0a769988ee 180 time_t seconds = time(NULL);
brunofgc 0:1c0a769988ee 181 pc.printf("Horario atualizado pelo server %s",ctime(&seconds));
brunofgc 0:1c0a769988ee 182 }
brunofgc 0:1c0a769988ee 183
brunofgc 0:1c0a769988ee 184 ptr=strstr(ptrComando,"sendArmazenamento");
brunofgc 0:1c0a769988ee 185 if(ptr) {
brunofgc 18:1eefda1f7736 186 eventosRTC::rotinaEnvioDeDados=maxTentativasEnvioDados;
brunofgc 29:823a9da3696b 187 sprintf(msg,"sendArmazenamento_ack");
brunofgc 29:823a9da3696b 188 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 29:823a9da3696b 189 }
brunofgc 29:823a9da3696b 190
brunofgc 29:823a9da3696b 191 ptr=strstr(ptrComando,"deleteArmazenamento");
brunofgc 29:823a9da3696b 192 if(ptr) {
brunofgc 29:823a9da3696b 193 sdCard::deleteBanks(2);
brunofgc 29:823a9da3696b 194 sprintf(msg,"deleteArmazenamento_ack");
brunofgc 29:823a9da3696b 195 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 196 }
brunofgc 0:1c0a769988ee 197
brunofgc 0:1c0a769988ee 198 ptr=strstr(ptrComando,"StartSendFile");
brunofgc 0:1c0a769988ee 199 if(ptr){
brunofgc 0:1c0a769988ee 200 modemCom::status.timeOut=100;
brunofgc 13:b9183b4bc049 201 sdCard::nBytesArquivoRecebidos = 0;
brunofgc 14:c0162ab2a951 202 sdCard::checkSum = 0;
brunofgc 0:1c0a769988ee 203 strtok(ptr,",");//Descartando inicio
brunofgc 0:1c0a769988ee 204 pCharFile = strtok(NULL,",");//Selecionando nome do arquivo
brunofgc 0:1c0a769988ee 205 strcpy(sdCard::nomeArquivoEmRecebimento,pCharFile);
brunofgc 0:1c0a769988ee 206 pCharFile = strtok(NULL,";");//Selecionando string checkSum16BIT
brunofgc 0:1c0a769988ee 207 fileCheckSum16BIT = atoi(pCharFile);
brunofgc 0:1c0a769988ee 208 remove("/sd/RAD/tempFile.bin");
brunofgc 0:1c0a769988ee 209 pc.printf("Iniciando recepcao do server arquivo deviceCfg.\n");
brunofgc 29:823a9da3696b 210 modemCom::status.recebendoArquivoDoServer = 600;
brunofgc 0:1c0a769988ee 211
brunofgc 22:cb832a9bc704 212
brunofgc 22:cb832a9bc704 213
brunofgc 29:823a9da3696b 214 /*
brunofgc 22:cb832a9bc704 215 sprintf(msg,"AT+CIPSEND=%u,%lu\r\n",idConnection,10);
brunofgc 22:cb832a9bc704 216 if(modemCom::sendToModem(msg,1,&modemCom::status.OK,NULL,20,3,1000)){
brunofgc 22:cb832a9bc704 217 modemCom::sendToModem("sendData\r\n",0,NULL,NULL,20,3,1000);
brunofgc 22:cb832a9bc704 218 }
brunofgc 29:823a9da3696b 219 */
brunofgc 29:823a9da3696b 220 sprintf(msg,"sendData\r\n");
brunofgc 29:823a9da3696b 221 modemCom::cipSend(idConnection,msg,10);
brunofgc 22:cb832a9bc704 222 /*
brunofgc 0:1c0a769988ee 223 modem.puts(modemCom::bufIn);
brunofgc 0:1c0a769988ee 224 osDelay(100);
brunofgc 22:cb832a9bc704 225 modem.puts("sendData\r\n");*/
brunofgc 0:1c0a769988ee 226
brunofgc 22:cb832a9bc704 227 //pc.printf("sendData\n");
brunofgc 29:823a9da3696b 228 modemCom::timeOutModem = 150;
brunofgc 0:1c0a769988ee 229 return;
brunofgc 0:1c0a769988ee 230 }
brunofgc 0:1c0a769988ee 231
brunofgc 0:1c0a769988ee 232 ptr=strstr(ptrComando,"StopSendFile");
brunofgc 0:1c0a769988ee 233 if(ptr){
brunofgc 14:c0162ab2a951 234 pc.printf("Finalizando recepcao de arquivo.\n");
brunofgc 14:c0162ab2a951 235 checkSum16BIT_Lido = sdCard::checkSum;//sdCard::calcCheckSum16BITFile("/sd/RAD/tempFile.bin");
brunofgc 14:c0162ab2a951 236 pc.printf("Feito o calculo de CRC = %lu.\n",checkSum16BIT_Lido);
brunofgc 0:1c0a769988ee 237 if(fileCheckSum16BIT == checkSum16BIT_Lido){
brunofgc 29:823a9da3696b 238 if(sdCard::file_rename("/sd/RAD/tempFile.bin",sdCard::nomeArquivoEmRecebimento)){
brunofgc 0:1c0a769988ee 239 sprintf(msg,"File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido);
brunofgc 0:1c0a769988ee 240 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 241 pc.printf("File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido);
brunofgc 0:1c0a769988ee 242
brunofgc 21:b9315cdd9275 243 if(strstr(sdCard::nomeArquivoEmRecebimento,"config.bin")!=NULL){
brunofgc 21:b9315cdd9275 244 pc.printf("Resetando o drome.\r\n");
brunofgc 21:b9315cdd9275 245 hardwareReset=true;
brunofgc 21:b9315cdd9275 246 }
brunofgc 21:b9315cdd9275 247
brunofgc 0:1c0a769988ee 248 if(strstr(sdCard::nomeArquivoEmRecebimento,"devices.cfg")!=NULL){
brunofgc 10:263c093f8977 249 sdCard::deleteBanks(2);
brunofgc 0:1c0a769988ee 250 pc.printf("Resetando o drome.\r\n");
brunofgc 0:1c0a769988ee 251 hardwareReset=true;
brunofgc 0:1c0a769988ee 252 }
brunofgc 0:1c0a769988ee 253
brunofgc 13:b9183b4bc049 254 if(strstr(sdCard::nomeArquivoEmRecebimento,"firmware.bin")!=NULL){
brunofgc 14:c0162ab2a951 255 pc.printf("Chamando bootloader.\r\n");
brunofgc 13:b9183b4bc049 256 callBootLoader = true;
brunofgc 13:b9183b4bc049 257 }
brunofgc 0:1c0a769988ee 258 }else{
brunofgc 0:1c0a769988ee 259 //modemCom::sendBufferCommandMode(2,"FileCorrupted.\r\n",strlen("FileCorrupted.\r\n"));
brunofgc 4:13ff9c81dc10 260 sprintf(msg,"erro");
brunofgc 0:1c0a769988ee 261 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 262 pc.printf("Recebido correto mas n foi possivel renomear arquivo.\r\n");
brunofgc 17:9b0eecbacbaa 263 }
brunofgc 0:1c0a769988ee 264 }else{
brunofgc 0:1c0a769988ee 265 /*sprintf(diversos::msg,"File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido);
brunofgc 0:1c0a769988ee 266 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 20:da1b8d80ba00 267 sprintf(msg,"erro");
brunofgc 0:1c0a769988ee 268 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 269 pc.printf("File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido);
brunofgc 0:1c0a769988ee 270 }
brunofgc 29:823a9da3696b 271 modemCom::status.recebendoArquivoDoServer = 0;
brunofgc 9:cf406384efd9 272 modemCom::timeOutModem = 50;
brunofgc 29:823a9da3696b 273 modemCom::status.timeOut = 30;
brunofgc 0:1c0a769988ee 274 }
brunofgc 0:1c0a769988ee 275
brunofgc 0:1c0a769988ee 276 /*ptr=strstr(ptrComando,"requestFile,");
brunofgc 0:1c0a769988ee 277 if(ptr){
brunofgc 0:1c0a769988ee 278 FILE *fp;
brunofgc 0:1c0a769988ee 279 char c;
brunofgc 0:1c0a769988ee 280 strtok(ptr,",");
brunofgc 0:1c0a769988ee 281 pCharFile = strtok(NULL,";");
brunofgc 0:1c0a769988ee 282
brunofgc 0:1c0a769988ee 283 fp = fopen(pCharFile,"r");
brunofgc 0:1c0a769988ee 284 if(fp!=NULL){
brunofgc 0:1c0a769988ee 285 do{
brunofgc 0:1c0a769988ee 286 c = fgetc(fp);
brunofgc 0:1c0a769988ee 287 if(!feof(fp)){modem.printf("%c",c);}
brunofgc 0:1c0a769988ee 288 modemCom::status.timeOut=10;
brunofgc 0:1c0a769988ee 289 }while(!feof(fp));
brunofgc 0:1c0a769988ee 290 fclose(fp);
brunofgc 0:1c0a769988ee 291 }else{
brunofgc 0:1c0a769988ee 292 modem.printf("Nao foi possivel abrir o arquivo.\n");
brunofgc 0:1c0a769988ee 293 }
brunofgc 0:1c0a769988ee 294 }*/
brunofgc 0:1c0a769988ee 295
brunofgc 1:0e0967c88590 296 ptr=strstr(ptrComando,"execAct:");
brunofgc 1:0e0967c88590 297 if(ptr){
brunofgc 1:0e0967c88590 298 char *pChar;
brunofgc 1:0e0967c88590 299 uint32_t actId;
brunofgc 1:0e0967c88590 300 pChar = strtok(ptr,":");
brunofgc 1:0e0967c88590 301 pChar = strtok(NULL,"");
brunofgc 1:0e0967c88590 302 actId = atoi(pChar);
brunofgc 1:0e0967c88590 303 if(dispositivos[0]->execAct(actId)){
brunofgc 1:0e0967c88590 304 sprintf(msg,"execAct_ack");
brunofgc 1:0e0967c88590 305 }else{
brunofgc 1:0e0967c88590 306 sprintf(msg,"execAct_nack");
brunofgc 1:0e0967c88590 307 }
brunofgc 1:0e0967c88590 308 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 7:ae9c47f62946 309 }
brunofgc 1:0e0967c88590 310
brunofgc 2:55b7b466e742 311 if(strstr(ptr,"enviaDadosAoServer")){
brunofgc 18:1eefda1f7736 312 eventosRTC::rotinaEnvioDeDados=maxTentativasEnvioDados;
brunofgc 2:55b7b466e742 313 sprintf(msg,"ack");
brunofgc 2:55b7b466e742 314 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 2:55b7b466e742 315 }
brunofgc 2:55b7b466e742 316
brunofgc 0:1c0a769988ee 317 ptr=strstr(ptrComando,"mbwrite:");
brunofgc 0:1c0a769988ee 318 if(ptr){
brunofgc 0:1c0a769988ee 319 uint8_t addr;
brunofgc 0:1c0a769988ee 320 uint16_t reg;
brunofgc 0:1c0a769988ee 321 uint32_t auxMod;
brunofgc 0:1c0a769988ee 322 char *pChar;
brunofgc 0:1c0a769988ee 323
brunofgc 1:0e0967c88590 324 //IR
brunofgc 1:0e0967c88590 325 if(strstr(ptr,"IR,")){
brunofgc 1:0e0967c88590 326 uint8_t freq=38,porta=0;
brunofgc 20:da1b8d80ba00 327 pChar = strtok(ptr,",");
brunofgc 1:0e0967c88590 328 freq = atoi(strtok(NULL,","));
brunofgc 20:da1b8d80ba00 329 porta = atoi(strtok(NULL,","));
brunofgc 1:0e0967c88590 330
brunofgc 7:ae9c47f62946 331 pChar = strtok(NULL,"S");
brunofgc 20:da1b8d80ba00 332 deserializaPacoteIR(pChar);
brunofgc 1:0e0967c88590 333 enviaComandoIR(freq,porta);
brunofgc 1:0e0967c88590 334 sprintf(msg,"WriteIR_ack");
brunofgc 1:0e0967c88590 335 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 1:0e0967c88590 336 }
brunofgc 1:0e0967c88590 337
brunofgc 0:1c0a769988ee 338 //Float
brunofgc 0:1c0a769988ee 339 if(strstr(ptr,"float,")){
brunofgc 0:1c0a769988ee 340 float v_float;
brunofgc 0:1c0a769988ee 341
brunofgc 0:1c0a769988ee 342 //Capturando parametros comuns
brunofgc 0:1c0a769988ee 343 pChar = strtok(ptr,",");
brunofgc 0:1c0a769988ee 344
brunofgc 0:1c0a769988ee 345 //Parametro 1
brunofgc 0:1c0a769988ee 346 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 347 addr = atoi(pChar);
brunofgc 0:1c0a769988ee 348
brunofgc 0:1c0a769988ee 349 //Parametro 2
brunofgc 0:1c0a769988ee 350 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 351 reg = atoi(pChar);
brunofgc 0:1c0a769988ee 352
brunofgc 0:1c0a769988ee 353 //Parametro 3
brunofgc 0:1c0a769988ee 354 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 355 //v_float = diversos::stringToFloat(pChar,50);
brunofgc 0:1c0a769988ee 356 v_float = atof(pChar);
brunofgc 1:0e0967c88590 357
brunofgc 1:0e0967c88590 358 //Envio o dado via Modbus
brunofgc 1:0e0967c88590 359 if(!modBusMaster1::writeFloat(addr,reg,1,&v_float)){
brunofgc 1:0e0967c88590 360 pc.printf("Valor <%f> escrito no registrador %lu do endereco modbus %u.\n",v_float,reg,addr);
brunofgc 0:1c0a769988ee 361 /*sprintf(diversos::msg,"WriteFloat_ack");
brunofgc 0:1c0a769988ee 362 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 363
brunofgc 1:0e0967c88590 364 sprintf(msg,"WriteFloat_ack");
brunofgc 1:0e0967c88590 365 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 1:0e0967c88590 366 }else{
brunofgc 1:0e0967c88590 367 pc.printf("Erro ao escrever valor <%f> no registrador %lu do endereco modbus %u.\n",v_float,reg,addr);
brunofgc 1:0e0967c88590 368 /*sprintf(diversos::msg,"WriteFloat_nack");
brunofgc 1:0e0967c88590 369 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 1:0e0967c88590 370
brunofgc 1:0e0967c88590 371 sprintf(msg,"WriteFloat_nack");
brunofgc 0:1c0a769988ee 372 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 1:0e0967c88590 373 }
brunofgc 1:0e0967c88590 374 }
brunofgc 1:0e0967c88590 375
brunofgc 1:0e0967c88590 376 //PWM
brunofgc 1:0e0967c88590 377 if(strstr(ptr,"PWM,")){
brunofgc 1:0e0967c88590 378 float v_float;
brunofgc 1:0e0967c88590 379
brunofgc 1:0e0967c88590 380 //Capturando parametros comuns
brunofgc 1:0e0967c88590 381 pChar = strtok(ptr,",");
brunofgc 1:0e0967c88590 382
brunofgc 1:0e0967c88590 383 //Parametro 1
brunofgc 1:0e0967c88590 384 pChar = strtok(NULL,",");
brunofgc 1:0e0967c88590 385 addr = atoi(pChar);
brunofgc 1:0e0967c88590 386
brunofgc 1:0e0967c88590 387 //Parametro 2
brunofgc 1:0e0967c88590 388 pChar = strtok(NULL,",");
brunofgc 1:0e0967c88590 389 reg = atoi(pChar);
brunofgc 1:0e0967c88590 390
brunofgc 1:0e0967c88590 391 //Parametro 3
brunofgc 1:0e0967c88590 392 pChar = strtok(NULL,",");
brunofgc 1:0e0967c88590 393 //v_float = diversos::stringToFloat(pChar,50);
brunofgc 1:0e0967c88590 394 v_float = atof(pChar);
brunofgc 1:0e0967c88590 395
brunofgc 1:0e0967c88590 396 pc.printf("\r\n\r\nVerificando conteudo de pChar antes de converter para float <%s>.\r\n",pChar);
brunofgc 1:0e0967c88590 397
brunofgc 1:0e0967c88590 398 //Retirando a parte inteira referente ao periodo em us do pwm
brunofgc 1:0e0967c88590 399
brunofgc 1:0e0967c88590 400 auxMod = v_float/10;
brunofgc 1:0e0967c88590 401 v_float = v_float-(auxMod*10);
brunofgc 1:0e0967c88590 402 SD1.period_us(auxMod);
brunofgc 1:0e0967c88590 403 pwmPeriod = auxMod;
brunofgc 1:0e0967c88590 404 switch(reg){
brunofgc 1:0e0967c88590 405 case 0:
brunofgc 1:0e0967c88590 406 SD1.write(v_float);
brunofgc 1:0e0967c88590 407 break;
brunofgc 1:0e0967c88590 408 case 1:
brunofgc 1:0e0967c88590 409 SD2.write(v_float);
brunofgc 1:0e0967c88590 410 break;
brunofgc 1:0e0967c88590 411 case 2:
brunofgc 1:0e0967c88590 412 SD3.write(v_float);
brunofgc 1:0e0967c88590 413 break;
brunofgc 1:0e0967c88590 414 case 3:
brunofgc 1:0e0967c88590 415 SD4.write(v_float);
brunofgc 1:0e0967c88590 416 break;
brunofgc 1:0e0967c88590 417 case 4:
brunofgc 1:0e0967c88590 418 SD5.write(v_float);
brunofgc 1:0e0967c88590 419 break;
brunofgc 1:0e0967c88590 420 case 5:
brunofgc 1:0e0967c88590 421 SD6.write(v_float);
brunofgc 1:0e0967c88590 422 break;
brunofgc 0:1c0a769988ee 423 }
brunofgc 1:0e0967c88590 424 pc.printf("Valor puro %f, %lu escrito como periodo e %f como duty no reg %u.\n",v_float,auxMod,v_float,reg);
brunofgc 1:0e0967c88590 425 /*sprintf(diversos::msg,"WriteFloat_ack");
brunofgc 1:0e0967c88590 426 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 1:0e0967c88590 427
brunofgc 1:0e0967c88590 428 sprintf(msg,"WritePWM_ack");
brunofgc 1:0e0967c88590 429 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 430 }
brunofgc 0:1c0a769988ee 431
brunofgc 0:1c0a769988ee 432 //uint32_t
brunofgc 0:1c0a769988ee 433 if(strstr(ptr,"uint32_t,")){
brunofgc 0:1c0a769988ee 434 uint32_t v_uint32_t;
brunofgc 0:1c0a769988ee 435
brunofgc 0:1c0a769988ee 436 //Capturando parametros comuns
brunofgc 0:1c0a769988ee 437 pChar = strtok(ptr,",");
brunofgc 0:1c0a769988ee 438
brunofgc 0:1c0a769988ee 439 //Parametro 1
brunofgc 0:1c0a769988ee 440 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 441 addr = atoi(pChar);
brunofgc 0:1c0a769988ee 442
brunofgc 0:1c0a769988ee 443 //Parametro 2
brunofgc 0:1c0a769988ee 444 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 445 reg = atoi(pChar);
brunofgc 0:1c0a769988ee 446
brunofgc 0:1c0a769988ee 447 //Parametro 3
brunofgc 0:1c0a769988ee 448 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 449 v_uint32_t = atoi(pChar);
brunofgc 0:1c0a769988ee 450
brunofgc 0:1c0a769988ee 451 if(!modBusMaster1::writeRegister32BIT(addr,reg,1,&v_uint32_t)){
brunofgc 0:1c0a769988ee 452 pc.printf("Valor <%lu> escrito no registrador %lu do endereco modbus %u.\n",v_uint32_t,reg,addr);
brunofgc 0:1c0a769988ee 453 /*sprintf(diversos::msg,"Write_uint32_t_ack");
brunofgc 0:1c0a769988ee 454 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 455
brunofgc 1:0e0967c88590 456 sprintf(msg,"Write_uint32_t_ack");
brunofgc 0:1c0a769988ee 457 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 458 }else{
brunofgc 0:1c0a769988ee 459 pc.printf("Erro ao escrever valor <%lu> no registrador %lu do endereco modbus %u.\n",v_uint32_t,reg,addr);
brunofgc 0:1c0a769988ee 460 /*sprintf(diversos::msg,"Write_uint32_t_nack");
brunofgc 0:1c0a769988ee 461 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 462
brunofgc 1:0e0967c88590 463 sprintf(msg,"Write_uint32_t_nack");
brunofgc 0:1c0a769988ee 464 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 465 }
brunofgc 0:1c0a769988ee 466 }
brunofgc 0:1c0a769988ee 467
brunofgc 0:1c0a769988ee 468 //uint16_t
brunofgc 0:1c0a769988ee 469 if(strstr(ptr,"uint16_t,")){
brunofgc 0:1c0a769988ee 470 uint16_t v_uint16_t;
brunofgc 0:1c0a769988ee 471
brunofgc 0:1c0a769988ee 472 //Capturando parametros comuns
brunofgc 0:1c0a769988ee 473 pChar = strtok(ptr,",");
brunofgc 0:1c0a769988ee 474
brunofgc 0:1c0a769988ee 475 //Parametro 1
brunofgc 0:1c0a769988ee 476 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 477 addr = atoi(pChar);
brunofgc 0:1c0a769988ee 478
brunofgc 0:1c0a769988ee 479 //Parametro 2
brunofgc 0:1c0a769988ee 480 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 481 reg = atoi(pChar);
brunofgc 0:1c0a769988ee 482
brunofgc 0:1c0a769988ee 483 //Parametro 3
brunofgc 0:1c0a769988ee 484 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 485 v_uint16_t = atoi(pChar);
brunofgc 0:1c0a769988ee 486
brunofgc 0:1c0a769988ee 487 if(!modBusMaster1::writeRegister16BIT(addr,reg,1,&v_uint16_t)){
brunofgc 0:1c0a769988ee 488 pc.printf("Valor <%lu> escrito no registrador %lu do endereco modbus %u.\n",v_uint16_t,reg,addr);
brunofgc 0:1c0a769988ee 489 /*sprintf(diversos::msg,"Write_uint16_t_ack");
brunofgc 0:1c0a769988ee 490 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 491
brunofgc 1:0e0967c88590 492 sprintf(msg,"Write_uint16_t_ack");
brunofgc 0:1c0a769988ee 493 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 494 }else{
brunofgc 0:1c0a769988ee 495 pc.printf("Erro ao escrever valor <%lu> no registrador %lu do endereco modbus %u.\n",v_uint16_t,reg,addr);
brunofgc 0:1c0a769988ee 496 /*sprintf(diversos::msg,"Write_uint16_t_nack");
brunofgc 0:1c0a769988ee 497 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg)); */
brunofgc 0:1c0a769988ee 498
brunofgc 1:0e0967c88590 499 sprintf(msg,"Write_uint16_t_nack");
brunofgc 0:1c0a769988ee 500 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 501 }
brunofgc 0:1c0a769988ee 502 }
brunofgc 0:1c0a769988ee 503
brunofgc 0:1c0a769988ee 504 //bit
brunofgc 0:1c0a769988ee 505 if(strstr(ptr,"bit,")){
brunofgc 0:1c0a769988ee 506 bool v_bool;
brunofgc 0:1c0a769988ee 507
brunofgc 0:1c0a769988ee 508 //Capturando parametros comuns
brunofgc 0:1c0a769988ee 509 pChar = strtok(ptr,",");
brunofgc 0:1c0a769988ee 510
brunofgc 0:1c0a769988ee 511 //Parametro 1
brunofgc 0:1c0a769988ee 512 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 513 addr = atoi(pChar);
brunofgc 0:1c0a769988ee 514
brunofgc 0:1c0a769988ee 515 //Parametro 2
brunofgc 0:1c0a769988ee 516 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 517 reg = atoi(pChar);
brunofgc 0:1c0a769988ee 518
brunofgc 0:1c0a769988ee 519 //Parametro 3
brunofgc 0:1c0a769988ee 520 pChar = strtok(NULL,",");
brunofgc 0:1c0a769988ee 521 v_bool = (atoi(pChar) > 0);
brunofgc 0:1c0a769988ee 522
brunofgc 5:7801f913384e 523 if(addr != enderecoControladoraVirtual){
brunofgc 0:1c0a769988ee 524 //writeSingleCoil(uint8_t,uint16_t,bool); //Endereço slave, registrador, bool
brunofgc 0:1c0a769988ee 525 if(!modBusMaster1::writeSingleCoil(addr,reg,v_bool)){
brunofgc 0:1c0a769988ee 526 pc.printf("Valor <%u> escrito no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr);
brunofgc 0:1c0a769988ee 527 /*sprintf(diversos::msg,"Write_bool_ack");
brunofgc 0:1c0a769988ee 528 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 529
brunofgc 1:0e0967c88590 530 sprintf(msg,"Write_bool_ack");
brunofgc 0:1c0a769988ee 531 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 532 }else{
brunofgc 0:1c0a769988ee 533 pc.printf("Erro ao escrever valor <%u> no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr);
brunofgc 0:1c0a769988ee 534 /*sprintf(diversos::msg,"Write_bool_nack");
brunofgc 0:1c0a769988ee 535 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 536
brunofgc 1:0e0967c88590 537 sprintf(msg,"Write_bool_nack");
brunofgc 0:1c0a769988ee 538 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 539 }
brunofgc 0:1c0a769988ee 540 }else{
brunofgc 0:1c0a769988ee 541 switch(reg){
brunofgc 0:1c0a769988ee 542 case 0:
brunofgc 0:1c0a769988ee 543 SD1.write(v_bool*1.0f);
brunofgc 0:1c0a769988ee 544 break;
brunofgc 0:1c0a769988ee 545 case 1:
brunofgc 0:1c0a769988ee 546 SD2.write(v_bool*1.0f);
brunofgc 0:1c0a769988ee 547 break;
brunofgc 0:1c0a769988ee 548 case 2:
brunofgc 0:1c0a769988ee 549 SD3.write(v_bool*1.0f);
brunofgc 0:1c0a769988ee 550 break;
brunofgc 0:1c0a769988ee 551 case 3:
brunofgc 0:1c0a769988ee 552 SD4.write(v_bool*1.0f);
brunofgc 0:1c0a769988ee 553 break;
brunofgc 0:1c0a769988ee 554 case 4:
brunofgc 0:1c0a769988ee 555 SD5.write(v_bool*1.0f);
brunofgc 0:1c0a769988ee 556 break;
brunofgc 0:1c0a769988ee 557 case 5:
brunofgc 0:1c0a769988ee 558 SD6.write(v_bool*1.0f);
brunofgc 0:1c0a769988ee 559 break;
brunofgc 0:1c0a769988ee 560 case 6:
brunofgc 0:1c0a769988ee 561 SD7 = v_bool;
brunofgc 0:1c0a769988ee 562 break;
brunofgc 0:1c0a769988ee 563 case 7:
brunofgc 0:1c0a769988ee 564 SD8 = v_bool;
brunofgc 0:1c0a769988ee 565 break;
brunofgc 0:1c0a769988ee 566 }
brunofgc 0:1c0a769988ee 567 pc.printf("Valor <%u> escrito no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr);
brunofgc 0:1c0a769988ee 568 /*sprintf(diversos::msg,"Write_bool_ack");
brunofgc 0:1c0a769988ee 569 modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/
brunofgc 0:1c0a769988ee 570
brunofgc 1:0e0967c88590 571 sprintf(msg,"Write_bool_ack");
brunofgc 0:1c0a769988ee 572 modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg));
brunofgc 0:1c0a769988ee 573 }
brunofgc 0:1c0a769988ee 574 }
brunofgc 9:cf406384efd9 575 modemCom::status.timeOut=5;
brunofgc 0:1c0a769988ee 576 }
brunofgc 0:1c0a769988ee 577
brunofgc 0:1c0a769988ee 578 currentCur = strlen(ptrComando) + 1; //Aqui eu pego onde devo buscar novamente mais algum comando.
brunofgc 0:1c0a769988ee 579 if(currentCur>=totalLengthCommands){
brunofgc 0:1c0a769988ee 580 //modemCom::status.timeOut=10;
brunofgc 0:1c0a769988ee 581 ptrComando = NULL;
brunofgc 0:1c0a769988ee 582 }else{
brunofgc 0:1c0a769988ee 583 ptrComando = strtok(&ptrComando[currentCur],";");
brunofgc 0:1c0a769988ee 584 }
brunofgc 0:1c0a769988ee 585 }while(ptrComando != NULL);//Este loop executa cada instrução.
brunofgc 0:1c0a769988ee 586 pc.printf("Atendidos todos os comandos.\n");
brunofgc 0:1c0a769988ee 587 }