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