teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Mon Apr 16 12:30:29 2018 +0000
Revision:
25:a6da63ed025b
Parent:
18:1eefda1f7736
Child:
26:c246eacf6815
Vers?o 16/04/2018 com modbus otimizado para 100 leituras

Who changed what in which revision?

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