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); // 1 = Serial 2 = slave 7 = pino de Ema
- unsigned int Start = 0;
- unsigned int Regs = 4;
- byte flag = 0;
- //-------------------------------------------------------
- void setup()
- {
- Serial.begin(9600);
- // initialize Modbus communication baud rate
- node.begin(9600); // era 4800
- }
- //-----------------------------------------------
- void loop()
- {
- static uint32_t i;
- uint8_t j, result;
- uint16_t data[32];
- if (flag == 0)
- {
- // Serial.println("Digite o inicio: ");
- if(Serial.available()) // Se algo for recebido pela serial do módulo bluetooth
- {
- Start = Serial.parseInt();
- Serial.println(Start);
- flag = 1;
- }
- }
- if (flag == 1)
- {
- Serial.println("Digite qtos Regs: ");
- while(flag == 1)
- {
- if(Serial.available()) // Se algo for recebido pela serial do módulo bluetooth
- {
- Regs = Serial.parseInt();
- Serial.println(Regs);
- flag = 0;
- }
- }
- }
- /* // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
- node.setTransmitBuffer(0, lowWord(i));
- // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
- node.setTransmitBuffer(1, highWord(i));
- // slave: write TX buffer to (2) 16-bit registers starting at register 0
- result = node.writeMultipleRegisters(0, 1);
- i++;
- // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
- node.setTransmitBuffer(0, lowWord(i));
- // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
- node.setTransmitBuffer(1, highWord(i));
- // slave: write TX buffer to (2) 16-bit registers starting at register 0
- result = node.writeMultipleRegisters(1, 1);
- i++;
- // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
- node.setTransmitBuffer(0, lowWord(i));
- // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
- node.setTransmitBuffer(1, highWord(i));
- // slave: write TX buffer to (2) 16-bit registers starting at register 0
- result = node.writeMultipleRegisters(2, 1);
- i++;
- // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
- node.setTransmitBuffer(0, lowWord(i));
- // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
- node.setTransmitBuffer(1, highWord(i));
- // slave: write TX buffer to (2) 16-bit registers starting at register 0
- result = node.writeMultipleRegisters(3, 1);
- */ i++;
- // slave: read (6) 16-bit registers starting at register 2 to RX buffer
- // result = node.readHoldingRegisters(Start, Regs);
- // node.setTransmitBuffer(0, lowWord(4500));
- // node.setTransmitBuffer(1, highWord(65537));
- // node.setTransmitBuffer(2, lowWord(22));
- // node.setTransmitBuffer(3, lowWord(65539));
- node.setTransmitBuffer(0, 450);
- node.setTransmitBuffer(1, 220);
- node.setTransmitBuffer(2, 435);
- node.setTransmitBuffer(3, 553);
- result = node.writeMultipleRegisters(0, 4);
- result = node.readHoldingRegisters(Start, Regs);
- //node.readCoils(Start, Regs);
- Serial.print("Result ");
- Serial.println(result);
- // do something with data if read is successful
- if (result == node.ku8MBSuccess)
- {
- for (j = 0; j < Regs; j++)
- {
- data[j] = node.getResponseBuffer(j);
- Serial.println(data[j]);
- }
- }
- //delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement