Advertisement
RuiViana

Modbus_Mod_MPS

Jan 14th, 2017
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.94 KB | None | 0 0
  1.     /**
  2.     @file
  3.     Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
  4.  
  5.     @defgroup setup ModbusMaster Object Instantiation/Initialization
  6.     @defgroup buffer ModbusMaster Buffer Management
  7.     @defgroup discrete Modbus Function Codes for Discrete Coils/Inputs
  8.     @defgroup register Modbus Function Codes for Holding/Input Registers
  9.     @defgroup constant Modbus Function Codes, Exception Codes
  10.     */
  11.     /*
  12.  
  13.       ModbusMaster.h - Arduino library for communicating with Modbus slaves
  14.       over RS232/485 (via RTU protocol).
  15.  
  16.       This file is part of ModbusMaster.
  17.  
  18.       ModbusMaster is free software: you can redistribute it and/or modify
  19.       it under the terms of the GNU General Public License as published by
  20.       the Free Software Foundation, either version 3 of the License, or
  21.       (at your option) any later version.
  22.  
  23.       ModbusMaster is distributed in the hope that it will be useful,
  24.       but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.       GNU General Public License for more details.
  27.  
  28.       You should have received a copy of the GNU General Public License
  29.       along with ModbusMaster.  If not, see <http://www.gnu.org/licenses/>.
  30.  
  31.       Written by Doc Walker (Rx)
  32.       Copyright © 2009-2013 Doc Walker <4-20ma at wvfans dot net>
  33.  
  34.     */
  35.  
  36.  
  37. #ifndef ModbusMaster_Mod_h
  38. #define ModbusMaster_Mod_h
  39.  
  40.    
  41.     /**
  42.     @def __MODBUSMASTER_DEBUG__ (1).
  43.     Set to 1 to enable debugging features within class:
  44.       - pin 4 cycles for each byte read in the Modbus response
  45.       - pin 5 cycles for each millisecond timeout during the Modbus response
  46.     */
  47. #define __MODBUSMASTER_DEBUG__ (1)
  48.  
  49.  
  50.     /* _____STANDARD INCLUDES____________________________________________________ */
  51.     // include types & constants of Wiring core API
  52. #if defined(ARDUINO) && ARDUINO >= 100
  53. #include "Arduino.h"
  54. #else
  55. #include "WProgram.h"
  56. #endif
  57.  
  58.     /* _____UTILITY MACROS_______________________________________________________ */
  59.  
  60.  
  61.     /* _____PROJECT INCLUDES_____________________________________________________ */
  62.     // functions to calculate Modbus Application Data Unit CRC
  63. #include "util/crc16.h"
  64.  
  65.     // functions to manipulate words
  66. #include "util/word.h"
  67.  
  68.  
  69.     /* _____CLASS DEFINITIONS____________________________________________________ */
  70.     /**
  71.     Arduino class library for communicating with Modbus slaves over
  72.     RS232/485 (via RTU protocol).
  73.     */
  74.  
  75. class ModbusMaster
  76. {
  77.   public:
  78.     ModbusMaster();
  79.     ModbusMaster(uint8_t);
  80.     ModbusMaster(uint8_t, uint8_t, uint8_t);
  81.    
  82.     void begin();
  83.     void begin(uint16_t);
  84.     void idle(void (*)());
  85.    
  86.         // Modbus exception codes
  87.         /**
  88.        Modbus protocol illegal function exception.
  89.    
  90.         The function code received in the query is not an allowable action for
  91.         the server (or slave). This may be because the function code is only
  92.         applicable to newer devices, and was not implemented in the unit
  93.         selected. It could also indicate that the server (or slave) is in the
  94.         wrong state to process a request of this type, for example because it is
  95.         unconfigured and is being asked to return register values.
  96.    
  97.         @ingroup constant
  98.        */
  99.     static const uint8_t ku8MBIllegalFunction            = 0x01;
  100.  
  101.        /**
  102.         Modbus protocol illegal data address exception.
  103.    
  104.         The data address received in the query is not an allowable address for
  105.         the server (or slave). More specifically, the combination of reference
  106.         number and transfer length is invalid. For a controller with 100
  107.         registers, the ADU addresses the first register as 0, and the last one
  108.         as 99. If a request is submitted with a starting register address of 96
  109.         and a quantity of registers of 4, then this request will successfully
  110.         operate (address-wise at least) on registers 96, 97, 98, 99. If a
  111.         request is submitted with a starting register address of 96 and a
  112.         quantity of registers of 5, then this request will fail with Exception
  113.         Code 0x02 "Illegal Data Address" since it attempts to operate on
  114.         registers 96, 97, 98, 99 and 100, and there is no register with address
  115.         100.
  116.    
  117.         @ingroup constant
  118.         */
  119.     static const uint8_t ku8MBIllegalDataAddress         = 0x02;
  120.    
  121.         /**
  122.        Modbus protocol illegal data value exception.
  123.    
  124.         A value contained in the query data field is not an allowable value for
  125.         server (or slave). This indicates a fault in the structure of the
  126.         remainder of a complex request, such as that the implied length is
  127.         incorrect. It specifically does NOT mean that a data item submitted for
  128.         storage in a register has a value outside the expectation of the
  129.         application program, since the MODBUS protocol is unaware of the
  130.         significance of any particular value of any particular register.
  131.    
  132.         @ingroup constant
  133.         */
  134.     static const uint8_t ku8MBIllegalDataValue           = 0x03;
  135.    
  136.        /**
  137.        Modbus protocol slave device failure exception.
  138.    
  139.         An unrecoverable error occurred while the server (or slave) was
  140.         attempting to perform the requested action.
  141.    
  142.         @ingroup constant
  143.         */
  144.     static const uint8_t ku8MBSlaveDeviceFailure         = 0x04;
  145.  
  146.        // Class-defined success/exception codes
  147.        /**
  148.        ModbusMaster success.
  149.    
  150.        Modbus transaction was successful; the following checks were valid:
  151.           - slave ID
  152.           - function code
  153.           - response code
  154.           - data
  155.           - CRC
  156.      
  157.         @ingroup constant
  158.        */
  159.     static const uint8_t ku8MBSuccess                    = 0x00;
  160.    
  161.        /**
  162.         ModbusMaster invalid response slave ID exception.
  163.    
  164.         The slave ID in the response does not match that of the request.
  165.    
  166.        @ingroup constant
  167.         */
  168.     static const uint8_t ku8MBInvalidSlaveID             = 0xE0;
  169.    
  170.         /**
  171.        ModbusMaster invalid response function exception.
  172.    
  173.        The function code in the response does not match that of the request.
  174.    
  175.         @ingroup constant
  176.         */
  177.     static const uint8_t ku8MBInvalidFunction            = 0xE1;
  178.    
  179.        /**
  180.        ModbusMaster response timed out exception.
  181.    
  182.        The entire response was not received within the timeout period,
  183.        ModbusMaster::ku8MBResponseTimeout.
  184.    
  185.        @ingroup constant
  186.         */
  187.     static const uint8_t ku8MBResponseTimedOut           = 0xE2;
  188.    
  189.         /**
  190.         ModbusMaster invalid response CRC exception.
  191.    
  192.         The CRC in the response does not match the one calculated.
  193.    
  194.        @ingroup constant
  195.        */
  196.     static const uint8_t ku8MBInvalidCRC                 = 0xE3;
  197.    
  198.     uint16_t getResponseBuffer(uint8_t);
  199.     void     clearResponseBuffer();
  200.     uint8_t  setTransmitBuffer(uint8_t, uint16_t);
  201.     void     clearTransmitBuffer();
  202.    
  203.     void beginTransmission(uint16_t);
  204.     uint8_t requestFrom(uint16_t, uint16_t);
  205.     void sendBit(bool);
  206.     void send(uint8_t);
  207.     void send(uint16_t);
  208.     void send(uint32_t);
  209.     uint8_t available(void);
  210.     uint16_t receive(void);
  211.    
  212.    
  213.     uint8_t  readCoils(uint16_t, uint16_t);
  214.     uint8_t  readDiscreteInputs(uint16_t, uint16_t);
  215.     uint8_t  readHoldingRegisters(uint16_t, uint16_t);
  216.     uint8_t  readInputRegisters(uint16_t, uint8_t);
  217.     uint8_t  writeSingleCoil(uint16_t, uint8_t);
  218.     uint8_t  writeSingleRegister(uint16_t, uint16_t);
  219.     uint8_t  writeMultipleCoils(uint16_t, uint16_t);
  220.     uint8_t  writeMultipleCoils();
  221.     uint8_t  writeMultipleRegisters(uint16_t, uint16_t);
  222.     uint8_t  writeMultipleRegisters();
  223.     uint8_t  maskWriteRegister(uint16_t, uint16_t, uint16_t);
  224.     uint8_t  readWriteMultipleRegisters(uint16_t, uint16_t, uint16_t, uint16_t);
  225.     uint8_t  readWriteMultipleRegisters(uint16_t, uint16_t);
  226.    
  227.   private:
  228.     uint8_t  _u8SerialPort;                                      ///< serial port (0..3) initialized in constructor
  229.     uint8_t  _u8MBSlave;                                         ///< Modbus slave (1..255) initialized in constructor
  230.     uint8_t  _u8MBEna;                                           ///< Modbus Enable 485
  231.     uint16_t _u16BaudRate;                                       ///< baud rate (300..115200) initialized in begin()
  232.     static const uint8_t ku8MaxBufferSize                = 64;   ///< size of response/transmit buffers    
  233.     uint16_t _u16ReadAddress;                                    ///< slave register from which to read
  234.     uint16_t _u16ReadQty;                                        ///< quantity of words to read
  235.     uint16_t _u16ResponseBuffer[ku8MaxBufferSize];               ///< buffer to store Modbus slave response; read via GetResponseBuffer()
  236.     uint16_t _u16WriteAddress;                                   ///< slave register to which to write
  237.     uint16_t _u16WriteQty;                                       ///< quantity of words to write
  238.     uint16_t _u16TransmitBuffer[ku8MaxBufferSize];               ///< buffer containing data to transmit to Modbus slave; set via SetTransmitBuffer()
  239.     uint16_t* txBuffer; // from Wire.h -- need to clean this up Rx
  240.     uint8_t _u8TransmitBufferIndex;
  241.     uint16_t u16TransmitBufferLength;
  242.     uint16_t* rxBuffer; // from Wire.h -- need to clean this up Rx
  243.     uint8_t _u8ResponseBufferIndex;
  244.     uint8_t _u8ResponseBufferLength;
  245.    
  246.         // Modbus function codes for bit access
  247.     static const uint8_t ku8MBReadCoils                  = 0x01; ///< Modbus function 0x01 Read Coils
  248.     static const uint8_t ku8MBReadDiscreteInputs         = 0x02; ///< Modbus function 0x02 Read Discrete Inputs
  249.     static const uint8_t ku8MBWriteSingleCoil            = 0x05; ///< Modbus function 0x05 Write Single Coil
  250.     static const uint8_t ku8MBWriteMultipleCoils         = 0x0F; ///< Modbus function 0x0F Write Multiple Coils
  251.  
  252.         // Modbus function codes for 16 bit access
  253.     static const uint8_t ku8MBReadHoldingRegisters       = 0x03; ///< Modbus function 0x03 Read Holding Registers
  254.     static const uint8_t ku8MBReadInputRegisters         = 0x04; ///< Modbus function 0x04 Read Input Registers
  255.     static const uint8_t ku8MBWriteSingleRegister        = 0x06; ///< Modbus function 0x06 Write Single Register
  256.     static const uint8_t ku8MBWriteMultipleRegisters     = 0x10; ///< Modbus function 0x10 Write Multiple Registers
  257.     static const uint8_t ku8MBMaskWriteRegister          = 0x16; ///< Modbus function 0x16 Mask Write Register
  258.     static const uint8_t ku8MBReadWriteMultipleRegisters = 0x17; ///< Modbus function 0x17 Read Write Multiple Registers
  259.    
  260.        // Modbus timeout [milliseconds]
  261.     static const uint16_t ku16MBResponseTimeout          = 2000; ///< Modbus timeout [milliseconds]
  262.    
  263.         // master function that conducts Modbus transactions
  264.     uint8_t ModbusMasterTransaction(uint8_t u8MBFunction);
  265.    
  266.         // idle callback function; gets called during idle time between TX and RX
  267.     void (*_idle)();
  268. };
  269. #endif
  270.  
  271.     /**
  272.     @example examples/Basic/Basic.pde
  273.     @example examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde
  274.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement