Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
modbusMaster1.cpp@32:7cf1fb8a8bf3, 2018-05-26 (annotated)
- Committer:
- brunofgc
- Date:
- Sat May 26 14:17:55 2018 +0000
- Revision:
- 32:7cf1fb8a8bf3
- Parent:
- 26:c246eacf6815
- Child:
- 37:0e95c85f0160
Esta revis?o ja conta com Dispositivo ?nico, maquina de modbus concertada com retentativas, erro de reset concertado, alarmes-schedules-exceptions-reads em area de memoria extendida.; ; Faltando reposicionar config.bin e devices.cfg para FLASH ROM
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 | 0:1c0a769988ee | 122 | uint16_t i; |
brunofgc | 26:c246eacf6815 | 123 | |
brunofgc | 32:7cf1fb8a8bf3 | 124 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 125 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 126 | pc.printf("Frame enviado (hexa) <"); |
brunofgc | 25:a6da63ed025b | 127 | for(i=0;i<tamBytes;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 128 | pc.printf("%02x ",modBusMaster1::buffer[i]); |
brunofgc | 25:a6da63ed025b | 129 | } |
brunofgc | 32:7cf1fb8a8bf3 | 130 | pc.printf(">.\n"); |
brunofgc | 25:a6da63ed025b | 131 | } |
brunofgc | 32:7cf1fb8a8bf3 | 132 | |
brunofgc | 32:7cf1fb8a8bf3 | 133 | //osDelay(5); |
brunofgc | 32:7cf1fb8a8bf3 | 134 | |
brunofgc | 32:7cf1fb8a8bf3 | 135 | modBusMaster1::pacoteEmEspera = false; |
brunofgc | 32:7cf1fb8a8bf3 | 136 | modBusMaster1::index=0; |
brunofgc | 32:7cf1fb8a8bf3 | 137 | *modBusMaster1::de=1; |
brunofgc | 32:7cf1fb8a8bf3 | 138 | //wait_us(1750); |
brunofgc | 32:7cf1fb8a8bf3 | 139 | osDelay(1); |
brunofgc | 32:7cf1fb8a8bf3 | 140 | for(i=0;i<tamBytes;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 141 | modBusMaster1::serModBus->putc(modBusMaster1::buffer[i]); |
brunofgc | 32:7cf1fb8a8bf3 | 142 | //wait_us(750); |
brunofgc | 32:7cf1fb8a8bf3 | 143 | while( |
brunofgc | 32:7cf1fb8a8bf3 | 144 | ((LPC_UART1->LSR >> 6) &0x1) |
brunofgc | 32:7cf1fb8a8bf3 | 145 | == 0 ); |
brunofgc | 32:7cf1fb8a8bf3 | 146 | //wait_us(modBusMaster1::T1_5); |
brunofgc | 32:7cf1fb8a8bf3 | 147 | } |
brunofgc | 32:7cf1fb8a8bf3 | 148 | //wait_us(1750); |
brunofgc | 32:7cf1fb8a8bf3 | 149 | //osDelay(1); |
brunofgc | 32:7cf1fb8a8bf3 | 150 | *modBusMaster1::de=0; |
brunofgc | 32:7cf1fb8a8bf3 | 151 | |
brunofgc | 32:7cf1fb8a8bf3 | 152 | while((timeout>0)&&(modBusMaster1::pacoteEmEspera!=true)){ |
brunofgc | 32:7cf1fb8a8bf3 | 153 | osDelay(1); |
brunofgc | 32:7cf1fb8a8bf3 | 154 | //wait_us(1000); |
brunofgc | 32:7cf1fb8a8bf3 | 155 | timeout--; |
brunofgc | 32:7cf1fb8a8bf3 | 156 | } |
brunofgc | 32:7cf1fb8a8bf3 | 157 | wait_us(modBusMaster1::T3_5*2); |
brunofgc | 0:1c0a769988ee | 158 | return timeout; |
brunofgc | 0:1c0a769988ee | 159 | } |
brunofgc | 0:1c0a769988ee | 160 | |
brunofgc | 0:1c0a769988ee | 161 | uint8_t modBusMaster1::readRegister16BIT(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,uint16_t *var){ |
brunofgc | 0:1c0a769988ee | 162 | union{ |
brunofgc | 0:1c0a769988ee | 163 | char c[2]; |
brunofgc | 0:1c0a769988ee | 164 | uint16_t v; |
brunofgc | 0:1c0a769988ee | 165 | }u; |
brunofgc | 0:1c0a769988ee | 166 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 167 | uint16_t qtd_dados_recebidos; |
brunofgc | 0:1c0a769988ee | 168 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 169 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 170 | |
brunofgc | 18:1eefda1f7736 | 171 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 18:1eefda1f7736 | 172 | var[i]=NULL; |
brunofgc | 18:1eefda1f7736 | 173 | } |
brunofgc | 32:7cf1fb8a8bf3 | 174 | |
brunofgc | 32:7cf1fb8a8bf3 | 175 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 176 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 177 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 178 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 179 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 180 | modBusMaster1::buffer[1]=funcao; |
brunofgc | 32:7cf1fb8a8bf3 | 181 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 182 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 183 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 184 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 185 | |
brunofgc | 32:7cf1fb8a8bf3 | 186 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 187 | |
brunofgc | 32:7cf1fb8a8bf3 | 188 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 189 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 190 | |
brunofgc | 32:7cf1fb8a8bf3 | 191 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 192 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 193 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 194 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 195 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 196 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 197 | } |
brunofgc | 32:7cf1fb8a8bf3 | 198 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 199 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 200 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 201 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 202 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 203 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 204 | } |
brunofgc | 32:7cf1fb8a8bf3 | 205 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 206 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 207 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 208 | } |
brunofgc | 0:1c0a769988ee | 209 | } |
brunofgc | 32:7cf1fb8a8bf3 | 210 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 211 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 212 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 213 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 214 | return 255; |
brunofgc | 0:1c0a769988ee | 215 | } |
brunofgc | 32:7cf1fb8a8bf3 | 216 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 217 | |
brunofgc | 0:1c0a769988ee | 218 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 219 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 220 | }else{ |
brunofgc | 0:1c0a769988ee | 221 | qtd_dados_recebidos = modBusMaster1::buffer[2]/2; |
brunofgc | 0:1c0a769988ee | 222 | for(i=0;i<qtd_dados_recebidos;i++){ |
brunofgc | 0:1c0a769988ee | 223 | u.c[1]=modBusMaster1::buffer[(i*2)+3]; |
brunofgc | 0:1c0a769988ee | 224 | u.c[0]=modBusMaster1::buffer[(i*2)+4]; |
brunofgc | 0:1c0a769988ee | 225 | var[i]=u.v; |
brunofgc | 0:1c0a769988ee | 226 | } |
brunofgc | 0:1c0a769988ee | 227 | result = 0; |
brunofgc | 0:1c0a769988ee | 228 | } |
brunofgc | 0:1c0a769988ee | 229 | |
brunofgc | 0:1c0a769988ee | 230 | |
brunofgc | 0:1c0a769988ee | 231 | |
brunofgc | 0:1c0a769988ee | 232 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 233 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 234 | return result; |
brunofgc | 0:1c0a769988ee | 235 | } |
brunofgc | 0:1c0a769988ee | 236 | |
brunofgc | 0:1c0a769988ee | 237 | uint8_t modBusMaster1::writeSingleCoil(uint8_t enderecoSlave,uint16_t registrador,bool var){ |
brunofgc | 0:1c0a769988ee | 238 | uint16_t crc; |
brunofgc | 18:1eefda1f7736 | 239 | uint16_t estadoSetado; |
brunofgc | 32:7cf1fb8a8bf3 | 240 | uint8_t result = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 241 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 242 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 243 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 244 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 245 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 246 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 247 | modBusMaster1::buffer[1]=5; |
brunofgc | 32:7cf1fb8a8bf3 | 248 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 249 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 250 | |
brunofgc | 32:7cf1fb8a8bf3 | 251 | if(var){ |
brunofgc | 32:7cf1fb8a8bf3 | 252 | modBusMaster1::buffer[4]=0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 253 | modBusMaster1::buffer[5]=0x00; |
brunofgc | 32:7cf1fb8a8bf3 | 254 | estadoSetado = 0xFF00; |
brunofgc | 32:7cf1fb8a8bf3 | 255 | |
brunofgc | 32:7cf1fb8a8bf3 | 256 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 257 | modBusMaster1::buffer[4]=0x00; |
brunofgc | 32:7cf1fb8a8bf3 | 258 | modBusMaster1::buffer[5]=0x00; |
brunofgc | 32:7cf1fb8a8bf3 | 259 | estadoSetado = 0x0000; |
brunofgc | 32:7cf1fb8a8bf3 | 260 | } |
brunofgc | 32:7cf1fb8a8bf3 | 261 | |
brunofgc | 32:7cf1fb8a8bf3 | 262 | |
brunofgc | 32:7cf1fb8a8bf3 | 263 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 264 | |
brunofgc | 32:7cf1fb8a8bf3 | 265 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 266 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 267 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 268 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 269 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 270 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 271 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 272 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 273 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 274 | } |
brunofgc | 32:7cf1fb8a8bf3 | 275 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 276 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 277 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 278 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 279 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 280 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 281 | } |
brunofgc | 32:7cf1fb8a8bf3 | 282 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 283 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 284 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 285 | } |
brunofgc | 32:7cf1fb8a8bf3 | 286 | } |
brunofgc | 32:7cf1fb8a8bf3 | 287 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 32:7cf1fb8a8bf3 | 288 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 289 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 290 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 291 | return 255; |
brunofgc | 32:7cf1fb8a8bf3 | 292 | } |
brunofgc | 32:7cf1fb8a8bf3 | 293 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 294 | |
brunofgc | 32:7cf1fb8a8bf3 | 295 | |
brunofgc | 32:7cf1fb8a8bf3 | 296 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 32:7cf1fb8a8bf3 | 297 | result = modBusMaster1::buffer[2]; |
brunofgc | 32:7cf1fb8a8bf3 | 298 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 299 | if( |
brunofgc | 32:7cf1fb8a8bf3 | 300 | (((modBusMaster1::buffer[2]<<8)+(modBusMaster1::buffer[3]))==registrador) |
brunofgc | 32:7cf1fb8a8bf3 | 301 | && |
brunofgc | 32:7cf1fb8a8bf3 | 302 | (((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))==estadoSetado) |
brunofgc | 32:7cf1fb8a8bf3 | 303 | ){ |
brunofgc | 32:7cf1fb8a8bf3 | 304 | result=0; |
brunofgc | 32:7cf1fb8a8bf3 | 305 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 306 | result=253; |
brunofgc | 32:7cf1fb8a8bf3 | 307 | } |
brunofgc | 32:7cf1fb8a8bf3 | 308 | } |
brunofgc | 0:1c0a769988ee | 309 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 310 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 311 | return result; |
brunofgc | 0:1c0a769988ee | 312 | } |
brunofgc | 0:1c0a769988ee | 313 | |
brunofgc | 0:1c0a769988ee | 314 | uint8_t modBusMaster1::readCoils(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,bool *var){ |
brunofgc | 0:1c0a769988ee | 315 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 316 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 317 | uint16_t i; |
brunofgc | 32:7cf1fb8a8bf3 | 318 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 319 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 320 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 321 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 322 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 323 | |
brunofgc | 32:7cf1fb8a8bf3 | 324 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 325 | modBusMaster1::buffer[1]=1; |
brunofgc | 32:7cf1fb8a8bf3 | 326 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 327 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 328 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 329 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 330 | |
brunofgc | 32:7cf1fb8a8bf3 | 331 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 332 | |
brunofgc | 32:7cf1fb8a8bf3 | 333 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 334 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 335 | |
brunofgc | 32:7cf1fb8a8bf3 | 336 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 337 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 338 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 339 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 340 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 341 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 342 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 343 | } |
brunofgc | 32:7cf1fb8a8bf3 | 344 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 345 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 346 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 347 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 348 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 349 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 350 | } |
brunofgc | 32:7cf1fb8a8bf3 | 351 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 352 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 353 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 354 | } |
brunofgc | 0:1c0a769988ee | 355 | } |
brunofgc | 32:7cf1fb8a8bf3 | 356 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 357 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 358 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 359 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 360 | return 255; |
brunofgc | 0:1c0a769988ee | 361 | } |
brunofgc | 32:7cf1fb8a8bf3 | 362 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 363 | |
brunofgc | 0:1c0a769988ee | 364 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 365 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 366 | }else{ |
brunofgc | 0:1c0a769988ee | 367 | if( |
brunofgc | 0:1c0a769988ee | 368 | modBusMaster1::buffer[2] == (1+((uint16_t)(qtdRegistros/8))) |
brunofgc | 0:1c0a769988ee | 369 | ){ |
brunofgc | 0:1c0a769988ee | 370 | //Bloco de leitura das coils |
brunofgc | 0:1c0a769988ee | 371 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 0:1c0a769988ee | 372 | var[i] = ( |
brunofgc | 0:1c0a769988ee | 373 | (modBusMaster1::buffer[(i/8)+3] & ( 0x1 << (i%8) )) >0 |
brunofgc | 0:1c0a769988ee | 374 | ); |
brunofgc | 0:1c0a769988ee | 375 | } |
brunofgc | 0:1c0a769988ee | 376 | result = 0; |
brunofgc | 0:1c0a769988ee | 377 | }else{ |
brunofgc | 0:1c0a769988ee | 378 | result = 253; |
brunofgc | 0:1c0a769988ee | 379 | } |
brunofgc | 0:1c0a769988ee | 380 | } |
brunofgc | 0:1c0a769988ee | 381 | |
brunofgc | 0:1c0a769988ee | 382 | |
brunofgc | 0:1c0a769988ee | 383 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 384 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 385 | return result; |
brunofgc | 0:1c0a769988ee | 386 | } |
brunofgc | 0:1c0a769988ee | 387 | uint8_t modBusMaster1::readRegister32BIT(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,uint32_t *var){ |
brunofgc | 0:1c0a769988ee | 388 | union{ |
brunofgc | 0:1c0a769988ee | 389 | char c[4]; |
brunofgc | 0:1c0a769988ee | 390 | uint32_t v; |
brunofgc | 0:1c0a769988ee | 391 | }u; |
brunofgc | 0:1c0a769988ee | 392 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 393 | uint16_t qtd_dados_recebidos; |
brunofgc | 0:1c0a769988ee | 394 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 395 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 396 | |
brunofgc | 18:1eefda1f7736 | 397 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 18:1eefda1f7736 | 398 | var[i]=NULL; |
brunofgc | 18:1eefda1f7736 | 399 | } |
brunofgc | 32:7cf1fb8a8bf3 | 400 | |
brunofgc | 32:7cf1fb8a8bf3 | 401 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 402 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 403 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 404 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 405 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 406 | |
brunofgc | 32:7cf1fb8a8bf3 | 407 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 408 | modBusMaster1::buffer[1]=funcao; |
brunofgc | 32:7cf1fb8a8bf3 | 409 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 410 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 411 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 412 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 413 | |
brunofgc | 32:7cf1fb8a8bf3 | 414 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 415 | |
brunofgc | 32:7cf1fb8a8bf3 | 416 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 417 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 418 | |
brunofgc | 32:7cf1fb8a8bf3 | 419 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 420 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 421 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 422 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 423 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 424 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 425 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 426 | } |
brunofgc | 32:7cf1fb8a8bf3 | 427 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 428 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 429 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 430 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 431 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 432 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 433 | } |
brunofgc | 32:7cf1fb8a8bf3 | 434 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 435 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 436 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 437 | } |
brunofgc | 0:1c0a769988ee | 438 | } |
brunofgc | 32:7cf1fb8a8bf3 | 439 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 440 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 441 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 442 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 443 | return 255; |
brunofgc | 0:1c0a769988ee | 444 | } |
brunofgc | 32:7cf1fb8a8bf3 | 445 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 446 | |
brunofgc | 0:1c0a769988ee | 447 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 448 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 449 | }else{ |
brunofgc | 0:1c0a769988ee | 450 | |
brunofgc | 0:1c0a769988ee | 451 | qtd_dados_recebidos = modBusMaster1::buffer[2]/4; |
brunofgc | 0:1c0a769988ee | 452 | for(i=0;i<qtd_dados_recebidos;i++){ |
brunofgc | 0:1c0a769988ee | 453 | u.c[3]=modBusMaster1::buffer[(i*4)+3]; |
brunofgc | 0:1c0a769988ee | 454 | u.c[2]=modBusMaster1::buffer[(i*4)+4]; |
brunofgc | 0:1c0a769988ee | 455 | u.c[1]=modBusMaster1::buffer[(i*4)+5]; |
brunofgc | 0:1c0a769988ee | 456 | u.c[0]=modBusMaster1::buffer[(i*4)+6]; |
brunofgc | 0:1c0a769988ee | 457 | var[i]=u.v; |
brunofgc | 0:1c0a769988ee | 458 | } |
brunofgc | 0:1c0a769988ee | 459 | result =0; |
brunofgc | 0:1c0a769988ee | 460 | } |
brunofgc | 0:1c0a769988ee | 461 | |
brunofgc | 0:1c0a769988ee | 462 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 463 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 464 | return result; |
brunofgc | 0:1c0a769988ee | 465 | } |
brunofgc | 0:1c0a769988ee | 466 | |
brunofgc | 0:1c0a769988ee | 467 | uint8_t modBusMaster1::writeRegister16BIT(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,uint16_t *var){ |
brunofgc | 0:1c0a769988ee | 468 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 469 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 470 | uint8_t result; |
brunofgc | 32:7cf1fb8a8bf3 | 471 | |
brunofgc | 32:7cf1fb8a8bf3 | 472 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 473 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 474 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 475 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 476 | //------------Bloco de construcao de frame request---------- |
brunofgc | 0:1c0a769988ee | 477 | |
brunofgc | 32:7cf1fb8a8bf3 | 478 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 479 | modBusMaster1::buffer[1]=16; |
brunofgc | 32:7cf1fb8a8bf3 | 480 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 481 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 482 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 483 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 484 | modBusMaster1::buffer[6]=qtdRegistros*2; |
brunofgc | 32:7cf1fb8a8bf3 | 485 | |
brunofgc | 32:7cf1fb8a8bf3 | 486 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 487 | modBusMaster1::buffer[(i*2)+7]=var[i]>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 488 | modBusMaster1::buffer[(i*2)+8]=var[i]&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 489 | } |
brunofgc | 32:7cf1fb8a8bf3 | 490 | |
brunofgc | 32:7cf1fb8a8bf3 | 491 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*2)+7); |
brunofgc | 32:7cf1fb8a8bf3 | 492 | |
brunofgc | 32:7cf1fb8a8bf3 | 493 | modBusMaster1::buffer[(qtdRegistros*2)+7]=crc>>8;; |
brunofgc | 32:7cf1fb8a8bf3 | 494 | modBusMaster1::buffer[(qtdRegistros*2)+8]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 495 | |
brunofgc | 32:7cf1fb8a8bf3 | 496 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 497 | if(!modBusMaster1::sendFrame((qtdRegistros*2)+9)){ |
brunofgc | 32:7cf1fb8a8bf3 | 498 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 499 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 500 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 501 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 502 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 503 | } |
brunofgc | 32:7cf1fb8a8bf3 | 504 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 505 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 506 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 507 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 508 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 509 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 510 | } |
brunofgc | 32:7cf1fb8a8bf3 | 511 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 512 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 513 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 514 | } |
brunofgc | 0:1c0a769988ee | 515 | } |
brunofgc | 32:7cf1fb8a8bf3 | 516 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 517 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 518 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 519 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 520 | return 255; |
brunofgc | 0:1c0a769988ee | 521 | } |
brunofgc | 32:7cf1fb8a8bf3 | 522 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 523 | |
brunofgc | 0:1c0a769988ee | 524 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 525 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 526 | |
brunofgc | 0:1c0a769988ee | 527 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 528 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 529 | }else{ |
brunofgc | 0:1c0a769988ee | 530 | |
brunofgc | 0:1c0a769988ee | 531 | //Interpratando resposta |
brunofgc | 0:1c0a769988ee | 532 | if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){ |
brunofgc | 0:1c0a769988ee | 533 | return 253; |
brunofgc | 0:1c0a769988ee | 534 | }else{ |
brunofgc | 0:1c0a769988ee | 535 | result = 0; |
brunofgc | 0:1c0a769988ee | 536 | } |
brunofgc | 0:1c0a769988ee | 537 | } |
brunofgc | 0:1c0a769988ee | 538 | return result; |
brunofgc | 0:1c0a769988ee | 539 | } |
brunofgc | 0:1c0a769988ee | 540 | |
brunofgc | 0:1c0a769988ee | 541 | uint8_t modBusMaster1::writeRegister32BIT(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,uint32_t *var){ |
brunofgc | 0:1c0a769988ee | 542 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 543 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 544 | uint8_t result; |
brunofgc | 0:1c0a769988ee | 545 | |
brunofgc | 32:7cf1fb8a8bf3 | 546 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 547 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 548 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 549 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 550 | //------------Bloco de construcao de frame request---------- |
brunofgc | 0:1c0a769988ee | 551 | |
brunofgc | 32:7cf1fb8a8bf3 | 552 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 553 | modBusMaster1::buffer[1]=16; |
brunofgc | 32:7cf1fb8a8bf3 | 554 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 555 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 556 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 557 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 558 | modBusMaster1::buffer[6]=qtdRegistros*4; |
brunofgc | 32:7cf1fb8a8bf3 | 559 | |
brunofgc | 32:7cf1fb8a8bf3 | 560 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 561 | modBusMaster1::buffer[(i*4)+7]=(var[i]>>24)&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 562 | modBusMaster1::buffer[(i*4)+8]=(var[i]>>16)&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 563 | modBusMaster1::buffer[(i*4)+9]=(var[i]>>8)&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 564 | modBusMaster1::buffer[(i*4)+10]=var[i]&0xFF; |
brunofgc | 32:7cf1fb8a8bf3 | 565 | } |
brunofgc | 32:7cf1fb8a8bf3 | 566 | |
brunofgc | 32:7cf1fb8a8bf3 | 567 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*4)+7); |
brunofgc | 32:7cf1fb8a8bf3 | 568 | |
brunofgc | 32:7cf1fb8a8bf3 | 569 | modBusMaster1::buffer[(qtdRegistros*4)+7]=crc>>8;; |
brunofgc | 32:7cf1fb8a8bf3 | 570 | modBusMaster1::buffer[(qtdRegistros*4)+8]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 571 | |
brunofgc | 32:7cf1fb8a8bf3 | 572 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 573 | if(!modBusMaster1::sendFrame((qtdRegistros*4)+9)){ |
brunofgc | 32:7cf1fb8a8bf3 | 574 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 575 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 576 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 577 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 578 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 579 | } |
brunofgc | 32:7cf1fb8a8bf3 | 580 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 581 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 582 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 583 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 584 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 585 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 586 | } |
brunofgc | 32:7cf1fb8a8bf3 | 587 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 588 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 589 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 590 | } |
brunofgc | 0:1c0a769988ee | 591 | } |
brunofgc | 32:7cf1fb8a8bf3 | 592 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 593 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 594 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 595 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 596 | return 255; |
brunofgc | 0:1c0a769988ee | 597 | } |
brunofgc | 32:7cf1fb8a8bf3 | 598 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 599 | |
brunofgc | 0:1c0a769988ee | 600 | |
brunofgc | 0:1c0a769988ee | 601 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 602 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 603 | |
brunofgc | 0:1c0a769988ee | 604 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 605 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 606 | }else{ |
brunofgc | 0:1c0a769988ee | 607 | |
brunofgc | 0:1c0a769988ee | 608 | //Interpratando resposta |
brunofgc | 0:1c0a769988ee | 609 | if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){ |
brunofgc | 0:1c0a769988ee | 610 | return 253; |
brunofgc | 0:1c0a769988ee | 611 | }else{ |
brunofgc | 0:1c0a769988ee | 612 | result = 0; |
brunofgc | 0:1c0a769988ee | 613 | } |
brunofgc | 0:1c0a769988ee | 614 | } |
brunofgc | 0:1c0a769988ee | 615 | return result; |
brunofgc | 0:1c0a769988ee | 616 | } |
brunofgc | 0:1c0a769988ee | 617 | |
brunofgc | 0:1c0a769988ee | 618 | uint8_t modBusMaster1::writeFloat(uint8_t enderecoSlave,uint16_t registrador,uint8_t qtdRegistros,float *var){ |
brunofgc | 0:1c0a769988ee | 619 | union{ |
brunofgc | 0:1c0a769988ee | 620 | char c[4]; |
brunofgc | 0:1c0a769988ee | 621 | float v; |
brunofgc | 0:1c0a769988ee | 622 | }u; |
brunofgc | 0:1c0a769988ee | 623 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 624 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 625 | uint8_t result; |
brunofgc | 32:7cf1fb8a8bf3 | 626 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 627 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 628 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 629 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 630 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 631 | |
brunofgc | 32:7cf1fb8a8bf3 | 632 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 633 | modBusMaster1::buffer[1]=16; |
brunofgc | 32:7cf1fb8a8bf3 | 634 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 635 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 636 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 637 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 638 | modBusMaster1::buffer[6]=qtdRegistros*4; |
brunofgc | 32:7cf1fb8a8bf3 | 639 | |
brunofgc | 32:7cf1fb8a8bf3 | 640 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 32:7cf1fb8a8bf3 | 641 | u.v = var[i]; |
brunofgc | 32:7cf1fb8a8bf3 | 642 | modBusMaster1::buffer[(i*4)+7]=u.c[3]; |
brunofgc | 32:7cf1fb8a8bf3 | 643 | modBusMaster1::buffer[(i*4)+8]=u.c[2]; |
brunofgc | 32:7cf1fb8a8bf3 | 644 | modBusMaster1::buffer[(i*4)+9]=u.c[1]; |
brunofgc | 32:7cf1fb8a8bf3 | 645 | modBusMaster1::buffer[(i*4)+10]=u.c[0]; |
brunofgc | 32:7cf1fb8a8bf3 | 646 | } |
brunofgc | 32:7cf1fb8a8bf3 | 647 | |
brunofgc | 32:7cf1fb8a8bf3 | 648 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*4)+7); |
brunofgc | 32:7cf1fb8a8bf3 | 649 | |
brunofgc | 32:7cf1fb8a8bf3 | 650 | modBusMaster1::buffer[(qtdRegistros*4)+7]=crc>>8;; |
brunofgc | 32:7cf1fb8a8bf3 | 651 | modBusMaster1::buffer[(qtdRegistros*4)+8]=(crc & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 652 | |
brunofgc | 32:7cf1fb8a8bf3 | 653 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 654 | if(!modBusMaster1::sendFrame((qtdRegistros*4)+9)){ |
brunofgc | 32:7cf1fb8a8bf3 | 655 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 656 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 657 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 658 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 659 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 660 | } |
brunofgc | 32:7cf1fb8a8bf3 | 661 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 662 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 663 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 664 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 665 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 666 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 667 | } |
brunofgc | 32:7cf1fb8a8bf3 | 668 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 669 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 670 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 671 | } |
brunofgc | 0:1c0a769988ee | 672 | } |
brunofgc | 32:7cf1fb8a8bf3 | 673 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 674 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 675 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 676 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 677 | return 255; |
brunofgc | 0:1c0a769988ee | 678 | } |
brunofgc | 32:7cf1fb8a8bf3 | 679 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 680 | |
brunofgc | 0:1c0a769988ee | 681 | |
brunofgc | 0:1c0a769988ee | 682 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 683 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 684 | |
brunofgc | 0:1c0a769988ee | 685 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 686 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 687 | }else{ |
brunofgc | 0:1c0a769988ee | 688 | //Interpratando resposta |
brunofgc | 0:1c0a769988ee | 689 | if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){ |
brunofgc | 0:1c0a769988ee | 690 | return 253; |
brunofgc | 0:1c0a769988ee | 691 | }else{ |
brunofgc | 0:1c0a769988ee | 692 | result = 0; |
brunofgc | 0:1c0a769988ee | 693 | } |
brunofgc | 0:1c0a769988ee | 694 | } |
brunofgc | 0:1c0a769988ee | 695 | return result; |
brunofgc | 0:1c0a769988ee | 696 | } |
brunofgc | 0:1c0a769988ee | 697 | |
brunofgc | 0:1c0a769988ee | 698 | uint8_t modBusMaster1::readFloat(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,float *var){ |
brunofgc | 0:1c0a769988ee | 699 | union { |
brunofgc | 0:1c0a769988ee | 700 | char c[4]; |
brunofgc | 0:1c0a769988ee | 701 | float v; |
brunofgc | 0:1c0a769988ee | 702 | }u; |
brunofgc | 0:1c0a769988ee | 703 | uint16_t crc; |
brunofgc | 0:1c0a769988ee | 704 | uint16_t qtd_dados_recebidos; |
brunofgc | 0:1c0a769988ee | 705 | //void *p; |
brunofgc | 0:1c0a769988ee | 706 | uint16_t i; |
brunofgc | 18:1eefda1f7736 | 707 | uint8_t result; |
brunofgc | 18:1eefda1f7736 | 708 | |
brunofgc | 18:1eefda1f7736 | 709 | for(i=0;i<qtdRegistros;i++){ |
brunofgc | 18:1eefda1f7736 | 710 | var[i]=0.0; |
brunofgc | 18:1eefda1f7736 | 711 | } |
brunofgc | 32:7cf1fb8a8bf3 | 712 | |
brunofgc | 32:7cf1fb8a8bf3 | 713 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 32:7cf1fb8a8bf3 | 714 | uint16_t retentativas = maxRetentativas+1; |
brunofgc | 32:7cf1fb8a8bf3 | 715 | uint16_t delayEntreTentativas = maxDelayEntreTentativas; |
brunofgc | 32:7cf1fb8a8bf3 | 716 | while(retentativas){ |
brunofgc | 32:7cf1fb8a8bf3 | 717 | //------------Bloco de construcao de frame request---------- |
brunofgc | 18:1eefda1f7736 | 718 | |
brunofgc | 32:7cf1fb8a8bf3 | 719 | modBusMaster1::buffer[0]=enderecoSlave; |
brunofgc | 32:7cf1fb8a8bf3 | 720 | modBusMaster1::buffer[1]=funcao; |
brunofgc | 32:7cf1fb8a8bf3 | 721 | modBusMaster1::buffer[2]=registrador>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 722 | modBusMaster1::buffer[3]=(registrador & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 723 | modBusMaster1::buffer[4]=qtdRegistros>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 724 | modBusMaster1::buffer[5]=(qtdRegistros & 0xFF); |
brunofgc | 32:7cf1fb8a8bf3 | 725 | |
brunofgc | 32:7cf1fb8a8bf3 | 726 | crc=modBusMaster1::CRC16(modBusMaster1::buffer,6); |
brunofgc | 32:7cf1fb8a8bf3 | 727 | |
brunofgc | 32:7cf1fb8a8bf3 | 728 | modBusMaster1::buffer[6]=crc>>8; |
brunofgc | 32:7cf1fb8a8bf3 | 729 | modBusMaster1::buffer[7]=(crc & 0xFF); |
brunofgc | 0:1c0a769988ee | 730 | |
brunofgc | 32:7cf1fb8a8bf3 | 731 | //------------Bloco de construcao de frame request---------- |
brunofgc | 32:7cf1fb8a8bf3 | 732 | if(!modBusMaster1::sendFrame(8)){ |
brunofgc | 32:7cf1fb8a8bf3 | 733 | osDelay(delayEntreTentativas); |
brunofgc | 32:7cf1fb8a8bf3 | 734 | retentativas--; |
brunofgc | 32:7cf1fb8a8bf3 | 735 | if(debug){ |
brunofgc | 32:7cf1fb8a8bf3 | 736 | //DEBUG Mostrando o que é enviado via modbus |
brunofgc | 32:7cf1fb8a8bf3 | 737 | pc.printf("Retentativa de comando modbus.\r\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 738 | } |
brunofgc | 32:7cf1fb8a8bf3 | 739 | if(retentativas==0){ |
brunofgc | 32:7cf1fb8a8bf3 | 740 | //erro de timeout certamente |
brunofgc | 32:7cf1fb8a8bf3 | 741 | pc.printf("Erro de timeout.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 742 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 743 | modBusMaster1::index = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 744 | return 254; |
brunofgc | 32:7cf1fb8a8bf3 | 745 | } |
brunofgc | 32:7cf1fb8a8bf3 | 746 | }else{ |
brunofgc | 32:7cf1fb8a8bf3 | 747 | //Chegou pacote, vamos ver se é valido |
brunofgc | 32:7cf1fb8a8bf3 | 748 | retentativas = 0; |
brunofgc | 32:7cf1fb8a8bf3 | 749 | } |
brunofgc | 0:1c0a769988ee | 750 | } |
brunofgc | 32:7cf1fb8a8bf3 | 751 | if(!modBusMaster1::pacoteEmEsperaValido){ |
brunofgc | 0:1c0a769988ee | 752 | pc.printf("Erro de CRC.\n"); |
brunofgc | 32:7cf1fb8a8bf3 | 753 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 32:7cf1fb8a8bf3 | 754 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 755 | return 255; |
brunofgc | 0:1c0a769988ee | 756 | } |
brunofgc | 32:7cf1fb8a8bf3 | 757 | //-----------------Bloco de tentativas--------------------- |
brunofgc | 0:1c0a769988ee | 758 | |
brunofgc | 0:1c0a769988ee | 759 | if(modBusMaster1::buffer[1]&(0x1<<7)){ |
brunofgc | 0:1c0a769988ee | 760 | result = modBusMaster1::buffer[2]; |
brunofgc | 0:1c0a769988ee | 761 | }else{ |
brunofgc | 0:1c0a769988ee | 762 | qtd_dados_recebidos = modBusMaster1::buffer[2]/4; |
brunofgc | 0:1c0a769988ee | 763 | for(i=0;i<qtd_dados_recebidos;i++){ |
brunofgc | 0:1c0a769988ee | 764 | u.c[3]=modBusMaster1::buffer[(i*4)+3]; |
brunofgc | 0:1c0a769988ee | 765 | u.c[2]=modBusMaster1::buffer[(i*4)+4]; |
brunofgc | 0:1c0a769988ee | 766 | u.c[1]=modBusMaster1::buffer[(i*4)+5]; |
brunofgc | 0:1c0a769988ee | 767 | u.c[0]=modBusMaster1::buffer[(i*4)+6]; |
brunofgc | 0:1c0a769988ee | 768 | var[i]=u.v; |
brunofgc | 0:1c0a769988ee | 769 | } |
brunofgc | 0:1c0a769988ee | 770 | result = 0; |
brunofgc | 0:1c0a769988ee | 771 | } |
brunofgc | 0:1c0a769988ee | 772 | modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote |
brunofgc | 0:1c0a769988ee | 773 | modBusMaster1::index = 0; |
brunofgc | 0:1c0a769988ee | 774 | return result; |
brunofgc | 0:1c0a769988ee | 775 | } |