teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
modbusMaster1.cpp@38:07d3907b74e5, 2018-06-08 (annotated)
- Committer:
- brunofgc
- Date:
- Fri Jun 08 22:14:21 2018 +0000
- Revision:
- 38:07d3907b74e5
- Parent:
- 37:0e95c85f0160
teste de publish para compilar no mbed-cli
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:1c0a769988ee | 1 | #include "modbusMaster1.h" |
brunofgc | 32:7cf1fb8a8bf3 | 2 | #define maxRetentativas 5 |
brunofgc | 32:7cf1fb8a8bf3 | 3 | #define maxDelayEntreTentativas 100 |
brunofgc | 0:1c0a769988ee | 4 | //RAD |
brunofgc | 0:1c0a769988ee | 5 | enum { |
brunofgc | 0:1c0a769988ee | 6 | READ_COIL_STATUS = 1, |
brunofgc | 0:1c0a769988ee | 7 | READ_INPUT_STATUS, |
brunofgc | 0:1c0a769988ee | 8 | READ_HOLDING_REGISTERS, |
brunofgc | 0:1c0a769988ee | 9 | READ_INPUT_REGISTERS, |
brunofgc | 0:1c0a769988ee | 10 | WRITE_SINGLE_COIL, |
brunofgc | 0:1c0a769988ee | 11 | WRITE_SINGLE_REGISTER, |
brunofgc | 0:1c0a769988ee | 12 | WRITE_MULTIPLE_COILS, |
brunofgc | 0:1c0a769988ee | 13 | WRITE_MULTIPLE_REGISTERS |
brunofgc | 0:1c0a769988ee | 14 | }modBusFunctions; |
brunofgc | 0:1c0a769988ee | 15 | |
brunofgc | 26:c246eacf6815 | 16 | uint16_t modBusMaster1::T3_5; |
brunofgc | 26:c246eacf6815 | 17 | uint16_t modBusMaster1::T1_5; |
brunofgc | 0:1c0a769988ee | 18 | bool modBusMaster1::pacoteEmEspera; |
brunofgc | 32:7cf1fb8a8bf3 | 19 | bool modBusMaster1::pacoteEmEsperaValido; |
brunofgc | 25:a6da63ed025b | 20 | uint16_t modBusMaster1::MODBUS_TIMEOUT; |
brunofgc | 0:1c0a769988ee | 21 | bool modBusMaster1::startThreadModBusMaster; |
brunofgc | 0:1c0a769988ee | 22 | Serial *modBusMaster1::serModBus; |
brunofgc | 0:1c0a769988ee | 23 | DigitalOut *modBusMaster1::de; |
brunofgc | 0:1c0a769988ee | 24 | uint16_t modBusMaster1::MODBUS_SERIAL_BAUD; |
brunofgc | 0:1c0a769988ee | 25 | |
brunofgc | 0:1c0a769988ee | 26 | uint8_t modBusMaster1::buffer[maxLenBufModBus]; //Buffer in e out; |
brunofgc | 0:1c0a769988ee | 27 | uint16_t modBusMaster1::index; |
brunofgc | 0:1c0a769988ee | 28 | |
brunofgc | 0:1c0a769988ee | 29 | //Timer |
brunofgc | 0:1c0a769988ee | 30 | osTimerId modBusMaster1::timer_pacote; |
brunofgc | 0:1c0a769988ee | 31 | osTimerDef(timerProcessaPacoteModBusMaster1,modBusMaster1::processaPacote); |
brunofgc | 0:1c0a769988ee | 32 | //Timer |
brunofgc | 0:1c0a769988ee | 33 | |
brunofgc | 0:1c0a769988ee | 34 | void modBusMaster1::processaPacote(void const *){ |
brunofgc | 0:1c0a769988ee | 35 | //Validando CRC |
brunofgc | 0:1c0a769988ee | 36 | uint16_t crc_calculado; |
brunofgc | 0:1c0a769988ee | 37 | uint16_t crc_lido; |
brunofgc | 0:1c0a769988ee | 38 | |
brunofgc | 25:a6da63ed025b | 39 | if(debug){ |
brunofgc | 25:a6da63ed025b | 40 | //Lido resposta DEBUG |
brunofgc | 25:a6da63ed025b | 41 | pc.printf("Lido de resposta modbus <"); |
brunofgc | 25:a6da63ed025b | 42 | for(crc_lido = 0; crc_lido < modBusMaster1::index; crc_lido++){ |
brunofgc | 25:a6da63ed025b | 43 | pc.printf("%02X ",modBusMaster1::buffer[crc_lido]); |
brunofgc | 25:a6da63ed025b | 44 | } |
brunofgc | 25:a6da63ed025b | 45 | pc.printf(">.\n"); |
brunofgc | 0:1c0a769988ee | 46 | } |
brunofgc | 0:1c0a769988ee | 47 | if(modBusMaster1::index<3){ |
brunofgc | 0:1c0a769988ee | 48 | modBusMaster1::pacoteEmEspera=false; |
brunofgc | 0:1c0a769988ee | 49 | return; |
brunofgc | 0:1c0a769988ee | 50 | } |
brunofgc | 0:1c0a769988ee | 51 | |
brunofgc | 0:1c0a769988ee | 52 | crc_calculado = modBusMaster1::CRC16(modBusMaster1::buffer,modBusMaster1::index-2); |
brunofgc | 0:1c0a769988ee | 53 | crc_lido = (modBusMaster1::buffer[modBusMaster1::index-2]<<8)+(modBusMaster1::buffer[modBusMaster1::index-1]); |
brunofgc | 0:1c0a769988ee | 54 | |
brunofgc | 32:7cf1fb8a8bf3 | 55 | modBusMaster1::pacoteEmEspera=true; |
brunofgc | 0:1c0a769988ee | 56 | if(crc_calculado == crc_lido){ |
brunofgc | 32:7cf1fb8a8bf3 | 57 | modBusMaster1::pacoteEmEsperaValido=true; |
brunofgc | 0:1c0a769988ee | 58 | } |
brunofgc | 0:1c0a769988ee | 59 | //pc.printf("crc_calculado = 0x%02x, crc_lido = 0x%02x.\n",crc_calculado,crc_lido); |
brunofgc | 0:1c0a769988ee | 60 | } |
brunofgc | 0:1c0a769988ee | 61 | |
brunofgc | 0:1c0a769988ee | 62 | uint16_t modBusMaster1::CRC16(uint8_t *buf,uint16_t len){ |
brunofgc | 0:1c0a769988ee | 63 | uint16_t crc = 0xFFFF; |
brunofgc | 0:1c0a769988ee | 64 | uint16_t pos; |
brunofgc | 0:1c0a769988ee | 65 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 66 | for (pos = 0; pos < len; pos++) { |
brunofgc | 0:1c0a769988ee | 67 | crc ^= buf[pos]; // XOR byte into least sig. byte of crc |
brunofgc | 0:1c0a769988ee | 68 | |
brunofgc | 0:1c0a769988ee | 69 | for (i = 8; i != 0; i--) { // Loop over each bit |
brunofgc | 0:1c0a769988ee | 70 | if ((crc & 0x0001) != 0) { // If the LSB is set |
brunofgc | 0:1c0a769988ee | 71 | crc >>= 1; // Shift right and XOR 0xA001 |
brunofgc | 0:1c0a769988ee | 72 | crc ^= 0xA001; |
brunofgc | 0:1c0a769988ee | 73 | } |
brunofgc | 0:1c0a769988ee | 74 | else // Else LSB is not set |
brunofgc | 0:1c0a769988ee | 75 | crc >>= 1; // Just shift right |
brunofgc | 0:1c0a769988ee | 76 | } |
brunofgc | 0:1c0a769988ee | 77 | } |
brunofgc | 0:1c0a769988ee | 78 | // Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes) |
brunofgc | 0:1c0a769988ee | 79 | return ((crc<<8)+(crc>>8)); |
brunofgc | 0:1c0a769988ee | 80 | } |
brunofgc | 0:1c0a769988ee | 81 | |
brunofgc | 0:1c0a769988ee | 82 | void modBusMaster1::modBusMaster(Serial *serial,uint32_t baud,DigitalOut *pinDe){ |
brunofgc | 0:1c0a769988ee | 83 | modBusMaster1::serModBus = serial; |
brunofgc | 0:1c0a769988ee | 84 | modBusMaster1::de = pinDe; |
brunofgc | 0:1c0a769988ee | 85 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 86 | *modBusMaster1::de = 0; |
brunofgc | 0:1c0a769988ee | 87 | modBusMaster1::serModBus->attach(&modBusMaster1::processaCaractere); |
brunofgc | 0:1c0a769988ee | 88 | modBusMaster1::serModBus->baud(baud); |
brunofgc | 25:a6da63ed025b | 89 | modBusMaster1::pacoteEmEspera = false; |
brunofgc | 0:1c0a769988ee | 90 | //Criando timer_pacote |
brunofgc | 0:1c0a769988ee | 91 | modBusMaster1::timer_pacote = osTimerCreate(osTimer(timerProcessaPacoteModBusMaster1),osTimerOnce,NULL); |
brunofgc | 0:1c0a769988ee | 92 | } |
brunofgc | 0:1c0a769988ee | 93 | |
brunofgc | 25:a6da63ed025b | 94 | void modBusMaster1::setBaud(uint32_t baud){ |
brunofgc | 25:a6da63ed025b | 95 | modBusMaster1::serModBus->baud(baud); |
brunofgc | 26:c246eacf6815 | 96 | if (baud > 19200){ |
brunofgc | 26:c246eacf6815 | 97 | modBusMaster1::T1_5 = 750; |
brunofgc | 26:c246eacf6815 | 98 | modBusMaster1::T3_5 = 1750; |
brunofgc | 26:c246eacf6815 | 99 | }else { |
brunofgc | 26:c246eacf6815 | 100 | modBusMaster1::T1_5 = 15000000/baud; |
brunofgc | 26:c246eacf6815 | 101 | modBusMaster1::T3_5 = 35000000/baud; |
brunofgc | 26:c246eacf6815 | 102 | } |
brunofgc | 25:a6da63ed025b | 103 | } |
brunofgc | 25:a6da63ed025b | 104 | |
brunofgc | 25:a6da63ed025b | 105 | |
brunofgc | 25:a6da63ed025b | 106 | |
brunofgc | 0:1c0a769988ee | 107 | void modBusMaster1::processaCaractere(){ |
brunofgc | 0:1c0a769988ee | 108 | uint32_t RBR = LPC_UART1->RBR; //Reset RBR interrupt flag e captura o caractere entrante |
brunofgc | 0:1c0a769988ee | 109 | modBusMaster1::buffer[modBusMaster1::index]=RBR; |
brunofgc | 0:1c0a769988ee | 110 | modBusMaster1::index++; |
brunofgc | 0:1c0a769988ee | 111 | if(modBusMaster1::index>=maxLenBufModBus){ |
brunofgc | 0:1c0a769988ee | 112 | modBusMaster1::index=modBusMaster1::index-1; |
brunofgc | 0:1c0a769988ee | 113 | } |
brunofgc | 0:1c0a769988ee | 114 | modBusMaster1::startThreadModBusMaster=true; |
brunofgc | 0:1c0a769988ee | 115 | osSignalSet(idThreadTimers, 0x1); //Envia sinal para a thread de manipulação dos timers para ativar os timers agendados |
brunofgc | 0:1c0a769988ee | 116 | //comLedIn=!comLedIn; |
brunofgc | 0:1c0a769988ee | 117 | //pc.printf("%c",RBR); |
brunofgc | 0:1c0a769988ee | 118 | } |
brunofgc | 0:1c0a769988ee | 119 | |
brunofgc | 0:1c0a769988ee | 120 | uint16_t modBusMaster1::sendFrame(uint16_t tamBytes){ |
brunofgc | 25:a6da63ed025b | 121 | uint16_t timeout = modBusMaster1::MODBUS_TIMEOUT; |
brunofgc | 37:0e95c85f0160 | 122 | timeout /=10; |
brunofgc | 0:1c0a769988ee | 123 | uint16_t i; |
brunofgc | 26:c246eacf6815 | 124 | |
brunofgc | 32:7cf1fb8a8bf3 | 125 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 126 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 127 | pc.printf("Frame enviado (hexa) <"); |
brunofgc | 25:a6da63ed025b | 128 | for(i=0;i<tamBytes;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 129 | pc.printf("%02x ",modBusMaster1::buffer[i]); |
brunofgc | 25:a6da63ed025b | 130 | } |
brunofgc | 32:7cf1fb8a8bf3 | 131 | pc.printf(">.\n"); |
brunofgc | 25:a6da63ed025b | 132 | } |
brunofgc | 32:7cf1fb8a8bf3 | 133 | |
brunofgc | 32:7cf1fb8a8bf3 | 134 | //osDelay(5); |
brunofgc | 32:7cf1fb8a8bf3 | 135 | |
brunofgc | 32:7cf1fb8a8bf3 | 136 | modBusMaster1::pacoteEmEspera = false; |
brunofgc | 32:7cf1fb8a8bf3 | 137 | modBusMaster1::index=0; |
brunofgc | 32:7cf1fb8a8bf3 | 138 | *modBusMaster1::de=1; |
brunofgc | 32:7cf1fb8a8bf3 | 139 | //wait_us(1750); |
brunofgc | 32:7cf1fb8a8bf3 | 140 | osDelay(1); |
brunofgc | 32:7cf1fb8a8bf3 | 141 | for(i=0;i<tamBytes;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 142 | modBusMaster1::serModBus->putc(modBusMaster1::buffer[i]); |
brunofgc | 32:7cf1fb8a8bf3 | 143 | //wait_us(750); |
brunofgc | 32:7cf1fb8a8bf3 | 144 | while( |
brunofgc | 32:7cf1fb8a8bf3 | 145 | ((LPC_UART1->LSR >> 6) &0x1) |
brunofgc | 32:7cf1fb8a8bf3 | 146 | == 0 ); |
brunofgc | 32:7cf1fb8a8bf3 | 147 | //wait_us(modBusMaster1::T1_5); |
brunofgc | 32:7cf1fb8a8bf3 | 148 | } |
brunofgc | 32:7cf1fb8a8bf3 | 149 | //wait_us(1750); |
brunofgc | 32:7cf1fb8a8bf3 | 150 | //osDelay(1); |
brunofgc | 32:7cf1fb8a8bf3 | 151 | *modBusMaster1::de=0; |
brunofgc | 32:7cf1fb8a8bf3 | 152 | |
brunofgc | 32:7cf1fb8a8bf3 | 153 | while((timeout>0)&&(modBusMaster1::pacoteEmEspera!=true)){ |
brunofgc | 37:0e95c85f0160 | 154 | osDelay(10); |
brunofgc | 32:7cf1fb8a8bf3 | 155 | //wait_us(1000); |
brunofgc | 32:7cf1fb8a8bf3 | 156 | timeout--; |
brunofgc | 32:7cf1fb8a8bf3 | 157 | } |
brunofgc | 32:7cf1fb8a8bf3 | 158 | wait_us(modBusMaster1::T3_5*2); |
brunofgc | 0:1c0a769988ee | 159 | return timeout; |
brunofgc | 0:1c0a769988ee | 160 | } |
brunofgc | 0:1c0a769988ee | 161 | |
brunofgc | 0:1c0a769988ee | 162 | uint8_t modBusMaster1::readRegister16BIT(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,uint16_t *var){ |
brunofgc | 0:1c0a769988ee | 163 | union{ |
brunofgc | 0:1c0a769988ee | 164 | char c[2]; |
brunofgc | 0:1c0a769988ee | 165 | uint16_t v; |
brunofgc | 0:1c0a769988ee | 166 | }u; |
brunofgc | 0:1c0a769988ee | 167 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 168 | uint16_t qtd_dados_recebidos; |
brunofgc | 0:1c0a769988ee | 169 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 170 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 171 | |
brunofgc | 18:1eefda1f7736 | 172 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 18:1eefda1f7736 | 173 | var[i]=NULL; |
brunofgc | 18:1eefda1f7736 | 174 | } |
brunofgc | 32:7cf1fb8a8bf3 | 175 | |
brunofgc | 32:7cf1fb8a8bf3 | 176 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 177 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 178 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 179 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 180 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 181 | modBusMaster1::buffer[1]=funcao; |
brunofgc | 32:7cf1fb8a8bf3 | 182 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 183 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 184 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 185 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 186 | |
brunofgc | 32:7cf1fb8a8bf3 | 187 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 188 | |
brunofgc | 32:7cf1fb8a8bf3 | 189 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 190 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 191 | |
brunofgc | 32:7cf1fb8a8bf3 | 192 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 193 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 194 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 195 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 196 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 197 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 198 | } |
brunofgc | 32:7cf1fb8a8bf3 | 199 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 200 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 201 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 202 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 203 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 204 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 205 | } |
brunofgc | 32:7cf1fb8a8bf3 | 206 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 207 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 208 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 209 | } |
brunofgc | 0:1c0a769988ee | 210 | } |
brunofgc | 32:7cf1fb8a8bf3 | 211 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 212 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 213 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 214 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 215 | return 255; |
brunofgc | 0:1c0a769988ee | 216 | } |
brunofgc | 32:7cf1fb8a8bf3 | 217 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 218 | |
brunofgc | 0:1c0a769988ee | 219 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 220 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 221 | }else{ |
brunofgc | 0:1c0a769988ee | 222 | qtd_dados_recebidos = modBusMaster1::buffer[2]/2; |
brunofgc | 0:1c0a769988ee | 223 | for(i=0;i<qtd_dados_recebidos;i++){ |
brunofgc | 0:1c0a769988ee | 224 | u.c[1]=modBusMaster1::buffer[(i*2)+3]; |
brunofgc | 0:1c0a769988ee | 225 | u.c[0]=modBusMaster1::buffer[(i*2)+4]; |
brunofgc | 0:1c0a769988ee | 226 | var[i]=u.v; |
brunofgc | 0:1c0a769988ee | 227 | } |
brunofgc | 0:1c0a769988ee | 228 | result = 0; |
brunofgc | 0:1c0a769988ee | 229 | } |
brunofgc | 0:1c0a769988ee | 230 | |
brunofgc | 0:1c0a769988ee | 231 | |
brunofgc | 0:1c0a769988ee | 232 | |
brunofgc | 0:1c0a769988ee | 233 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 234 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 235 | return result; |
brunofgc | 0:1c0a769988ee | 236 | } |
brunofgc | 0:1c0a769988ee | 237 | |
brunofgc | 0:1c0a769988ee | 238 | uint8_t modBusMaster1::writeSingleCoil(uint8_t enderecoSlave,uint16_t registrador,bool var){ |
brunofgc | 0:1c0a769988ee | 239 | uint16_t crc; |
brunofgc | 18:1eefda1f7736 | 240 | uint16_t estadoSetado; |
brunofgc | 32:7cf1fb8a8bf3 | 241 | uint8_t result = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 242 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 243 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 244 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 245 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 246 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 247 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 248 | modBusMaster1::buffer[1]=5; |
brunofgc | 32:7cf1fb8a8bf3 | 249 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 250 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 251 | |
brunofgc | 32:7cf1fb8a8bf3 | 252 | if(var){ |
brunofgc | 32:7cf1fb8a8bf3 | 253 | modBusMaster1::buffer[4]=0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 254 | modBusMaster1::buffer[5]=0x00; |
brunofgc | 32:7cf1fb8a8bf3 | 255 | estadoSetado = 0xFF00; |
brunofgc | 32:7cf1fb8a8bf3 | 256 | |
brunofgc | 32:7cf1fb8a8bf3 | 257 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 258 | modBusMaster1::buffer[4]=0x00; |
brunofgc | 32:7cf1fb8a8bf3 | 259 | modBusMaster1::buffer[5]=0x00; |
brunofgc | 32:7cf1fb8a8bf3 | 260 | estadoSetado = 0x0000; |
brunofgc | 32:7cf1fb8a8bf3 | 261 | } |
brunofgc | 32:7cf1fb8a8bf3 | 262 | |
brunofgc | 32:7cf1fb8a8bf3 | 263 | |
brunofgc | 32:7cf1fb8a8bf3 | 264 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 265 | |
brunofgc | 32:7cf1fb8a8bf3 | 266 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 267 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 268 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 269 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 270 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 271 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 272 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 273 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 274 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 275 | } |
brunofgc | 32:7cf1fb8a8bf3 | 276 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 277 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 278 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 279 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 280 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 281 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 282 | } |
brunofgc | 32:7cf1fb8a8bf3 | 283 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 284 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 285 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 286 | } |
brunofgc | 32:7cf1fb8a8bf3 | 287 | } |
brunofgc | 32:7cf1fb8a8bf3 | 288 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 32:7cf1fb8a8bf3 | 289 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 290 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 291 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 292 | return 255; |
brunofgc | 32:7cf1fb8a8bf3 | 293 | } |
brunofgc | 32:7cf1fb8a8bf3 | 294 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 295 | |
brunofgc | 32:7cf1fb8a8bf3 | 296 | |
brunofgc | 32:7cf1fb8a8bf3 | 297 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 32:7cf1fb8a8bf3 | 298 | result = modBusMaster1::buffer[2]; |
brunofgc | 32:7cf1fb8a8bf3 | 299 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 300 | if( |
brunofgc | 32:7cf1fb8a8bf3 | 301 | (((modBusMaster1::buffer[2]<<8)+(modBusMaster1::buffer[3]))==registrador) |
brunofgc | 32:7cf1fb8a8bf3 | 302 | && |
brunofgc | 32:7cf1fb8a8bf3 | 303 | (((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))==estadoSetado) |
brunofgc | 32:7cf1fb8a8bf3 | 304 | ){ |
brunofgc | 32:7cf1fb8a8bf3 | 305 | result=0; |
brunofgc | 32:7cf1fb8a8bf3 | 306 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 307 | result=253; |
brunofgc | 32:7cf1fb8a8bf3 | 308 | } |
brunofgc | 32:7cf1fb8a8bf3 | 309 | } |
brunofgc | 0:1c0a769988ee | 310 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 311 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 312 | return result; |
brunofgc | 0:1c0a769988ee | 313 | } |
brunofgc | 0:1c0a769988ee | 314 | |
brunofgc | 0:1c0a769988ee | 315 | uint8_t modBusMaster1::readCoils(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,bool *var){ |
brunofgc | 0:1c0a769988ee | 316 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 317 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 318 | uint16_t i; |
brunofgc | 32:7cf1fb8a8bf3 | 319 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 320 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 321 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 322 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 323 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 324 | |
brunofgc | 32:7cf1fb8a8bf3 | 325 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 326 | modBusMaster1::buffer[1]=1; |
brunofgc | 32:7cf1fb8a8bf3 | 327 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 328 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 329 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 330 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 331 | |
brunofgc | 32:7cf1fb8a8bf3 | 332 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 333 | |
brunofgc | 32:7cf1fb8a8bf3 | 334 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 335 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 336 | |
brunofgc | 32:7cf1fb8a8bf3 | 337 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 338 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 339 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 340 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 341 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 342 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 343 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 344 | } |
brunofgc | 32:7cf1fb8a8bf3 | 345 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 346 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 347 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 348 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 349 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 350 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 351 | } |
brunofgc | 32:7cf1fb8a8bf3 | 352 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 353 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 354 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 355 | } |
brunofgc | 0:1c0a769988ee | 356 | } |
brunofgc | 32:7cf1fb8a8bf3 | 357 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 358 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 359 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 360 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 361 | return 255; |
brunofgc | 0:1c0a769988ee | 362 | } |
brunofgc | 32:7cf1fb8a8bf3 | 363 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 364 | |
brunofgc | 0:1c0a769988ee | 365 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 366 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 367 | }else{ |
brunofgc | 0:1c0a769988ee | 368 | if( |
brunofgc | 0:1c0a769988ee | 369 | modBusMaster1::buffer[2] == (1+((uint16_t)(qtdRegistros/8))) |
brunofgc | 0:1c0a769988ee | 370 | ){ |
brunofgc | 0:1c0a769988ee | 371 | //Bloco de leitura das coils |
brunofgc | 0:1c0a769988ee | 372 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 0:1c0a769988ee | 373 | var[i] = ( |
brunofgc | 0:1c0a769988ee | 374 | (modBusMaster1::buffer[(i/8)+3] & ( 0x1 << (i%8) )) >0 |
brunofgc | 0:1c0a769988ee | 375 | ); |
brunofgc | 0:1c0a769988ee | 376 | } |
brunofgc | 0:1c0a769988ee | 377 | result = 0; |
brunofgc | 0:1c0a769988ee | 378 | }else{ |
brunofgc | 0:1c0a769988ee | 379 | result = 253; |
brunofgc | 0:1c0a769988ee | 380 | } |
brunofgc | 0:1c0a769988ee | 381 | } |
brunofgc | 0:1c0a769988ee | 382 | |
brunofgc | 0:1c0a769988ee | 383 | |
brunofgc | 0:1c0a769988ee | 384 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 385 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 386 | return result; |
brunofgc | 0:1c0a769988ee | 387 | } |
brunofgc | 0:1c0a769988ee | 388 | uint8_t modBusMaster1::readRegister32BIT(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,uint32_t *var){ |
brunofgc | 0:1c0a769988ee | 389 | union{ |
brunofgc | 0:1c0a769988ee | 390 | char c[4]; |
brunofgc | 0:1c0a769988ee | 391 | uint32_t v; |
brunofgc | 0:1c0a769988ee | 392 | }u; |
brunofgc | 0:1c0a769988ee | 393 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 394 | uint16_t qtd_dados_recebidos; |
brunofgc | 0:1c0a769988ee | 395 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 396 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 397 | |
brunofgc | 18:1eefda1f7736 | 398 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 18:1eefda1f7736 | 399 | var[i]=NULL; |
brunofgc | 18:1eefda1f7736 | 400 | } |
brunofgc | 32:7cf1fb8a8bf3 | 401 | |
brunofgc | 32:7cf1fb8a8bf3 | 402 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 403 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 404 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 405 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 406 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 407 | |
brunofgc | 32:7cf1fb8a8bf3 | 408 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 409 | modBusMaster1::buffer[1]=funcao; |
brunofgc | 32:7cf1fb8a8bf3 | 410 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 411 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 412 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 413 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 414 | |
brunofgc | 32:7cf1fb8a8bf3 | 415 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 416 | |
brunofgc | 32:7cf1fb8a8bf3 | 417 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 418 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 419 | |
brunofgc | 32:7cf1fb8a8bf3 | 420 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 421 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 422 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 423 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 424 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 425 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 426 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 427 | } |
brunofgc | 32:7cf1fb8a8bf3 | 428 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 429 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 430 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 431 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 432 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 433 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 434 | } |
brunofgc | 32:7cf1fb8a8bf3 | 435 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 436 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 437 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 438 | } |
brunofgc | 0:1c0a769988ee | 439 | } |
brunofgc | 32:7cf1fb8a8bf3 | 440 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 441 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 442 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 443 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 444 | return 255; |
brunofgc | 0:1c0a769988ee | 445 | } |
brunofgc | 32:7cf1fb8a8bf3 | 446 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 447 | |
brunofgc | 0:1c0a769988ee | 448 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 449 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 450 | }else{ |
brunofgc | 0:1c0a769988ee | 451 | |
brunofgc | 0:1c0a769988ee | 452 | qtd_dados_recebidos = modBusMaster1::buffer[2]/4; |
brunofgc | 0:1c0a769988ee | 453 | for(i=0;i<qtd_dados_recebidos;i++){ |
brunofgc | 0:1c0a769988ee | 454 | u.c[3]=modBusMaster1::buffer[(i*4)+3]; |
brunofgc | 0:1c0a769988ee | 455 | u.c[2]=modBusMaster1::buffer[(i*4)+4]; |
brunofgc | 0:1c0a769988ee | 456 | u.c[1]=modBusMaster1::buffer[(i*4)+5]; |
brunofgc | 0:1c0a769988ee | 457 | u.c[0]=modBusMaster1::buffer[(i*4)+6]; |
brunofgc | 0:1c0a769988ee | 458 | var[i]=u.v; |
brunofgc | 0:1c0a769988ee | 459 | } |
brunofgc | 0:1c0a769988ee | 460 | result =0; |
brunofgc | 0:1c0a769988ee | 461 | } |
brunofgc | 0:1c0a769988ee | 462 | |
brunofgc | 0:1c0a769988ee | 463 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 464 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 465 | return result; |
brunofgc | 0:1c0a769988ee | 466 | } |
brunofgc | 0:1c0a769988ee | 467 | |
brunofgc | 0:1c0a769988ee | 468 | uint8_t modBusMaster1::writeRegister16BIT(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,uint16_t *var){ |
brunofgc | 0:1c0a769988ee | 469 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 470 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 471 | uint8_t result; |
brunofgc | 32:7cf1fb8a8bf3 | 472 | |
brunofgc | 32:7cf1fb8a8bf3 | 473 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 474 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 475 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 476 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 477 | //------------Bloco de construcao de frame request---------- |
brunofgc | 0:1c0a769988ee | 478 | |
brunofgc | 32:7cf1fb8a8bf3 | 479 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 480 | modBusMaster1::buffer[1]=16; |
brunofgc | 32:7cf1fb8a8bf3 | 481 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 482 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 483 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 484 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 485 | modBusMaster1::buffer[6]=qtdRegistros*2; |
brunofgc | 32:7cf1fb8a8bf3 | 486 | |
brunofgc | 32:7cf1fb8a8bf3 | 487 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 488 | modBusMaster1::buffer[(i*2)+7]=var[i]>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 489 | modBusMaster1::buffer[(i*2)+8]=var[i]&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 490 | } |
brunofgc | 32:7cf1fb8a8bf3 | 491 | |
brunofgc | 32:7cf1fb8a8bf3 | 492 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*2)+7); |
brunofgc | 32:7cf1fb8a8bf3 | 493 | |
brunofgc | 32:7cf1fb8a8bf3 | 494 | modBusMaster1::buffer[(qtdRegistros*2)+7]=crc>>8;; |
brunofgc | 32:7cf1fb8a8bf3 | 495 | modBusMaster1::buffer[(qtdRegistros*2)+8]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 496 | |
brunofgc | 32:7cf1fb8a8bf3 | 497 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 498 | if(!modBusMaster1::sendFrame((qtdRegistros*2)+9)){ |
brunofgc | 32:7cf1fb8a8bf3 | 499 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 500 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 501 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 502 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 503 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 504 | } |
brunofgc | 32:7cf1fb8a8bf3 | 505 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 506 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 507 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 508 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 509 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 510 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 511 | } |
brunofgc | 32:7cf1fb8a8bf3 | 512 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 513 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 514 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 515 | } |
brunofgc | 0:1c0a769988ee | 516 | } |
brunofgc | 32:7cf1fb8a8bf3 | 517 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 518 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 519 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 520 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 521 | return 255; |
brunofgc | 0:1c0a769988ee | 522 | } |
brunofgc | 32:7cf1fb8a8bf3 | 523 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 524 | |
brunofgc | 0:1c0a769988ee | 525 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 526 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 527 | |
brunofgc | 0:1c0a769988ee | 528 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 529 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 530 | }else{ |
brunofgc | 0:1c0a769988ee | 531 | |
brunofgc | 0:1c0a769988ee | 532 | //Interpratando resposta |
brunofgc | 0:1c0a769988ee | 533 | if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){ |
brunofgc | 0:1c0a769988ee | 534 | return 253; |
brunofgc | 0:1c0a769988ee | 535 | }else{ |
brunofgc | 0:1c0a769988ee | 536 | result = 0; |
brunofgc | 0:1c0a769988ee | 537 | } |
brunofgc | 0:1c0a769988ee | 538 | } |
brunofgc | 0:1c0a769988ee | 539 | return result; |
brunofgc | 0:1c0a769988ee | 540 | } |
brunofgc | 0:1c0a769988ee | 541 | |
brunofgc | 0:1c0a769988ee | 542 | uint8_t modBusMaster1::writeRegister32BIT(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,uint32_t *var){ |
brunofgc | 0:1c0a769988ee | 543 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 544 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 545 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 546 | |
brunofgc | 32:7cf1fb8a8bf3 | 547 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 548 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 549 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 550 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 551 | //------------Bloco de construcao de frame request---------- |
brunofgc | 0:1c0a769988ee | 552 | |
brunofgc | 32:7cf1fb8a8bf3 | 553 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 554 | modBusMaster1::buffer[1]=16; |
brunofgc | 32:7cf1fb8a8bf3 | 555 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 556 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 557 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 558 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 559 | modBusMaster1::buffer[6]=qtdRegistros*4; |
brunofgc | 32:7cf1fb8a8bf3 | 560 | |
brunofgc | 32:7cf1fb8a8bf3 | 561 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 562 | modBusMaster1::buffer[(i*4)+7]=(var[i]>>24)&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 563 | modBusMaster1::buffer[(i*4)+8]=(var[i]>>16)&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 564 | modBusMaster1::buffer[(i*4)+9]=(var[i]>>8)&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 565 | modBusMaster1::buffer[(i*4)+10]=var[i]&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 566 | } |
brunofgc | 32:7cf1fb8a8bf3 | 567 | |
brunofgc | 32:7cf1fb8a8bf3 | 568 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*4)+7); |
brunofgc | 32:7cf1fb8a8bf3 | 569 | |
brunofgc | 32:7cf1fb8a8bf3 | 570 | modBusMaster1::buffer[(qtdRegistros*4)+7]=crc>>8;; |
brunofgc | 32:7cf1fb8a8bf3 | 571 | modBusMaster1::buffer[(qtdRegistros*4)+8]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 572 | |
brunofgc | 32:7cf1fb8a8bf3 | 573 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 574 | if(!modBusMaster1::sendFrame((qtdRegistros*4)+9)){ |
brunofgc | 32:7cf1fb8a8bf3 | 575 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 576 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 577 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 578 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 579 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 580 | } |
brunofgc | 32:7cf1fb8a8bf3 | 581 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 582 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 583 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 584 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 585 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 586 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 587 | } |
brunofgc | 32:7cf1fb8a8bf3 | 588 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 589 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 590 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 591 | } |
brunofgc | 0:1c0a769988ee | 592 | } |
brunofgc | 32:7cf1fb8a8bf3 | 593 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 594 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 595 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 596 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 597 | return 255; |
brunofgc | 0:1c0a769988ee | 598 | } |
brunofgc | 32:7cf1fb8a8bf3 | 599 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 600 | |
brunofgc | 0:1c0a769988ee | 601 | |
brunofgc | 0:1c0a769988ee | 602 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 603 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 604 | |
brunofgc | 0:1c0a769988ee | 605 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 606 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 607 | }else{ |
brunofgc | 0:1c0a769988ee | 608 | |
brunofgc | 0:1c0a769988ee | 609 | //Interpratando resposta |
brunofgc | 0:1c0a769988ee | 610 | if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){ |
brunofgc | 0:1c0a769988ee | 611 | return 253; |
brunofgc | 0:1c0a769988ee | 612 | }else{ |
brunofgc | 0:1c0a769988ee | 613 | result = 0; |
brunofgc | 0:1c0a769988ee | 614 | } |
brunofgc | 0:1c0a769988ee | 615 | } |
brunofgc | 0:1c0a769988ee | 616 | return result; |
brunofgc | 0:1c0a769988ee | 617 | } |
brunofgc | 0:1c0a769988ee | 618 | |
brunofgc | 0:1c0a769988ee | 619 | uint8_t modBusMaster1::writeFloat(uint8_t enderecoSlave,uint16_t registrador,uint8_t qtdRegistros,float *var){ |
brunofgc | 0:1c0a769988ee | 620 | union{ |
brunofgc | 0:1c0a769988ee | 621 | char c[4]; |
brunofgc | 0:1c0a769988ee | 622 | float v; |
brunofgc | 0:1c0a769988ee | 623 | }u; |
brunofgc | 0:1c0a769988ee | 624 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 625 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 626 | uint8_t result; |
brunofgc | 32:7cf1fb8a8bf3 | 627 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 628 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 629 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 630 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 631 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 632 | |
brunofgc | 32:7cf1fb8a8bf3 | 633 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 634 | modBusMaster1::buffer[1]=16; |
brunofgc | 32:7cf1fb8a8bf3 | 635 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 636 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 637 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 638 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 639 | modBusMaster1::buffer[6]=qtdRegistros*4; |
brunofgc | 32:7cf1fb8a8bf3 | 640 | |
brunofgc | 32:7cf1fb8a8bf3 | 641 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 642 | u.v = var[i]; |
brunofgc | 32:7cf1fb8a8bf3 | 643 | modBusMaster1::buffer[(i*4)+7]=u.c[3]; |
brunofgc | 32:7cf1fb8a8bf3 | 644 | modBusMaster1::buffer[(i*4)+8]=u.c[2]; |
brunofgc | 32:7cf1fb8a8bf3 | 645 | modBusMaster1::buffer[(i*4)+9]=u.c[1]; |
brunofgc | 32:7cf1fb8a8bf3 | 646 | modBusMaster1::buffer[(i*4)+10]=u.c[0]; |
brunofgc | 32:7cf1fb8a8bf3 | 647 | } |
brunofgc | 32:7cf1fb8a8bf3 | 648 | |
brunofgc | 32:7cf1fb8a8bf3 | 649 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*4)+7); |
brunofgc | 32:7cf1fb8a8bf3 | 650 | |
brunofgc | 32:7cf1fb8a8bf3 | 651 | modBusMaster1::buffer[(qtdRegistros*4)+7]=crc>>8;; |
brunofgc | 32:7cf1fb8a8bf3 | 652 | modBusMaster1::buffer[(qtdRegistros*4)+8]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 653 | |
brunofgc | 32:7cf1fb8a8bf3 | 654 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 655 | if(!modBusMaster1::sendFrame((qtdRegistros*4)+9)){ |
brunofgc | 32:7cf1fb8a8bf3 | 656 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 657 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 658 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 659 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 660 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 661 | } |
brunofgc | 32:7cf1fb8a8bf3 | 662 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 663 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 664 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 665 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 666 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 667 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 668 | } |
brunofgc | 32:7cf1fb8a8bf3 | 669 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 670 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 671 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 672 | } |
brunofgc | 0:1c0a769988ee | 673 | } |
brunofgc | 32:7cf1fb8a8bf3 | 674 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 675 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 676 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 677 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 678 | return 255; |
brunofgc | 0:1c0a769988ee | 679 | } |
brunofgc | 32:7cf1fb8a8bf3 | 680 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 681 | |
brunofgc | 0:1c0a769988ee | 682 | |
brunofgc | 0:1c0a769988ee | 683 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 684 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 685 | |
brunofgc | 0:1c0a769988ee | 686 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 687 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 688 | }else{ |
brunofgc | 0:1c0a769988ee | 689 | //Interpratando resposta |
brunofgc | 0:1c0a769988ee | 690 | if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){ |
brunofgc | 0:1c0a769988ee | 691 | return 253; |
brunofgc | 0:1c0a769988ee | 692 | }else{ |
brunofgc | 0:1c0a769988ee | 693 | result = 0; |
brunofgc | 0:1c0a769988ee | 694 | } |
brunofgc | 0:1c0a769988ee | 695 | } |
brunofgc | 0:1c0a769988ee | 696 | return result; |
brunofgc | 0:1c0a769988ee | 697 | } |
brunofgc | 0:1c0a769988ee | 698 | |
brunofgc | 0:1c0a769988ee | 699 | uint8_t modBusMaster1::readFloat(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,float *var){ |
brunofgc | 0:1c0a769988ee | 700 | union { |
brunofgc | 0:1c0a769988ee | 701 | char c[4]; |
brunofgc | 0:1c0a769988ee | 702 | float v; |
brunofgc | 0:1c0a769988ee | 703 | }u; |
brunofgc | 0:1c0a769988ee | 704 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 705 | uint16_t qtd_dados_recebidos; |
brunofgc | 0:1c0a769988ee | 706 | //void *p; |
brunofgc | 0:1c0a769988ee | 707 | uint16_t i; |
brunofgc | 18:1eefda1f7736 | 708 | uint8_t result; |
brunofgc | 18:1eefda1f7736 | 709 | |
brunofgc | 18:1eefda1f7736 | 710 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 18:1eefda1f7736 | 711 | var[i]=0.0; |
brunofgc | 18:1eefda1f7736 | 712 | } |
brunofgc | 32:7cf1fb8a8bf3 | 713 | |
brunofgc | 32:7cf1fb8a8bf3 | 714 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 715 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 716 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 717 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 718 | //------------Bloco de construcao de frame request---------- |
brunofgc | 18:1eefda1f7736 | 719 | |
brunofgc | 32:7cf1fb8a8bf3 | 720 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 721 | modBusMaster1::buffer[1]=funcao; |
brunofgc | 32:7cf1fb8a8bf3 | 722 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 723 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 724 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 725 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 726 | |
brunofgc | 32:7cf1fb8a8bf3 | 727 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 728 | |
brunofgc | 32:7cf1fb8a8bf3 | 729 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 730 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 731 | |
brunofgc | 32:7cf1fb8a8bf3 | 732 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 733 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 734 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 735 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 736 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 737 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 738 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 739 | } |
brunofgc | 32:7cf1fb8a8bf3 | 740 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 741 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 742 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 743 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 744 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 745 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 746 | } |
brunofgc | 32:7cf1fb8a8bf3 | 747 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 748 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 749 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 750 | } |
brunofgc | 0:1c0a769988ee | 751 | } |
brunofgc | 32:7cf1fb8a8bf3 | 752 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 753 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 754 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 755 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 756 | return 255; |
brunofgc | 0:1c0a769988ee | 757 | } |
brunofgc | 32:7cf1fb8a8bf3 | 758 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 759 | |
brunofgc | 0:1c0a769988ee | 760 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 761 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 762 | }else{ |
brunofgc | 0:1c0a769988ee | 763 | qtd_dados_recebidos = modBusMaster1::buffer[2]/4; |
brunofgc | 0:1c0a769988ee | 764 | for(i=0;i<qtd_dados_recebidos;i++){ |
brunofgc | 0:1c0a769988ee | 765 | u.c[3]=modBusMaster1::buffer[(i*4)+3]; |
brunofgc | 0:1c0a769988ee | 766 | u.c[2]=modBusMaster1::buffer[(i*4)+4]; |
brunofgc | 0:1c0a769988ee | 767 | u.c[1]=modBusMaster1::buffer[(i*4)+5]; |
brunofgc | 0:1c0a769988ee | 768 | u.c[0]=modBusMaster1::buffer[(i*4)+6]; |
brunofgc | 0:1c0a769988ee | 769 | var[i]=u.v; |
brunofgc | 0:1c0a769988ee | 770 | } |
brunofgc | 0:1c0a769988ee | 771 | result = 0; |
brunofgc | 0:1c0a769988ee | 772 | } |
brunofgc | 0:1c0a769988ee | 773 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 774 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 775 | return result; |
brunofgc | 0:1c0a769988ee | 776 | } |