Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Basic_Master - example using ModbusMaster library
- This file is part of ModbusMaster.
- ModbusMaster is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- ModbusMaster is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with ModbusMaster. If not, see <http://www.gnu.org/licenses/>.
- Written by Doc Walker (Rx)
- Copyright © 2009-2013 Doc Walker <4-20ma at wvfans dot net>
- */
- #include <ModbusMaster_Mod.h>
- // instantiate ModbusMaster object as slave ID 2
- // defaults to serial port 0 since no port was specified
- ModbusMaster node(1,1,7); // 1o. = Serial 2o. = Address slave 3o. = Pino de Ema no Master
- unsigned int Cmd = 0;
- unsigned int Start = 0;
- unsigned int Regs = 0;
- unsigned long Data1 = 0;
- unsigned long Data2 = 0;
- byte flag = 0;
- static uint32_t i;
- uint8_t j, result;
- uint16_t data[32];
- //-------------------------------------------------------
- void setup()
- {
- Serial.begin(9600);
- // initialize Modbus communication baud rate
- node.begin(9600); // era 4800
- }
- //--------------------------------------------------------
- void RecebeSerial(String CmdStr, String StartStr, String QtdeStr)
- {
- Serial.println(CmdStr);
- Serial.println(StartStr);
- while (flag == 1)
- {
- while(Serial.available()) // Se algo for recebido pela serial
- {
- Start = Serial.parseInt();
- Serial.println(Start);
- flag = 2;
- }
- }
- Serial.println(QtdeStr);
- while (flag == 2)
- {
- while(Serial.available()) // Se algo for recebido pela serial
- {
- Regs = Serial.parseInt();
- Serial.println(Regs);
- flag = 0;
- }
- }
- }
- //-----------------------------------------------
- void LeDados()
- {
- Serial.print("Result ");
- Serial.println(result);
- if (result == node.ku8MBSuccess)
- {
- for (j = 0; j < Regs; j++)
- {
- data[j] = node.getResponseBuffer(j);
- Serial.println(data[j]);
- }
- }
- }
- //-----------------------------------------------
- void loop()
- {
- Serial.println("Digite o comando: ");
- while(flag != 1)
- {
- if (flag == 0)
- {
- while(Serial.available()) // Se algo for recebido pela serial
- {
- Cmd = Serial.parseInt();
- flag = 1;
- }
- }
- }
- node.setTransmitBuffer(0, lowWord(Data1));
- node.setTransmitBuffer(1, highWord(Data2));
- switch(Cmd)
- {
- //--------------------------------------------------------------
- case 1:
- {
- String Str1 = "Read Coils";
- String Str2 = "Digite Endereco Inicial do Coil:";
- String Str3 = "Digite Quantidade de Coils:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.readCoils(Start, Regs); // 0x01 - Read Coils
- LeDados();
- break;
- }
- //--------------------------------------------------------------
- case 2:
- {
- String Str1 = "Read Discrete Inputs";
- String Str2 = "Digite Endereco Inicial da Input:";
- String Str3 = "Digite Quantidade de Input:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.readDiscreteInputs(Start, Regs); // 0x02 - Read Discrete Inputs
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 5:
- {
- String Str1 = "Write Single Coil";
- String Str2 = "Digite Endereco Inicial do Coil:";
- String Str3 = "Digite Enter";
- RecebeSerial(Str1,Str2,Str3);
- result = node.writeSingleCoil(Start, Regs); // 0x05 - Write Single Coil
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 15:
- {
- String Str1 = "Write Multiple Coils";
- String Str2 = "Digite Endereco Inicial do Coil:";
- String Str3 = "Digite Enter";
- RecebeSerial(Str1,Str2,Str3);
- result = node.writeMultipleCoils(Start, Regs); // 0x0F - Write Multiple Coils (Dec 15)
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 3:
- {
- String Str1 = "Read Holding Registers";
- String Str2 = "Digite Endereco Inicial dos Registers:";
- String Str3 = "Digite Quantidade de Registers:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.readHoldingRegisters(Start, Regs); // 0x03 - Read Holding Registers
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 4:
- {
- String Str1 = "Read Input Registers";
- String Str2 = "Digite Endereco Inicial dos Registers:";
- String Str3 = "Digite Quantidade de Registers:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.readInputRegisters(Start, Regs); // 0x04 - Read Input Registers
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 6:
- {
- String Str1 = "Write Single Registers";
- String Str2 = "Digite Endereco Inicial dos Registers:";
- String Str3 = "Digite Quantidade de Registers:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.writeSingleRegister(Start, Regs); // 0x06 - Write Single Register
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 16:
- {
- String Str1 = "Write Multiple Registers";
- String Str2 = "Digite Endereco Inicial dos Registers:";
- String Str3 = "Digite Quantidade de Registers:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.writeMultipleRegisters(Start, Regs); // 0x10 - Write Multiple Registers (Dec 16)
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 17:
- {
- String Str1 = "Mask Write Register";
- String Str2 = "Digite Endereco Inicial dos Registers:";
- String Str3 = "Digite Quantidade de Registers:";
- RecebeSerial(Str1,Str2,Str3);
- // result = node.maskWriteRegister(); // 0x16 - Mask Write Register (Dec 17)
- LeDados();
- break;
- }
- //-----------------------------------------------------------------------------------
- case 18:
- {
- String Str1 = "Read Write Multiple Registers";
- String Str2 = "Digite Endereco Inicial dos Registers:";
- String Str3 = "Digite Quantidade de Registers:";
- RecebeSerial(Str1,Str2,Str3);
- result = node.readWriteMultipleRegisters(Start, Regs); // 0x17 - Read Write Multiple Registers (Dec 18)
- LeDados();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement