teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
diversos.cpp@2:55b7b466e742, 2017-05-10 (annotated)
- Committer:
- brunofgc
- Date:
- Wed May 10 12:57:13 2017 +0000
- Revision:
- 2:55b7b466e742
- Parent:
- 1:0e0967c88590
- Child:
- 3:9598af355293
Ainda com bug em configRede.htm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:1c0a769988ee | 1 | #include "diversos.h" |
brunofgc | 0:1c0a769988ee | 2 | bool eventosRTC::segundos; |
brunofgc | 0:1c0a769988ee | 3 | bool eventosRTC::minutos; |
brunofgc | 0:1c0a769988ee | 4 | bool eventosRTC::rotina10Segundos; |
brunofgc | 0:1c0a769988ee | 5 | bool eventosRTC::rotina15Minutos; |
brunofgc | 0:1c0a769988ee | 6 | bool eventosRTC::rotina1hora; |
brunofgc | 0:1c0a769988ee | 7 | bool eventosRTC::rotina1segundo; |
brunofgc | 0:1c0a769988ee | 8 | WatchdogTimer diversos::wdt; |
brunofgc | 0:1c0a769988ee | 9 | //char diversos::msg[200]; |
brunofgc | 0:1c0a769988ee | 10 | //bool eventosRTC::hora_completa; |
brunofgc | 0:1c0a769988ee | 11 | char commands::buffer[tamBufferCommands]; |
brunofgc | 0:1c0a769988ee | 12 | |
brunofgc | 0:1c0a769988ee | 13 | /*void WatchdogTimer::WatchdogTimer(void){ |
brunofgc | 0:1c0a769988ee | 14 | //Constructor |
brunofgc | 0:1c0a769988ee | 15 | }*/ |
brunofgc | 0:1c0a769988ee | 16 | |
brunofgc | 0:1c0a769988ee | 17 | uint32_t diversos::memAvailable(){ |
brunofgc | 0:1c0a769988ee | 18 | char stackVariable; |
brunofgc | 0:1c0a769988ee | 19 | char *heap; |
brunofgc | 0:1c0a769988ee | 20 | uint32_t result; |
brunofgc | 0:1c0a769988ee | 21 | heap = (char*)malloc(1); |
brunofgc | 0:1c0a769988ee | 22 | result = (uint32_t) ((&stackVariable) - heap); |
brunofgc | 0:1c0a769988ee | 23 | free(heap); |
brunofgc | 0:1c0a769988ee | 24 | return result; |
brunofgc | 0:1c0a769988ee | 25 | } |
brunofgc | 0:1c0a769988ee | 26 | |
brunofgc | 0:1c0a769988ee | 27 | void WatchdogTimer::kick(float s) { |
brunofgc | 0:1c0a769988ee | 28 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
brunofgc | 0:1c0a769988ee | 29 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
brunofgc | 0:1c0a769988ee | 30 | LPC_WDT->WDTC = s * (float)clk; |
brunofgc | 0:1c0a769988ee | 31 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
brunofgc | 0:1c0a769988ee | 32 | kick(); |
brunofgc | 0:1c0a769988ee | 33 | } |
brunofgc | 0:1c0a769988ee | 34 | // "kick" or "feed" the dog - reset the watchdog timer |
brunofgc | 0:1c0a769988ee | 35 | // by writing this required bit pattern |
brunofgc | 0:1c0a769988ee | 36 | void WatchdogTimer::kick() { |
brunofgc | 0:1c0a769988ee | 37 | LPC_WDT->WDFEED = 0xAA; |
brunofgc | 0:1c0a769988ee | 38 | LPC_WDT->WDFEED = 0x55; |
brunofgc | 0:1c0a769988ee | 39 | } |
brunofgc | 0:1c0a769988ee | 40 | |
brunofgc | 0:1c0a769988ee | 41 | /*double diversos::stringToFloat(char *p,uint16_t max){ |
brunofgc | 0:1c0a769988ee | 42 | uint16_t caractere=0; |
brunofgc | 0:1c0a769988ee | 43 | double iPart=0.0,dPart=0.0,f=0.0,mult=0.1; |
brunofgc | 0:1c0a769988ee | 44 | while(*p != '.') { |
brunofgc | 0:1c0a769988ee | 45 | caractere++; |
brunofgc | 0:1c0a769988ee | 46 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 47 | iPart = (iPart*10) + (double)((*(p++)-'0')); |
brunofgc | 0:1c0a769988ee | 48 | } |
brunofgc | 0:1c0a769988ee | 49 | p++; |
brunofgc | 0:1c0a769988ee | 50 | while(*p!= '\0') { |
brunofgc | 0:1c0a769988ee | 51 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 52 | dPart += (double)(((*(p++)-'0'))*mult); |
brunofgc | 0:1c0a769988ee | 53 | mult /= 10; |
brunofgc | 0:1c0a769988ee | 54 | } |
brunofgc | 0:1c0a769988ee | 55 | f = iPart + dPart; |
brunofgc | 0:1c0a769988ee | 56 | return f; |
brunofgc | 0:1c0a769988ee | 57 | }*/ |
brunofgc | 0:1c0a769988ee | 58 | |
brunofgc | 0:1c0a769988ee | 59 | /*double diversos::stringToFloat(char *p,uint16_t max){ |
brunofgc | 0:1c0a769988ee | 60 | uint16_t caractere=0; |
brunofgc | 0:1c0a769988ee | 61 | double iPart=0.0,dPart=0.0,f=0.0,mult=0.1; |
brunofgc | 0:1c0a769988ee | 62 | while(p[caractere] != '.'){ |
brunofgc | 0:1c0a769988ee | 63 | caractere++; |
brunofgc | 0:1c0a769988ee | 64 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 65 | iPart = (iPart*10) + (((uint8_t)p[caractere])-30); |
brunofgc | 0:1c0a769988ee | 66 | } |
brunofgc | 0:1c0a769988ee | 67 | caractere++; |
brunofgc | 0:1c0a769988ee | 68 | while(p[caractere]!= '\0'){ |
brunofgc | 0:1c0a769988ee | 69 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 70 | dPart += (((uint8_t)p[caractere])-30)*mult; |
brunofgc | 0:1c0a769988ee | 71 | mult /= 10; |
brunofgc | 0:1c0a769988ee | 72 | } |
brunofgc | 0:1c0a769988ee | 73 | f = iPart + dPart; |
brunofgc | 0:1c0a769988ee | 74 | return f; |
brunofgc | 0:1c0a769988ee | 75 | }*/ |
brunofgc | 0:1c0a769988ee | 76 | |
brunofgc | 0:1c0a769988ee | 77 | void diversos::progressBar(uint32_t progresso,uint32_t total){ |
brunofgc | 0:1c0a769988ee | 78 | int i,j; |
brunofgc | 0:1c0a769988ee | 79 | char maxTam = 20; |
brunofgc | 0:1c0a769988ee | 80 | char engrenagem[4]={179,0x2F,0x2D,0x5C}; |
brunofgc | 0:1c0a769988ee | 81 | |
brunofgc | 0:1c0a769988ee | 82 | j=((double)progresso/(double)total)*maxTam; |
brunofgc | 0:1c0a769988ee | 83 | pc.printf("%c%c%c<",179,engrenagem[(unsigned int)progresso%4],179); |
brunofgc | 0:1c0a769988ee | 84 | for(i=0;i<maxTam;i++){ |
brunofgc | 0:1c0a769988ee | 85 | if(i<=j){ |
brunofgc | 0:1c0a769988ee | 86 | pc.printf("%c",178); |
brunofgc | 0:1c0a769988ee | 87 | }else{ |
brunofgc | 0:1c0a769988ee | 88 | pc.printf("%c",176); |
brunofgc | 0:1c0a769988ee | 89 | } |
brunofgc | 0:1c0a769988ee | 90 | } |
brunofgc | 0:1c0a769988ee | 91 | pc.printf("> %3.0f%% Progresso %5lu de %5lu\r ",((double)progresso/(double)total)*100,progresso,total); |
brunofgc | 0:1c0a769988ee | 92 | } |
brunofgc | 0:1c0a769988ee | 93 | |
brunofgc | 0:1c0a769988ee | 94 | void commands::exec(uint8_t idConnection){ |
brunofgc | 0:1c0a769988ee | 95 | char *ptr,*pCharFile,*ptrComando; |
brunofgc | 0:1c0a769988ee | 96 | char msg[100]; |
brunofgc | 0:1c0a769988ee | 97 | uint16_t currentCur,totalLengthCommands; |
brunofgc | 0:1c0a769988ee | 98 | static uint16_t fileCheckSum16BIT,checkSum16BIT_Lido; |
brunofgc | 0:1c0a769988ee | 99 | |
brunofgc | 0:1c0a769988ee | 100 | |
brunofgc | 0:1c0a769988ee | 101 | totalLengthCommands = strlen(commands::buffer); |
brunofgc | 0:1c0a769988ee | 102 | ptrComando = strtok(commands::buffer,";"); |
brunofgc | 0:1c0a769988ee | 103 | |
brunofgc | 0:1c0a769988ee | 104 | do{ |
brunofgc | 2:55b7b466e742 | 105 | |
brunofgc | 2:55b7b466e742 | 106 | |
brunofgc | 2:55b7b466e742 | 107 | |
brunofgc | 0:1c0a769988ee | 108 | ptr=strstr(ptrComando,"DELETE_SENT_FILES"); |
brunofgc | 0:1c0a769988ee | 109 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 110 | pc.printf("Recebido comando para deletar arquivos enviados.\n"); |
brunofgc | 0:1c0a769988ee | 111 | sdCard::deleteSentFiles = true; |
brunofgc | 0:1c0a769988ee | 112 | } |
brunofgc | 0:1c0a769988ee | 113 | |
brunofgc | 0:1c0a769988ee | 114 | ptr=strstr(ptrComando,"SET_RTC:"); |
brunofgc | 0:1c0a769988ee | 115 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 116 | ptr = strtok(ptr,":"); |
brunofgc | 0:1c0a769988ee | 117 | ptr = strtok(NULL,";"); |
brunofgc | 0:1c0a769988ee | 118 | set_time(atoi(ptr)); |
brunofgc | 0:1c0a769988ee | 119 | time_t seconds = time(NULL); |
brunofgc | 0:1c0a769988ee | 120 | pc.printf("Horario atualizado pelo server %s",ctime(&seconds)); |
brunofgc | 0:1c0a769988ee | 121 | } |
brunofgc | 0:1c0a769988ee | 122 | |
brunofgc | 0:1c0a769988ee | 123 | ptr=strstr(ptrComando,"sendArmazenamento"); |
brunofgc | 0:1c0a769988ee | 124 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 125 | eventosRTC::rotina1hora=1; |
brunofgc | 0:1c0a769988ee | 126 | } |
brunofgc | 0:1c0a769988ee | 127 | |
brunofgc | 0:1c0a769988ee | 128 | ptr=strstr(ptrComando,"StartSendFile"); |
brunofgc | 0:1c0a769988ee | 129 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 130 | modemCom::status.timeOut=100; |
brunofgc | 0:1c0a769988ee | 131 | strtok(ptr,",");//Descartando inicio |
brunofgc | 0:1c0a769988ee | 132 | pCharFile = strtok(NULL,",");//Selecionando nome do arquivo |
brunofgc | 0:1c0a769988ee | 133 | strcpy(sdCard::nomeArquivoEmRecebimento,pCharFile); |
brunofgc | 0:1c0a769988ee | 134 | pCharFile = strtok(NULL,";");//Selecionando string checkSum16BIT |
brunofgc | 0:1c0a769988ee | 135 | fileCheckSum16BIT = atoi(pCharFile); |
brunofgc | 0:1c0a769988ee | 136 | remove("/sd/RAD/tempFile.bin"); |
brunofgc | 0:1c0a769988ee | 137 | pc.printf("Iniciando recepcao do server arquivo deviceCfg.\n"); |
brunofgc | 0:1c0a769988ee | 138 | modemCom::status.recebendoArquivoDoServer = true; |
brunofgc | 0:1c0a769988ee | 139 | |
brunofgc | 0:1c0a769988ee | 140 | sprintf(modemCom::bufIn,"AT+CIPSEND=%u,%lu\r\n",idConnection,10); |
brunofgc | 0:1c0a769988ee | 141 | modem.puts(modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 142 | osDelay(100); |
brunofgc | 0:1c0a769988ee | 143 | modem.puts("sendData\r\n"); |
brunofgc | 0:1c0a769988ee | 144 | |
brunofgc | 0:1c0a769988ee | 145 | pc.printf("sendData\n"); |
brunofgc | 0:1c0a769988ee | 146 | modemCom::timeOutModem = 250; |
brunofgc | 0:1c0a769988ee | 147 | return; |
brunofgc | 0:1c0a769988ee | 148 | } |
brunofgc | 0:1c0a769988ee | 149 | |
brunofgc | 0:1c0a769988ee | 150 | ptr=strstr(ptrComando,"StopSendFile"); |
brunofgc | 0:1c0a769988ee | 151 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 152 | pc.printf("Finalizando recepcao do server arquivo deviceCfg.\n"); |
brunofgc | 0:1c0a769988ee | 153 | modemCom::status.recebendoArquivoDoServer = false; |
brunofgc | 0:1c0a769988ee | 154 | checkSum16BIT_Lido = sdCard::calcCheckSum16BITFile("/sd/RAD/tempFile.bin"); |
brunofgc | 0:1c0a769988ee | 155 | if(fileCheckSum16BIT == checkSum16BIT_Lido){ |
brunofgc | 0:1c0a769988ee | 156 | if(sdCard::file_rename("/sd/RAD/tempFile.bin",sdCard::nomeArquivoEmRecebimento)){ |
brunofgc | 0:1c0a769988ee | 157 | /*sprintf(diversos::msg,"File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 158 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 159 | sprintf(msg,"File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 160 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 161 | pc.printf("File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 162 | |
brunofgc | 0:1c0a769988ee | 163 | if(strstr(sdCard::nomeArquivoEmRecebimento,"devices.cfg")!=NULL){ |
brunofgc | 0:1c0a769988ee | 164 | pc.printf("Excluindo arquivo armazenamento.txt .\r\n"); |
brunofgc | 0:1c0a769988ee | 165 | sdCard::excluiArquivo(&sdCard::armazenamento); |
brunofgc | 0:1c0a769988ee | 166 | pc.printf("Resetando o drome.\r\n"); |
brunofgc | 0:1c0a769988ee | 167 | hardwareReset=true; |
brunofgc | 0:1c0a769988ee | 168 | } |
brunofgc | 0:1c0a769988ee | 169 | |
brunofgc | 0:1c0a769988ee | 170 | }else{ |
brunofgc | 0:1c0a769988ee | 171 | //modemCom::sendBufferCommandMode(2,"FileCorrupted.\r\n",strlen("FileCorrupted.\r\n")); |
brunofgc | 1:0e0967c88590 | 172 | sprintf(msg,"FileCorrupted.\r\n"); |
brunofgc | 0:1c0a769988ee | 173 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 174 | pc.printf("Recebido correto mas n foi possivel renomear arquivo.\r\n"); |
brunofgc | 0:1c0a769988ee | 175 | } |
brunofgc | 0:1c0a769988ee | 176 | |
brunofgc | 0:1c0a769988ee | 177 | }else{ |
brunofgc | 0:1c0a769988ee | 178 | /*sprintf(diversos::msg,"File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 179 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 180 | sprintf(msg,"File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 181 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 182 | pc.printf("File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 183 | } |
brunofgc | 0:1c0a769988ee | 184 | modemCom::status.timeOut=10; |
brunofgc | 0:1c0a769988ee | 185 | modemCom::timeOutModem = 50; |
brunofgc | 0:1c0a769988ee | 186 | } |
brunofgc | 0:1c0a769988ee | 187 | |
brunofgc | 0:1c0a769988ee | 188 | /*ptr=strstr(ptrComando,"requestFile,"); |
brunofgc | 0:1c0a769988ee | 189 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 190 | FILE *fp; |
brunofgc | 0:1c0a769988ee | 191 | char c; |
brunofgc | 0:1c0a769988ee | 192 | strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 193 | pCharFile = strtok(NULL,";"); |
brunofgc | 0:1c0a769988ee | 194 | |
brunofgc | 0:1c0a769988ee | 195 | fp = fopen(pCharFile,"r"); |
brunofgc | 0:1c0a769988ee | 196 | if(fp!=NULL){ |
brunofgc | 0:1c0a769988ee | 197 | do{ |
brunofgc | 0:1c0a769988ee | 198 | c = fgetc(fp); |
brunofgc | 0:1c0a769988ee | 199 | if(!feof(fp)){modem.printf("%c",c);} |
brunofgc | 0:1c0a769988ee | 200 | modemCom::status.timeOut=10; |
brunofgc | 0:1c0a769988ee | 201 | }while(!feof(fp)); |
brunofgc | 0:1c0a769988ee | 202 | fclose(fp); |
brunofgc | 0:1c0a769988ee | 203 | }else{ |
brunofgc | 0:1c0a769988ee | 204 | modem.printf("Nao foi possivel abrir o arquivo.\n"); |
brunofgc | 0:1c0a769988ee | 205 | } |
brunofgc | 0:1c0a769988ee | 206 | }*/ |
brunofgc | 0:1c0a769988ee | 207 | |
brunofgc | 1:0e0967c88590 | 208 | ptr=strstr(ptrComando,"execAct:"); |
brunofgc | 1:0e0967c88590 | 209 | if(ptr){ |
brunofgc | 1:0e0967c88590 | 210 | char *pChar; |
brunofgc | 1:0e0967c88590 | 211 | uint32_t actId; |
brunofgc | 1:0e0967c88590 | 212 | pChar = strtok(ptr,":"); |
brunofgc | 1:0e0967c88590 | 213 | pChar = strtok(NULL,""); |
brunofgc | 1:0e0967c88590 | 214 | actId = atoi(pChar); |
brunofgc | 1:0e0967c88590 | 215 | if(dispositivos[0]->execAct(actId)){ |
brunofgc | 1:0e0967c88590 | 216 | sprintf(msg,"execAct_ack"); |
brunofgc | 1:0e0967c88590 | 217 | }else{ |
brunofgc | 1:0e0967c88590 | 218 | sprintf(msg,"execAct_nack"); |
brunofgc | 1:0e0967c88590 | 219 | } |
brunofgc | 1:0e0967c88590 | 220 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 221 | } |
brunofgc | 1:0e0967c88590 | 222 | |
brunofgc | 2:55b7b466e742 | 223 | if(strstr(ptr,"enviaDadosAoServer")){ |
brunofgc | 2:55b7b466e742 | 224 | eventosRTC::rotina15Minutos=true; |
brunofgc | 2:55b7b466e742 | 225 | sprintf(msg,"ack"); |
brunofgc | 2:55b7b466e742 | 226 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 2:55b7b466e742 | 227 | } |
brunofgc | 2:55b7b466e742 | 228 | |
brunofgc | 0:1c0a769988ee | 229 | ptr=strstr(ptrComando,"mbwrite:"); |
brunofgc | 0:1c0a769988ee | 230 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 231 | uint8_t addr; |
brunofgc | 0:1c0a769988ee | 232 | uint16_t reg; |
brunofgc | 0:1c0a769988ee | 233 | uint32_t auxMod; |
brunofgc | 0:1c0a769988ee | 234 | char *pChar; |
brunofgc | 0:1c0a769988ee | 235 | |
brunofgc | 1:0e0967c88590 | 236 | //IR |
brunofgc | 1:0e0967c88590 | 237 | if(strstr(ptr,"IR,")){ |
brunofgc | 1:0e0967c88590 | 238 | uint8_t freq=38,porta=0; |
brunofgc | 1:0e0967c88590 | 239 | pChar = strtok(ptr,","); |
brunofgc | 1:0e0967c88590 | 240 | porta = atoi(strtok(NULL,",")); |
brunofgc | 1:0e0967c88590 | 241 | porta--; //Trazendo para índice 0; |
brunofgc | 1:0e0967c88590 | 242 | |
brunofgc | 1:0e0967c88590 | 243 | freq = atoi(strtok(NULL,",")); |
brunofgc | 1:0e0967c88590 | 244 | if(freq == 0){ |
brunofgc | 1:0e0967c88590 | 245 | //Se não tiver frequência, ponha 38. |
brunofgc | 1:0e0967c88590 | 246 | freq = 38; |
brunofgc | 1:0e0967c88590 | 247 | } |
brunofgc | 1:0e0967c88590 | 248 | |
brunofgc | 2:55b7b466e742 | 249 | pChar = strtok(NULL,"S");//Reaproveitando variável pTipoComando |
brunofgc | 2:55b7b466e742 | 250 | |
brunofgc | 1:0e0967c88590 | 251 | deserializaPacoteIR(pChar); |
brunofgc | 1:0e0967c88590 | 252 | enviaComandoIR(freq,porta); |
brunofgc | 1:0e0967c88590 | 253 | sprintf(msg,"WriteIR_ack"); |
brunofgc | 1:0e0967c88590 | 254 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 255 | } |
brunofgc | 1:0e0967c88590 | 256 | |
brunofgc | 0:1c0a769988ee | 257 | //Float |
brunofgc | 0:1c0a769988ee | 258 | if(strstr(ptr,"float,")){ |
brunofgc | 0:1c0a769988ee | 259 | float v_float; |
brunofgc | 0:1c0a769988ee | 260 | |
brunofgc | 0:1c0a769988ee | 261 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 262 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 263 | |
brunofgc | 0:1c0a769988ee | 264 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 265 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 266 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 267 | |
brunofgc | 0:1c0a769988ee | 268 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 269 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 270 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 271 | |
brunofgc | 0:1c0a769988ee | 272 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 273 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 274 | //v_float = diversos::stringToFloat(pChar,50); |
brunofgc | 0:1c0a769988ee | 275 | v_float = atof(pChar); |
brunofgc | 1:0e0967c88590 | 276 | |
brunofgc | 1:0e0967c88590 | 277 | //Envio o dado via Modbus |
brunofgc | 1:0e0967c88590 | 278 | if(!modBusMaster1::writeFloat(addr,reg,1,&v_float)){ |
brunofgc | 1:0e0967c88590 | 279 | pc.printf("Valor <%f> escrito no registrador %lu do endereco modbus %u.\n",v_float,reg,addr); |
brunofgc | 0:1c0a769988ee | 280 | /*sprintf(diversos::msg,"WriteFloat_ack"); |
brunofgc | 0:1c0a769988ee | 281 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 282 | |
brunofgc | 1:0e0967c88590 | 283 | sprintf(msg,"WriteFloat_ack"); |
brunofgc | 1:0e0967c88590 | 284 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 285 | }else{ |
brunofgc | 1:0e0967c88590 | 286 | pc.printf("Erro ao escrever valor <%f> no registrador %lu do endereco modbus %u.\n",v_float,reg,addr); |
brunofgc | 1:0e0967c88590 | 287 | /*sprintf(diversos::msg,"WriteFloat_nack"); |
brunofgc | 1:0e0967c88590 | 288 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 1:0e0967c88590 | 289 | |
brunofgc | 1:0e0967c88590 | 290 | sprintf(msg,"WriteFloat_nack"); |
brunofgc | 0:1c0a769988ee | 291 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 292 | } |
brunofgc | 1:0e0967c88590 | 293 | } |
brunofgc | 1:0e0967c88590 | 294 | |
brunofgc | 1:0e0967c88590 | 295 | //PWM |
brunofgc | 1:0e0967c88590 | 296 | if(strstr(ptr,"PWM,")){ |
brunofgc | 1:0e0967c88590 | 297 | float v_float; |
brunofgc | 1:0e0967c88590 | 298 | |
brunofgc | 1:0e0967c88590 | 299 | //Capturando parametros comuns |
brunofgc | 1:0e0967c88590 | 300 | pChar = strtok(ptr,","); |
brunofgc | 1:0e0967c88590 | 301 | |
brunofgc | 1:0e0967c88590 | 302 | //Parametro 1 |
brunofgc | 1:0e0967c88590 | 303 | pChar = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 304 | addr = atoi(pChar); |
brunofgc | 1:0e0967c88590 | 305 | |
brunofgc | 1:0e0967c88590 | 306 | //Parametro 2 |
brunofgc | 1:0e0967c88590 | 307 | pChar = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 308 | reg = atoi(pChar); |
brunofgc | 1:0e0967c88590 | 309 | |
brunofgc | 1:0e0967c88590 | 310 | //Parametro 3 |
brunofgc | 1:0e0967c88590 | 311 | pChar = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 312 | //v_float = diversos::stringToFloat(pChar,50); |
brunofgc | 1:0e0967c88590 | 313 | v_float = atof(pChar); |
brunofgc | 1:0e0967c88590 | 314 | |
brunofgc | 1:0e0967c88590 | 315 | pc.printf("\r\n\r\nVerificando conteudo de pChar antes de converter para float <%s>.\r\n",pChar); |
brunofgc | 1:0e0967c88590 | 316 | |
brunofgc | 1:0e0967c88590 | 317 | //Retirando a parte inteira referente ao periodo em us do pwm |
brunofgc | 1:0e0967c88590 | 318 | |
brunofgc | 1:0e0967c88590 | 319 | auxMod = v_float/10; |
brunofgc | 1:0e0967c88590 | 320 | v_float = v_float-(auxMod*10); |
brunofgc | 1:0e0967c88590 | 321 | SD1.period_us(auxMod); |
brunofgc | 1:0e0967c88590 | 322 | pwmPeriod = auxMod; |
brunofgc | 1:0e0967c88590 | 323 | switch(reg){ |
brunofgc | 1:0e0967c88590 | 324 | case 0: |
brunofgc | 1:0e0967c88590 | 325 | SD1.write(v_float); |
brunofgc | 1:0e0967c88590 | 326 | break; |
brunofgc | 1:0e0967c88590 | 327 | case 1: |
brunofgc | 1:0e0967c88590 | 328 | SD2.write(v_float); |
brunofgc | 1:0e0967c88590 | 329 | break; |
brunofgc | 1:0e0967c88590 | 330 | case 2: |
brunofgc | 1:0e0967c88590 | 331 | SD3.write(v_float); |
brunofgc | 1:0e0967c88590 | 332 | break; |
brunofgc | 1:0e0967c88590 | 333 | case 3: |
brunofgc | 1:0e0967c88590 | 334 | SD4.write(v_float); |
brunofgc | 1:0e0967c88590 | 335 | break; |
brunofgc | 1:0e0967c88590 | 336 | case 4: |
brunofgc | 1:0e0967c88590 | 337 | SD5.write(v_float); |
brunofgc | 1:0e0967c88590 | 338 | break; |
brunofgc | 1:0e0967c88590 | 339 | case 5: |
brunofgc | 1:0e0967c88590 | 340 | SD6.write(v_float); |
brunofgc | 1:0e0967c88590 | 341 | break; |
brunofgc | 0:1c0a769988ee | 342 | } |
brunofgc | 1:0e0967c88590 | 343 | 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 | 344 | /*sprintf(diversos::msg,"WriteFloat_ack"); |
brunofgc | 1:0e0967c88590 | 345 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 1:0e0967c88590 | 346 | |
brunofgc | 1:0e0967c88590 | 347 | sprintf(msg,"WritePWM_ack"); |
brunofgc | 1:0e0967c88590 | 348 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 349 | } |
brunofgc | 0:1c0a769988ee | 350 | |
brunofgc | 0:1c0a769988ee | 351 | //uint32_t |
brunofgc | 0:1c0a769988ee | 352 | if(strstr(ptr,"uint32_t,")){ |
brunofgc | 0:1c0a769988ee | 353 | uint32_t v_uint32_t; |
brunofgc | 0:1c0a769988ee | 354 | |
brunofgc | 0:1c0a769988ee | 355 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 356 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 357 | |
brunofgc | 0:1c0a769988ee | 358 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 359 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 360 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 361 | |
brunofgc | 0:1c0a769988ee | 362 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 363 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 364 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 365 | |
brunofgc | 0:1c0a769988ee | 366 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 367 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 368 | v_uint32_t = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 369 | |
brunofgc | 0:1c0a769988ee | 370 | if(!modBusMaster1::writeRegister32BIT(addr,reg,1,&v_uint32_t)){ |
brunofgc | 0:1c0a769988ee | 371 | pc.printf("Valor <%lu> escrito no registrador %lu do endereco modbus %u.\n",v_uint32_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 372 | /*sprintf(diversos::msg,"Write_uint32_t_ack"); |
brunofgc | 0:1c0a769988ee | 373 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 374 | |
brunofgc | 1:0e0967c88590 | 375 | sprintf(msg,"Write_uint32_t_ack"); |
brunofgc | 0:1c0a769988ee | 376 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 377 | }else{ |
brunofgc | 0:1c0a769988ee | 378 | pc.printf("Erro ao escrever valor <%lu> no registrador %lu do endereco modbus %u.\n",v_uint32_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 379 | /*sprintf(diversos::msg,"Write_uint32_t_nack"); |
brunofgc | 0:1c0a769988ee | 380 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 381 | |
brunofgc | 1:0e0967c88590 | 382 | sprintf(msg,"Write_uint32_t_nack"); |
brunofgc | 0:1c0a769988ee | 383 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 384 | } |
brunofgc | 0:1c0a769988ee | 385 | } |
brunofgc | 0:1c0a769988ee | 386 | |
brunofgc | 0:1c0a769988ee | 387 | //uint16_t |
brunofgc | 0:1c0a769988ee | 388 | if(strstr(ptr,"uint16_t,")){ |
brunofgc | 0:1c0a769988ee | 389 | uint16_t v_uint16_t; |
brunofgc | 0:1c0a769988ee | 390 | |
brunofgc | 0:1c0a769988ee | 391 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 392 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 393 | |
brunofgc | 0:1c0a769988ee | 394 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 395 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 396 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 397 | |
brunofgc | 0:1c0a769988ee | 398 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 399 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 400 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 401 | |
brunofgc | 0:1c0a769988ee | 402 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 403 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 404 | v_uint16_t = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 405 | |
brunofgc | 0:1c0a769988ee | 406 | if(!modBusMaster1::writeRegister16BIT(addr,reg,1,&v_uint16_t)){ |
brunofgc | 0:1c0a769988ee | 407 | pc.printf("Valor <%lu> escrito no registrador %lu do endereco modbus %u.\n",v_uint16_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 408 | /*sprintf(diversos::msg,"Write_uint16_t_ack"); |
brunofgc | 0:1c0a769988ee | 409 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 410 | |
brunofgc | 1:0e0967c88590 | 411 | sprintf(msg,"Write_uint16_t_ack"); |
brunofgc | 0:1c0a769988ee | 412 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 413 | }else{ |
brunofgc | 0:1c0a769988ee | 414 | pc.printf("Erro ao escrever valor <%lu> no registrador %lu do endereco modbus %u.\n",v_uint16_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 415 | /*sprintf(diversos::msg,"Write_uint16_t_nack"); |
brunofgc | 0:1c0a769988ee | 416 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg)); */ |
brunofgc | 0:1c0a769988ee | 417 | |
brunofgc | 1:0e0967c88590 | 418 | sprintf(msg,"Write_uint16_t_nack"); |
brunofgc | 0:1c0a769988ee | 419 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 420 | } |
brunofgc | 0:1c0a769988ee | 421 | } |
brunofgc | 0:1c0a769988ee | 422 | |
brunofgc | 0:1c0a769988ee | 423 | //bit |
brunofgc | 0:1c0a769988ee | 424 | if(strstr(ptr,"bit,")){ |
brunofgc | 0:1c0a769988ee | 425 | bool v_bool; |
brunofgc | 0:1c0a769988ee | 426 | |
brunofgc | 0:1c0a769988ee | 427 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 428 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 429 | |
brunofgc | 0:1c0a769988ee | 430 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 431 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 432 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 433 | |
brunofgc | 0:1c0a769988ee | 434 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 435 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 436 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 437 | |
brunofgc | 0:1c0a769988ee | 438 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 439 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 440 | v_bool = (atoi(pChar) > 0); |
brunofgc | 0:1c0a769988ee | 441 | |
brunofgc | 0:1c0a769988ee | 442 | if(addr != 254){ |
brunofgc | 0:1c0a769988ee | 443 | //writeSingleCoil(uint8_t,uint16_t,bool); //Endereço slave, registrador, bool |
brunofgc | 0:1c0a769988ee | 444 | if(!modBusMaster1::writeSingleCoil(addr,reg,v_bool)){ |
brunofgc | 0:1c0a769988ee | 445 | pc.printf("Valor <%u> escrito no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr); |
brunofgc | 0:1c0a769988ee | 446 | /*sprintf(diversos::msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 447 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 448 | |
brunofgc | 1:0e0967c88590 | 449 | sprintf(msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 450 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 451 | }else{ |
brunofgc | 0:1c0a769988ee | 452 | pc.printf("Erro ao escrever valor <%u> no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr); |
brunofgc | 0:1c0a769988ee | 453 | /*sprintf(diversos::msg,"Write_bool_nack"); |
brunofgc | 0:1c0a769988ee | 454 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 455 | |
brunofgc | 1:0e0967c88590 | 456 | sprintf(msg,"Write_bool_nack"); |
brunofgc | 0:1c0a769988ee | 457 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 458 | } |
brunofgc | 0:1c0a769988ee | 459 | }else{ |
brunofgc | 0:1c0a769988ee | 460 | switch(reg){ |
brunofgc | 0:1c0a769988ee | 461 | case 0: |
brunofgc | 0:1c0a769988ee | 462 | SD1.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 463 | break; |
brunofgc | 0:1c0a769988ee | 464 | case 1: |
brunofgc | 0:1c0a769988ee | 465 | SD2.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 466 | break; |
brunofgc | 0:1c0a769988ee | 467 | case 2: |
brunofgc | 0:1c0a769988ee | 468 | SD3.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 469 | break; |
brunofgc | 0:1c0a769988ee | 470 | case 3: |
brunofgc | 0:1c0a769988ee | 471 | SD4.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 472 | break; |
brunofgc | 0:1c0a769988ee | 473 | case 4: |
brunofgc | 0:1c0a769988ee | 474 | SD5.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 475 | break; |
brunofgc | 0:1c0a769988ee | 476 | case 5: |
brunofgc | 0:1c0a769988ee | 477 | SD6.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 478 | break; |
brunofgc | 0:1c0a769988ee | 479 | case 6: |
brunofgc | 0:1c0a769988ee | 480 | SD7 = v_bool; |
brunofgc | 0:1c0a769988ee | 481 | break; |
brunofgc | 0:1c0a769988ee | 482 | case 7: |
brunofgc | 0:1c0a769988ee | 483 | SD8 = v_bool; |
brunofgc | 0:1c0a769988ee | 484 | break; |
brunofgc | 0:1c0a769988ee | 485 | } |
brunofgc | 0:1c0a769988ee | 486 | pc.printf("Valor <%u> escrito no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr); |
brunofgc | 0:1c0a769988ee | 487 | /*sprintf(diversos::msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 488 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 489 | |
brunofgc | 1:0e0967c88590 | 490 | sprintf(msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 491 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 492 | } |
brunofgc | 0:1c0a769988ee | 493 | } |
brunofgc | 0:1c0a769988ee | 494 | modemCom::status.timeOut=10; |
brunofgc | 0:1c0a769988ee | 495 | } |
brunofgc | 0:1c0a769988ee | 496 | |
brunofgc | 0:1c0a769988ee | 497 | currentCur = strlen(ptrComando) + 1; //Aqui eu pego onde devo buscar novamente mais algum comando. |
brunofgc | 0:1c0a769988ee | 498 | if(currentCur>=totalLengthCommands){ |
brunofgc | 0:1c0a769988ee | 499 | //modemCom::status.timeOut=10; |
brunofgc | 0:1c0a769988ee | 500 | ptrComando = NULL; |
brunofgc | 0:1c0a769988ee | 501 | }else{ |
brunofgc | 0:1c0a769988ee | 502 | ptrComando = strtok(&ptrComando[currentCur],";"); |
brunofgc | 0:1c0a769988ee | 503 | } |
brunofgc | 0:1c0a769988ee | 504 | }while(ptrComando != NULL);//Este loop executa cada instrução. |
brunofgc | 0:1c0a769988ee | 505 | pc.printf("Atendidos todos os comandos.\n"); |
brunofgc | 0:1c0a769988ee | 506 | } |