Advertisement
RuiViana

Master_MPS.ino

Jan 14th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. /*
  2.     Basic_Master - example using ModbusMaster library
  3.     This file is part of ModbusMaster.
  4.    
  5.     ModbusMaster is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.    
  10.     ModbusMaster is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.    
  15.     You should have received a copy of the GNU General Public License
  16.     along with ModbusMaster.  If not, see <http://www.gnu.org/licenses/>.
  17.    
  18.     Written by Doc Walker (Rx)
  19.     Copyright © 2009-2013 Doc Walker <4-20ma at wvfans dot net>
  20.   */
  21.  
  22.   #include <ModbusMaster_Mod.h>
  23.  
  24.   // instantiate ModbusMaster object as slave ID 2
  25.   // defaults to serial port 0 since no port was specified
  26.   ModbusMaster node(1,1,7);   //  1 = Serial  2 = slave  7 = pino de Ema
  27.   unsigned int Start = 0;
  28.   unsigned int Regs = 4;
  29.   byte flag = 0;
  30.   //-------------------------------------------------------
  31.   void setup()
  32.   {
  33.     Serial.begin(9600);
  34.     // initialize Modbus communication baud rate
  35.     node.begin(9600);  // era 4800
  36.   }
  37.   //-----------------------------------------------
  38.   void loop()
  39.   {
  40.     static uint32_t i;
  41.     uint8_t j, result;
  42.     uint16_t data[32];
  43.  
  44.  
  45.    if (flag == 0)
  46.     {
  47.    //   Serial.println("Digite o inicio: ");      
  48.       if(Serial.available())                          // Se algo for recebido pela serial do módulo bluetooth
  49.       {
  50.         Start = Serial.parseInt();
  51.         Serial.println(Start);      
  52.         flag = 1;
  53.       }  
  54.     }
  55.     if (flag == 1)
  56.     {
  57.       Serial.println("Digite qtos Regs: ");      
  58.       while(flag == 1)
  59.       {
  60.         if(Serial.available())                          // Se algo for recebido pela serial do módulo bluetooth
  61.         {
  62.           Regs = Serial.parseInt();
  63.           Serial.println(Regs);      
  64.           flag = 0;
  65.         }
  66.       }
  67.     }
  68.  
  69.  /*  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  70.     node.setTransmitBuffer(0, lowWord(i));
  71.     // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  72.     node.setTransmitBuffer(1, highWord(i));
  73.  
  74.     // slave: write TX buffer to (2) 16-bit registers starting at register 0
  75.     result = node.writeMultipleRegisters(0, 1);
  76.     i++;
  77.  
  78.  
  79.     // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  80.     node.setTransmitBuffer(0, lowWord(i));
  81.     // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  82.     node.setTransmitBuffer(1, highWord(i));    
  83.    
  84.     // slave: write TX buffer to (2) 16-bit registers starting at register 0
  85.     result = node.writeMultipleRegisters(1, 1);
  86.     i++;
  87.    
  88.  
  89.     // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  90.     node.setTransmitBuffer(0, lowWord(i));
  91.     // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  92.     node.setTransmitBuffer(1, highWord(i));
  93.  
  94.     // slave: write TX buffer to (2) 16-bit registers starting at register 0
  95.     result = node.writeMultipleRegisters(2, 1);
  96.     i++;
  97.    
  98.  
  99.     // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  100.     node.setTransmitBuffer(0, lowWord(i));
  101.     // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  102.     node.setTransmitBuffer(1, highWord(i));
  103.    
  104.     // slave: write TX buffer to (2) 16-bit registers starting at register 0
  105.     result = node.writeMultipleRegisters(3, 1);
  106.  */   i++;    
  107.  
  108.  
  109.     // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  110.  //   result = node.readHoldingRegisters(Start, Regs);  
  111.  //     node.setTransmitBuffer(0, lowWord(4500));
  112.  //     node.setTransmitBuffer(1, highWord(65537));
  113.  //     node.setTransmitBuffer(2, lowWord(22));  
  114.  //     node.setTransmitBuffer(3, lowWord(65539));
  115.      
  116. //      node.setTransmitBuffer(0, 6000);
  117. //      node.setTransmitBuffer(1, 220);
  118. //      node.setTransmitBuffer(2, 435);  
  119. //      node.setTransmitBuffer(3, 553);
  120.      
  121. //    result = node.writeMultipleRegisters(0, 4);    
  122.     result = node.writeSingleRegister(0,6000);            
  123.     result = node.readHoldingRegisters(Start, Regs);
  124.    //node.readCoils(Start, Regs);  
  125.        Serial.print("Result ");
  126.        Serial.println(result);  
  127.     // do something with data if read is successful
  128.      if (result == node.ku8MBSuccess)
  129.     {
  130.       for (j = 0; j < Regs; j++)
  131.       {
  132.         data[j] = node.getResponseBuffer(j);
  133.           Serial.println(data[j]);
  134.       }
  135.     }
  136.     //delay(500);    
  137.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement