This library meant to allow mbed boards (tested on F401RE) to act as modbus slave. I don't own this work, I just made some simple modifications so that it runs on mbed. My modification were labeled with "afdhal". Summary: Modified modbus Arduino library for mbed. See readme for more details. Feel free to use it as you want. Special thanks to the original authors.

Dependents:   ModbusRTU-RS232 FoodComputerARM-alpha

Committer:
AfdhalAtiffTan
Date:
Thu Jul 28 13:19:47 2016 +0000
Revision:
2:5318159b5eab
Parent:
1:35fdc7056f66
Added small comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AfdhalAtiffTan 0:6262fc7582a9 1 #ifndef MODBUS_SLAVE_232H
AfdhalAtiffTan 0:6262fc7582a9 2 #define MODBUS_SLAVE_232H
AfdhalAtiffTan 0:6262fc7582a9 3 /****************************************************************************
AfdhalAtiffTan 0:6262fc7582a9 4 *
AfdhalAtiffTan 0:6262fc7582a9 5 * ModbusSlave library implementing a Modbus RTU Slave for Arduino
AfdhalAtiffTan 0:6262fc7582a9 6 * Modified by S.Marco. mailto:sammarcoarmengol@gmail.com
AfdhalAtiffTan 0:6262fc7582a9 7 * Based on the work published by jpmzometa at
AfdhalAtiffTan 0:6262fc7582a9 8 * http://sites.google.com/site/jpmzometa/arduino-mbrt
AfdhalAtiffTan 0:6262fc7582a9 9 *
AfdhalAtiffTan 0:6262fc7582a9 10 * Based also on http://pcscada.com.au by P.Costigan email: phil@pcscada.com.au
AfdhalAtiffTan 0:6262fc7582a9 11 *
AfdhalAtiffTan 0:6262fc7582a9 12 * These library of functions are designed to enable a program send and
AfdhalAtiffTan 0:6262fc7582a9 13 * receive data from a device that communicates using the Modbus protocol.
AfdhalAtiffTan 0:6262fc7582a9 14 *
AfdhalAtiffTan 0:6262fc7582a9 15 * Copyright (C) 2000 Philip Costigan P.C. SCADA LINK PTY. LTD.
AfdhalAtiffTan 0:6262fc7582a9 16 *
AfdhalAtiffTan 0:6262fc7582a9 17 * This file is part of ModbusSlave.
AfdhalAtiffTan 0:6262fc7582a9 18 *
AfdhalAtiffTan 0:6262fc7582a9 19 * ModbusSlave is free software; you can redistribute it and/or modify
AfdhalAtiffTan 0:6262fc7582a9 20 * it under the terms of the GNU General Public License as published by
AfdhalAtiffTan 0:6262fc7582a9 21 * the Free Software Foundation; either version 2 of the License, or
AfdhalAtiffTan 0:6262fc7582a9 22 * (at your option) any later version.
AfdhalAtiffTan 0:6262fc7582a9 23 *
AfdhalAtiffTan 0:6262fc7582a9 24 * This program is distributed in the hope that it will be useful,
AfdhalAtiffTan 0:6262fc7582a9 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
AfdhalAtiffTan 0:6262fc7582a9 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
AfdhalAtiffTan 0:6262fc7582a9 27 * GNU General Public License for more details.
AfdhalAtiffTan 0:6262fc7582a9 28 *
AfdhalAtiffTan 0:6262fc7582a9 29 * You should have received a copy of the GNU General Public License
AfdhalAtiffTan 0:6262fc7582a9 30 * along with this program; if not, write to the Free Software
AfdhalAtiffTan 0:6262fc7582a9 31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
AfdhalAtiffTan 0:6262fc7582a9 32 *
AfdhalAtiffTan 0:6262fc7582a9 33 *
AfdhalAtiffTan 0:6262fc7582a9 34 * The functions included here have been derived from the
AfdhalAtiffTan 0:6262fc7582a9 35 * Modbus Specifications and Implementation Guides
AfdhalAtiffTan 0:6262fc7582a9 36 *
AfdhalAtiffTan 0:6262fc7582a9 37 * http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
AfdhalAtiffTan 0:6262fc7582a9 38 * http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf
AfdhalAtiffTan 0:6262fc7582a9 39 * http://www.modbus.org/docs/PI_MBUS_300.pdf
AfdhalAtiffTan 0:6262fc7582a9 40 *
AfdhalAtiffTan 0:6262fc7582a9 41 ****************************************************************************/
AfdhalAtiffTan 0:6262fc7582a9 42
AfdhalAtiffTan 0:6262fc7582a9 43 /****************************************************************************
AfdhalAtiffTan 0:6262fc7582a9 44 * BEGIN MODBUS RTU SLAVE FUNCTIONS
AfdhalAtiffTan 0:6262fc7582a9 45 ****************************************************************************/
AfdhalAtiffTan 0:6262fc7582a9 46
AfdhalAtiffTan 0:6262fc7582a9 47 #include <inttypes.h>
AfdhalAtiffTan 0:6262fc7582a9 48
AfdhalAtiffTan 0:6262fc7582a9 49 class ModbusSlave232 {
AfdhalAtiffTan 0:6262fc7582a9 50
AfdhalAtiffTan 0:6262fc7582a9 51 private:
AfdhalAtiffTan 0:6262fc7582a9 52 unsigned char slave;
AfdhalAtiffTan 1:35fdc7056f66 53 //char txenpin; //afdhal
AfdhalAtiffTan 0:6262fc7582a9 54
AfdhalAtiffTan 0:6262fc7582a9 55 unsigned int crc(unsigned char *buf, unsigned char start, unsigned char cnt);
AfdhalAtiffTan 0:6262fc7582a9 56 void build_read_packet(unsigned char function, unsigned char count, unsigned char *packet);
AfdhalAtiffTan 0:6262fc7582a9 57 void build_write_packet(unsigned char function, unsigned int start_addr, unsigned char count, unsigned char *packet);
AfdhalAtiffTan 0:6262fc7582a9 58 void build_write_single_packet(unsigned char function, unsigned int write_addr, unsigned int reg_val, unsigned char* packet);
AfdhalAtiffTan 0:6262fc7582a9 59 void build_error_packet(unsigned char function,unsigned char exception, unsigned char *packet);
AfdhalAtiffTan 0:6262fc7582a9 60 void modbus_reply(unsigned char *packet, unsigned char string_length);
AfdhalAtiffTan 0:6262fc7582a9 61 int send_reply(unsigned char *query, unsigned char string_length);
AfdhalAtiffTan 0:6262fc7582a9 62 int receive_request(unsigned char *received_string);
AfdhalAtiffTan 0:6262fc7582a9 63 int modbus_request(unsigned char *data);
AfdhalAtiffTan 0:6262fc7582a9 64 int validate_request(unsigned char *data, unsigned char length, unsigned int regs_size);
AfdhalAtiffTan 0:6262fc7582a9 65 int write_regs(unsigned int start_addr, unsigned char *query, int *regs);
AfdhalAtiffTan 0:6262fc7582a9 66 int preset_multiple_registers(unsigned int start_addr,unsigned char count,unsigned char *query,int *regs);
AfdhalAtiffTan 0:6262fc7582a9 67 int read_holding_registers(unsigned int start_addr, unsigned char reg_count, int *regs);
AfdhalAtiffTan 0:6262fc7582a9 68 int write_single_register(unsigned int write_addr, unsigned char *query, int *regs);
AfdhalAtiffTan 0:6262fc7582a9 69 void configure(long baud, char parity, char txenpin);
AfdhalAtiffTan 0:6262fc7582a9 70
AfdhalAtiffTan 0:6262fc7582a9 71 public:
AfdhalAtiffTan 0:6262fc7582a9 72 /*
AfdhalAtiffTan 0:6262fc7582a9 73 * configure(slave, baud, parity, txenpin)
AfdhalAtiffTan 0:6262fc7582a9 74 *
AfdhalAtiffTan 0:6262fc7582a9 75 * sets the communication parameters for of the serial line.
AfdhalAtiffTan 0:6262fc7582a9 76 *
AfdhalAtiffTan 0:6262fc7582a9 77 * slave: identification number of the slave in the Modbus network (1 to 127)
AfdhalAtiffTan 0:6262fc7582a9 78 * baud: baudrate in bps (typical values 9600, 19200... 115200)
AfdhalAtiffTan 0:6262fc7582a9 79 * parity: a single character sets the parity mode (character frame format):
AfdhalAtiffTan 0:6262fc7582a9 80 * 'n' no parity (8N1); 'e' even parity (8E1), 'o' for odd parity (8O1).
AfdhalAtiffTan 0:6262fc7582a9 81 * txenpin: arduino pin number that controls transmision/reception
AfdhalAtiffTan 0:6262fc7582a9 82 * of an external half-duplex device (e.g. a RS485 interface chip).
AfdhalAtiffTan 0:6262fc7582a9 83 * 0 or 1 disables this function (for a two-device network)
AfdhalAtiffTan 0:6262fc7582a9 84 * >2 for point-to-multipoint topology (e.g. several arduinos)
AfdhalAtiffTan 0:6262fc7582a9 85 */
AfdhalAtiffTan 0:6262fc7582a9 86 void configure(unsigned char slave, long baud, char parity);
AfdhalAtiffTan 0:6262fc7582a9 87
AfdhalAtiffTan 0:6262fc7582a9 88 /*
AfdhalAtiffTan 0:6262fc7582a9 89 * update(regs, regs_size)
AfdhalAtiffTan 0:6262fc7582a9 90 *
AfdhalAtiffTan 0:6262fc7582a9 91 * checks if there is any valid request from the modbus master. If there is,
AfdhalAtiffTan 0:6262fc7582a9 92 * performs the requested action
AfdhalAtiffTan 0:6262fc7582a9 93 *
AfdhalAtiffTan 0:6262fc7582a9 94 * regs: an array with the holding registers. They start at address 1 (master point of view)
AfdhalAtiffTan 0:6262fc7582a9 95 * regs_size: total number of holding registers, i.e. the size of the array regs.
AfdhalAtiffTan 0:6262fc7582a9 96 * returns: 0 if no request from master,
AfdhalAtiffTan 0:6262fc7582a9 97 * NO_REPLY (-1) if no reply is sent to the master
AfdhalAtiffTan 0:6262fc7582a9 98 * an exception code (1 to 4) in case of a modbus exceptions
AfdhalAtiffTan 0:6262fc7582a9 99 * the number of bytes sent as reply ( > 4) if OK.
AfdhalAtiffTan 0:6262fc7582a9 100 */
AfdhalAtiffTan 0:6262fc7582a9 101 int update(int *regs, unsigned int regs_size);
AfdhalAtiffTan 0:6262fc7582a9 102
AfdhalAtiffTan 0:6262fc7582a9 103 // empty constructor
AfdhalAtiffTan 0:6262fc7582a9 104 ModbusSlave232()
AfdhalAtiffTan 0:6262fc7582a9 105 {
AfdhalAtiffTan 0:6262fc7582a9 106
AfdhalAtiffTan 0:6262fc7582a9 107 }
AfdhalAtiffTan 0:6262fc7582a9 108
AfdhalAtiffTan 0:6262fc7582a9 109 };
AfdhalAtiffTan 0:6262fc7582a9 110
AfdhalAtiffTan 0:6262fc7582a9 111 #endif