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