teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Fri Mar 24 15:54:41 2017 +0000
Revision:
0:1c0a769988ee
Child:
18:1eefda1f7736
Saidas digitais com pwm ok, entradas analogicas ok

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 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 0:1c0a769988ee 152 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 153 modBusMaster1::buffer[1]=funcao;
brunofgc 0:1c0a769988ee 154 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 155 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 156 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 157 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 158
brunofgc 0:1c0a769988ee 159 crc=modBusMaster1::CRC16(modBusMaster1::buffer,6);
brunofgc 0:1c0a769988ee 160
brunofgc 0:1c0a769988ee 161 modBusMaster1::buffer[6]=crc>>8;
brunofgc 0:1c0a769988ee 162 modBusMaster1::buffer[7]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 163
brunofgc 0:1c0a769988ee 164
brunofgc 0:1c0a769988ee 165
brunofgc 0:1c0a769988ee 166 if(!modBusMaster1::sendFrame(8)){
brunofgc 0:1c0a769988ee 167 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 168 return 254;
brunofgc 0:1c0a769988ee 169 }
brunofgc 0:1c0a769988ee 170
brunofgc 0:1c0a769988ee 171 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 172 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 173 return 255;
brunofgc 0:1c0a769988ee 174 }
brunofgc 0:1c0a769988ee 175
brunofgc 0:1c0a769988ee 176 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 177 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 178 }else{
brunofgc 0:1c0a769988ee 179 qtd_dados_recebidos = modBusMaster1::buffer[2]/2;
brunofgc 0:1c0a769988ee 180 for(i=0;i<qtd_dados_recebidos;i++){
brunofgc 0:1c0a769988ee 181 u.c[1]=modBusMaster1::buffer[(i*2)+3];
brunofgc 0:1c0a769988ee 182 u.c[0]=modBusMaster1::buffer[(i*2)+4];
brunofgc 0:1c0a769988ee 183 var[i]=u.v;
brunofgc 0:1c0a769988ee 184 }
brunofgc 0:1c0a769988ee 185 result = 0;
brunofgc 0:1c0a769988ee 186 }
brunofgc 0:1c0a769988ee 187
brunofgc 0:1c0a769988ee 188
brunofgc 0:1c0a769988ee 189
brunofgc 0:1c0a769988ee 190 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 191 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 192 return result;
brunofgc 0:1c0a769988ee 193 }
brunofgc 0:1c0a769988ee 194
brunofgc 0:1c0a769988ee 195 uint8_t modBusMaster1::writeSingleCoil(uint8_t enderecoSlave,uint16_t registrador,bool var){
brunofgc 0:1c0a769988ee 196 uint16_t crc;
brunofgc 0:1c0a769988ee 197 uint8_t result = 1; //Tudo ok ate que se prove o contrario.
brunofgc 0:1c0a769988ee 198 uint16_t estadoSetado;
brunofgc 0:1c0a769988ee 199
brunofgc 0:1c0a769988ee 200
brunofgc 0:1c0a769988ee 201 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 202 modBusMaster1::buffer[1]=5;
brunofgc 0:1c0a769988ee 203 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 204 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 205
brunofgc 0:1c0a769988ee 206 if(var){
brunofgc 0:1c0a769988ee 207 modBusMaster1::buffer[4]=0xFF;
brunofgc 0:1c0a769988ee 208 modBusMaster1::buffer[5]=0x00;
brunofgc 0:1c0a769988ee 209 estadoSetado = 0xFF00;
brunofgc 0:1c0a769988ee 210
brunofgc 0:1c0a769988ee 211 }else{
brunofgc 0:1c0a769988ee 212 modBusMaster1::buffer[4]=0x00;
brunofgc 0:1c0a769988ee 213 modBusMaster1::buffer[5]=0x00;
brunofgc 0:1c0a769988ee 214 estadoSetado = 0x0000;
brunofgc 0:1c0a769988ee 215 }
brunofgc 0:1c0a769988ee 216
brunofgc 0:1c0a769988ee 217
brunofgc 0:1c0a769988ee 218 crc=modBusMaster1::CRC16(modBusMaster1::buffer,6);
brunofgc 0:1c0a769988ee 219
brunofgc 0:1c0a769988ee 220 modBusMaster1::buffer[6]=crc>>8;
brunofgc 0:1c0a769988ee 221 modBusMaster1::buffer[7]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 222
brunofgc 0:1c0a769988ee 223
brunofgc 0:1c0a769988ee 224
brunofgc 0:1c0a769988ee 225 if(!modBusMaster1::sendFrame(8)){
brunofgc 0:1c0a769988ee 226 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 227 result = 254;
brunofgc 0:1c0a769988ee 228 return result;
brunofgc 0:1c0a769988ee 229 }
brunofgc 0:1c0a769988ee 230
brunofgc 0:1c0a769988ee 231 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 232 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 233 result = 255;
brunofgc 0:1c0a769988ee 234 return result;
brunofgc 0:1c0a769988ee 235 }
brunofgc 0:1c0a769988ee 236
brunofgc 0:1c0a769988ee 237 if(result){
brunofgc 0:1c0a769988ee 238 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 239 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 240 }else{
brunofgc 0:1c0a769988ee 241 if(
brunofgc 0:1c0a769988ee 242 (((modBusMaster1::buffer[2]<<8)+(modBusMaster1::buffer[3]))==registrador)
brunofgc 0:1c0a769988ee 243 &&
brunofgc 0:1c0a769988ee 244 (((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))==estadoSetado)
brunofgc 0:1c0a769988ee 245 ){
brunofgc 0:1c0a769988ee 246 result=0;
brunofgc 0:1c0a769988ee 247 }else{
brunofgc 0:1c0a769988ee 248 result=253;
brunofgc 0:1c0a769988ee 249 }
brunofgc 0:1c0a769988ee 250 }
brunofgc 0:1c0a769988ee 251 }
brunofgc 0:1c0a769988ee 252
brunofgc 0:1c0a769988ee 253 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 254 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 255 return result;
brunofgc 0:1c0a769988ee 256 }
brunofgc 0:1c0a769988ee 257
brunofgc 0:1c0a769988ee 258 uint8_t modBusMaster1::readCoils(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,bool *var){
brunofgc 0:1c0a769988ee 259 uint16_t crc;
brunofgc 0:1c0a769988ee 260 uint8_t result;
brunofgc 0:1c0a769988ee 261 uint16_t i;
brunofgc 0:1c0a769988ee 262
brunofgc 0:1c0a769988ee 263 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 264 modBusMaster1::buffer[1]=1;
brunofgc 0:1c0a769988ee 265 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 266 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 267 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 268 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 269
brunofgc 0:1c0a769988ee 270 crc=modBusMaster1::CRC16(modBusMaster1::buffer,6);
brunofgc 0:1c0a769988ee 271
brunofgc 0:1c0a769988ee 272 modBusMaster1::buffer[6]=crc>>8;
brunofgc 0:1c0a769988ee 273 modBusMaster1::buffer[7]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 274
brunofgc 0:1c0a769988ee 275
brunofgc 0:1c0a769988ee 276
brunofgc 0:1c0a769988ee 277 if(!modBusMaster1::sendFrame(8)){
brunofgc 0:1c0a769988ee 278 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 279 return 254;
brunofgc 0:1c0a769988ee 280 }
brunofgc 0:1c0a769988ee 281
brunofgc 0:1c0a769988ee 282 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 283 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 284 return 255;
brunofgc 0:1c0a769988ee 285 }
brunofgc 0:1c0a769988ee 286
brunofgc 0:1c0a769988ee 287 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 288 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 289 }else{
brunofgc 0:1c0a769988ee 290 if(
brunofgc 0:1c0a769988ee 291 modBusMaster1::buffer[2] == (1+((uint16_t)(qtdRegistros/8)))
brunofgc 0:1c0a769988ee 292 ){
brunofgc 0:1c0a769988ee 293 //Bloco de leitura das coils
brunofgc 0:1c0a769988ee 294 for(i=0;i<qtdRegistros;i++){
brunofgc 0:1c0a769988ee 295 var[i] = (
brunofgc 0:1c0a769988ee 296 (modBusMaster1::buffer[(i/8)+3] & ( 0x1 << (i%8) )) >0
brunofgc 0:1c0a769988ee 297 );
brunofgc 0:1c0a769988ee 298 }
brunofgc 0:1c0a769988ee 299 result = 0;
brunofgc 0:1c0a769988ee 300 }else{
brunofgc 0:1c0a769988ee 301 result = 253;
brunofgc 0:1c0a769988ee 302 }
brunofgc 0:1c0a769988ee 303 }
brunofgc 0:1c0a769988ee 304
brunofgc 0:1c0a769988ee 305
brunofgc 0:1c0a769988ee 306 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 307 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 308 return result;
brunofgc 0:1c0a769988ee 309 }
brunofgc 0:1c0a769988ee 310 uint8_t modBusMaster1::readRegister32BIT(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,uint32_t *var){
brunofgc 0:1c0a769988ee 311 union{
brunofgc 0:1c0a769988ee 312 char c[4];
brunofgc 0:1c0a769988ee 313 uint32_t v;
brunofgc 0:1c0a769988ee 314 }u;
brunofgc 0:1c0a769988ee 315 uint16_t crc;
brunofgc 0:1c0a769988ee 316 uint16_t qtd_dados_recebidos;
brunofgc 0:1c0a769988ee 317 uint16_t i;
brunofgc 0:1c0a769988ee 318 uint8_t result;
brunofgc 0:1c0a769988ee 319
brunofgc 0:1c0a769988ee 320 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 321 modBusMaster1::buffer[1]=funcao;
brunofgc 0:1c0a769988ee 322 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 323 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 324 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 325 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 326
brunofgc 0:1c0a769988ee 327 crc=modBusMaster1::CRC16(modBusMaster1::buffer,6);
brunofgc 0:1c0a769988ee 328
brunofgc 0:1c0a769988ee 329 modBusMaster1::buffer[6]=crc>>8;
brunofgc 0:1c0a769988ee 330 modBusMaster1::buffer[7]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 331
brunofgc 0:1c0a769988ee 332
brunofgc 0:1c0a769988ee 333
brunofgc 0:1c0a769988ee 334 if(!modBusMaster1::sendFrame(8)){
brunofgc 0:1c0a769988ee 335 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 336 return 254;
brunofgc 0:1c0a769988ee 337 }
brunofgc 0:1c0a769988ee 338
brunofgc 0:1c0a769988ee 339 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 340 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 341 return 255;
brunofgc 0:1c0a769988ee 342 }
brunofgc 0:1c0a769988ee 343
brunofgc 0:1c0a769988ee 344 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 345 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 346 }else{
brunofgc 0:1c0a769988ee 347
brunofgc 0:1c0a769988ee 348 qtd_dados_recebidos = modBusMaster1::buffer[2]/4;
brunofgc 0:1c0a769988ee 349 for(i=0;i<qtd_dados_recebidos;i++){
brunofgc 0:1c0a769988ee 350 u.c[3]=modBusMaster1::buffer[(i*4)+3];
brunofgc 0:1c0a769988ee 351 u.c[2]=modBusMaster1::buffer[(i*4)+4];
brunofgc 0:1c0a769988ee 352 u.c[1]=modBusMaster1::buffer[(i*4)+5];
brunofgc 0:1c0a769988ee 353 u.c[0]=modBusMaster1::buffer[(i*4)+6];
brunofgc 0:1c0a769988ee 354 var[i]=u.v;
brunofgc 0:1c0a769988ee 355 }
brunofgc 0:1c0a769988ee 356 result =0;
brunofgc 0:1c0a769988ee 357 }
brunofgc 0:1c0a769988ee 358
brunofgc 0:1c0a769988ee 359 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 360 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 361 return result;
brunofgc 0:1c0a769988ee 362 }
brunofgc 0:1c0a769988ee 363
brunofgc 0:1c0a769988ee 364 uint8_t modBusMaster1::writeRegister16BIT(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,uint16_t *var){
brunofgc 0:1c0a769988ee 365 uint16_t i;
brunofgc 0:1c0a769988ee 366 uint16_t crc;
brunofgc 0:1c0a769988ee 367 uint8_t result;
brunofgc 0:1c0a769988ee 368
brunofgc 0:1c0a769988ee 369 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 370 modBusMaster1::buffer[1]=16;
brunofgc 0:1c0a769988ee 371 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 372 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 373 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 374 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 375 modBusMaster1::buffer[6]=qtdRegistros*2;
brunofgc 0:1c0a769988ee 376
brunofgc 0:1c0a769988ee 377 for(i=0;i<qtdRegistros;i++){
brunofgc 0:1c0a769988ee 378 modBusMaster1::buffer[(i*2)+7]=var[i]>>8;
brunofgc 0:1c0a769988ee 379 modBusMaster1::buffer[(i*2)+8]=var[i]&0xFF;
brunofgc 0:1c0a769988ee 380 }
brunofgc 0:1c0a769988ee 381
brunofgc 0:1c0a769988ee 382 crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*2)+7);
brunofgc 0:1c0a769988ee 383
brunofgc 0:1c0a769988ee 384 modBusMaster1::buffer[(qtdRegistros*2)+7]=crc>>8;;
brunofgc 0:1c0a769988ee 385 modBusMaster1::buffer[(qtdRegistros*2)+8]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 386
brunofgc 0:1c0a769988ee 387
brunofgc 0:1c0a769988ee 388
brunofgc 0:1c0a769988ee 389 //Enviando frame
brunofgc 0:1c0a769988ee 390 if(!modBusMaster1::sendFrame((qtdRegistros*2)+9)){
brunofgc 0:1c0a769988ee 391 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 392 return 254;
brunofgc 0:1c0a769988ee 393 }
brunofgc 0:1c0a769988ee 394
brunofgc 0:1c0a769988ee 395 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 396 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 397 return 255;
brunofgc 0:1c0a769988ee 398 }
brunofgc 0:1c0a769988ee 399
brunofgc 0:1c0a769988ee 400 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 401 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 402
brunofgc 0:1c0a769988ee 403 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 404 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 405 }else{
brunofgc 0:1c0a769988ee 406
brunofgc 0:1c0a769988ee 407 //Interpratando resposta
brunofgc 0:1c0a769988ee 408 if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){
brunofgc 0:1c0a769988ee 409 return 253;
brunofgc 0:1c0a769988ee 410 }else{
brunofgc 0:1c0a769988ee 411 result = 0;
brunofgc 0:1c0a769988ee 412 }
brunofgc 0:1c0a769988ee 413 }
brunofgc 0:1c0a769988ee 414 return result;
brunofgc 0:1c0a769988ee 415 }
brunofgc 0:1c0a769988ee 416
brunofgc 0:1c0a769988ee 417 uint8_t modBusMaster1::writeRegister32BIT(uint8_t enderecoSlave,uint16_t registrador,uint16_t qtdRegistros,uint32_t *var){
brunofgc 0:1c0a769988ee 418 uint16_t i;
brunofgc 0:1c0a769988ee 419 uint16_t crc;
brunofgc 0:1c0a769988ee 420 uint8_t result;
brunofgc 0:1c0a769988ee 421
brunofgc 0:1c0a769988ee 422 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 423 modBusMaster1::buffer[1]=16;
brunofgc 0:1c0a769988ee 424 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 425 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 426 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 427 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 428 modBusMaster1::buffer[6]=qtdRegistros*4;
brunofgc 0:1c0a769988ee 429
brunofgc 0:1c0a769988ee 430 for(i=0;i<qtdRegistros;i++){
brunofgc 0:1c0a769988ee 431 modBusMaster1::buffer[(i*4)+7]=(var[i]>>24)&0xFF;
brunofgc 0:1c0a769988ee 432 modBusMaster1::buffer[(i*4)+8]=(var[i]>>16)&0xFF;
brunofgc 0:1c0a769988ee 433 modBusMaster1::buffer[(i*4)+9]=(var[i]>>8)&0xFF;
brunofgc 0:1c0a769988ee 434 modBusMaster1::buffer[(i*4)+10]=var[i]&0xFF;
brunofgc 0:1c0a769988ee 435 }
brunofgc 0:1c0a769988ee 436
brunofgc 0:1c0a769988ee 437 crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*4)+7);
brunofgc 0:1c0a769988ee 438
brunofgc 0:1c0a769988ee 439 modBusMaster1::buffer[(qtdRegistros*4)+7]=crc>>8;;
brunofgc 0:1c0a769988ee 440 modBusMaster1::buffer[(qtdRegistros*4)+8]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 441
brunofgc 0:1c0a769988ee 442
brunofgc 0:1c0a769988ee 443
brunofgc 0:1c0a769988ee 444 //Enviando frame
brunofgc 0:1c0a769988ee 445 if(!modBusMaster1::sendFrame((qtdRegistros*4)+9)){
brunofgc 0:1c0a769988ee 446 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 447 return 254;
brunofgc 0:1c0a769988ee 448 }
brunofgc 0:1c0a769988ee 449
brunofgc 0:1c0a769988ee 450 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 451 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 452 return 255;
brunofgc 0:1c0a769988ee 453 }
brunofgc 0:1c0a769988ee 454
brunofgc 0:1c0a769988ee 455 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 456 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 457
brunofgc 0:1c0a769988ee 458 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 459 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 460 }else{
brunofgc 0:1c0a769988ee 461
brunofgc 0:1c0a769988ee 462 //Interpratando resposta
brunofgc 0:1c0a769988ee 463 if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){
brunofgc 0:1c0a769988ee 464 return 253;
brunofgc 0:1c0a769988ee 465 }else{
brunofgc 0:1c0a769988ee 466 result = 0;
brunofgc 0:1c0a769988ee 467 }
brunofgc 0:1c0a769988ee 468 }
brunofgc 0:1c0a769988ee 469 return result;
brunofgc 0:1c0a769988ee 470 }
brunofgc 0:1c0a769988ee 471
brunofgc 0:1c0a769988ee 472 uint8_t modBusMaster1::writeFloat(uint8_t enderecoSlave,uint16_t registrador,uint8_t qtdRegistros,float *var){
brunofgc 0:1c0a769988ee 473 union{
brunofgc 0:1c0a769988ee 474 char c[4];
brunofgc 0:1c0a769988ee 475 float v;
brunofgc 0:1c0a769988ee 476 }u;
brunofgc 0:1c0a769988ee 477 uint16_t i;
brunofgc 0:1c0a769988ee 478 uint16_t crc;
brunofgc 0:1c0a769988ee 479 uint8_t result;
brunofgc 0:1c0a769988ee 480
brunofgc 0:1c0a769988ee 481 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 482 modBusMaster1::buffer[1]=16;
brunofgc 0:1c0a769988ee 483 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 484 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 485 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 486 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 487 modBusMaster1::buffer[6]=qtdRegistros*4;
brunofgc 0:1c0a769988ee 488
brunofgc 0:1c0a769988ee 489 for(i=0;i<qtdRegistros;i++){
brunofgc 0:1c0a769988ee 490 u.v = var[i];
brunofgc 0:1c0a769988ee 491 modBusMaster1::buffer[(i*4)+7]=u.c[3];
brunofgc 0:1c0a769988ee 492 modBusMaster1::buffer[(i*4)+8]=u.c[2];
brunofgc 0:1c0a769988ee 493 modBusMaster1::buffer[(i*4)+9]=u.c[1];
brunofgc 0:1c0a769988ee 494 modBusMaster1::buffer[(i*4)+10]=u.c[0];
brunofgc 0:1c0a769988ee 495 }
brunofgc 0:1c0a769988ee 496
brunofgc 0:1c0a769988ee 497 crc=modBusMaster1::CRC16(modBusMaster1::buffer,((qtdRegistros)*4)+7);
brunofgc 0:1c0a769988ee 498
brunofgc 0:1c0a769988ee 499 modBusMaster1::buffer[(qtdRegistros*4)+7]=crc>>8;;
brunofgc 0:1c0a769988ee 500 modBusMaster1::buffer[(qtdRegistros*4)+8]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 501
brunofgc 0:1c0a769988ee 502
brunofgc 0:1c0a769988ee 503
brunofgc 0:1c0a769988ee 504 //Enviando frame
brunofgc 0:1c0a769988ee 505 if(!modBusMaster1::sendFrame((qtdRegistros*4)+9)){
brunofgc 0:1c0a769988ee 506 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 507 return 254;
brunofgc 0:1c0a769988ee 508 }
brunofgc 0:1c0a769988ee 509
brunofgc 0:1c0a769988ee 510 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 511 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 512 return 255;
brunofgc 0:1c0a769988ee 513 }
brunofgc 0:1c0a769988ee 514
brunofgc 0:1c0a769988ee 515 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 516 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 517
brunofgc 0:1c0a769988ee 518 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 519 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 520 }else{
brunofgc 0:1c0a769988ee 521 //Interpratando resposta
brunofgc 0:1c0a769988ee 522 if(((modBusMaster1::buffer[4]<<8)+(modBusMaster1::buffer[5]))!=qtdRegistros){
brunofgc 0:1c0a769988ee 523 return 253;
brunofgc 0:1c0a769988ee 524 }else{
brunofgc 0:1c0a769988ee 525 result = 0;
brunofgc 0:1c0a769988ee 526 }
brunofgc 0:1c0a769988ee 527 }
brunofgc 0:1c0a769988ee 528 return result;
brunofgc 0:1c0a769988ee 529 }
brunofgc 0:1c0a769988ee 530
brunofgc 0:1c0a769988ee 531 uint8_t modBusMaster1::readFloat(uint8_t enderecoSlave,uint8_t funcao,uint16_t registrador,uint16_t qtdRegistros,float *var){
brunofgc 0:1c0a769988ee 532 union {
brunofgc 0:1c0a769988ee 533 char c[4];
brunofgc 0:1c0a769988ee 534 float v;
brunofgc 0:1c0a769988ee 535 }u;
brunofgc 0:1c0a769988ee 536 uint16_t crc;
brunofgc 0:1c0a769988ee 537 uint16_t qtd_dados_recebidos;
brunofgc 0:1c0a769988ee 538 //void *p;
brunofgc 0:1c0a769988ee 539 uint16_t i;
brunofgc 0:1c0a769988ee 540 uint8_t result;
brunofgc 0:1c0a769988ee 541 modBusMaster1::buffer[0]=enderecoSlave;
brunofgc 0:1c0a769988ee 542 modBusMaster1::buffer[1]=funcao;
brunofgc 0:1c0a769988ee 543 modBusMaster1::buffer[2]=registrador>>8;
brunofgc 0:1c0a769988ee 544 modBusMaster1::buffer[3]=(registrador & 0xFF);
brunofgc 0:1c0a769988ee 545 modBusMaster1::buffer[4]=qtdRegistros>>8;
brunofgc 0:1c0a769988ee 546 modBusMaster1::buffer[5]=(qtdRegistros & 0xFF);
brunofgc 0:1c0a769988ee 547
brunofgc 0:1c0a769988ee 548 crc=modBusMaster1::CRC16(modBusMaster1::buffer,6);
brunofgc 0:1c0a769988ee 549
brunofgc 0:1c0a769988ee 550 modBusMaster1::buffer[6]=crc>>8;
brunofgc 0:1c0a769988ee 551 modBusMaster1::buffer[7]=(crc & 0xFF);
brunofgc 0:1c0a769988ee 552
brunofgc 0:1c0a769988ee 553
brunofgc 0:1c0a769988ee 554
brunofgc 0:1c0a769988ee 555 if(!modBusMaster1::sendFrame(8)){
brunofgc 0:1c0a769988ee 556 pc.printf("Erro de timeout.\n");
brunofgc 0:1c0a769988ee 557 return 254;
brunofgc 0:1c0a769988ee 558 }
brunofgc 0:1c0a769988ee 559
brunofgc 0:1c0a769988ee 560 if(!modBusMaster1::pacoteEmEspera){
brunofgc 0:1c0a769988ee 561 pc.printf("Erro de CRC.\n");
brunofgc 0:1c0a769988ee 562 return 255;
brunofgc 0:1c0a769988ee 563 }
brunofgc 0:1c0a769988ee 564
brunofgc 0:1c0a769988ee 565
brunofgc 0:1c0a769988ee 566 /*
brunofgc 0:1c0a769988ee 567 for(i=3;i<modBusMaster1::index-2;i++){
brunofgc 0:1c0a769988ee 568 u.c[3-(i-3)]= modBusMaster1::buffer[i];
brunofgc 0:1c0a769988ee 569 }
brunofgc 0:1c0a769988ee 570
brunofgc 0:1c0a769988ee 571 *var = u.f;
brunofgc 0:1c0a769988ee 572 */
brunofgc 0:1c0a769988ee 573
brunofgc 0:1c0a769988ee 574
brunofgc 0:1c0a769988ee 575 /*pc.printf("Lido pacote de modBus <0x");
brunofgc 0:1c0a769988ee 576
brunofgc 0:1c0a769988ee 577 for(i=0;i<modBusMaster1::index;i++){
brunofgc 0:1c0a769988ee 578 pc.printf("%02x ",modBusMaster1::buffer[i]);
brunofgc 0:1c0a769988ee 579 }
brunofgc 0:1c0a769988ee 580 pc.printf(">.\n");*/
brunofgc 0:1c0a769988ee 581
brunofgc 0:1c0a769988ee 582 if(modBusMaster1::buffer[1]&(0x1<<7)){
brunofgc 0:1c0a769988ee 583 result = modBusMaster1::buffer[2];
brunofgc 0:1c0a769988ee 584 }else{
brunofgc 0:1c0a769988ee 585 qtd_dados_recebidos = modBusMaster1::buffer[2]/4;
brunofgc 0:1c0a769988ee 586 for(i=0;i<qtd_dados_recebidos;i++){
brunofgc 0:1c0a769988ee 587 u.c[3]=modBusMaster1::buffer[(i*4)+3];
brunofgc 0:1c0a769988ee 588 u.c[2]=modBusMaster1::buffer[(i*4)+4];
brunofgc 0:1c0a769988ee 589 u.c[1]=modBusMaster1::buffer[(i*4)+5];
brunofgc 0:1c0a769988ee 590 u.c[0]=modBusMaster1::buffer[(i*4)+6];
brunofgc 0:1c0a769988ee 591 var[i]=u.v;
brunofgc 0:1c0a769988ee 592 }
brunofgc 0:1c0a769988ee 593 result = 0;
brunofgc 0:1c0a769988ee 594 }
brunofgc 0:1c0a769988ee 595 modBusMaster1::pacoteEmEspera = false; //Sinalizo que li o pacote
brunofgc 0:1c0a769988ee 596 modBusMaster1::index = 0;
brunofgc 0:1c0a769988ee 597 return result;
brunofgc 0:1c0a769988ee 598 }