Simple, tiny minimal library to implement a ModBus slave in mbed.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tinymal_modbus.h Source File

tinymal_modbus.h

Go to the documentation of this file.
00001 /** @file tinymal_modbus.h
00002  *  Library to implement a ModBus slave in mbed. 
00003  *  @note Currently supports 38400 baud rate only.
00004  * 
00005  *  @author Fernando Cosentino
00006  *  os.mbed.com/users/fbcosentino
00007  *
00008  *  Example:
00009  *  @code
00010  *  #include "mbed.h"
00011  *  #include "tinymal_modbus.h"
00012  *
00013  *  // pins are for LPC11U35
00014  *  Serial rs485(P0_19, P0_18);  // a serial port object
00015  *  DigitalOut driver_pin(P0_2); // direction driver pin
00016  *  DigitalOut coil1(P0_20);     // a coil - this is the application
00017  *
00018  *  int main() {
00019  *      rs485.baud(38400);
00020  *      tinymod_Init(&rs485, &driver_pin);
00021  *      tinymod_Address(10);
00022  *
00023  *      // tiny_regs is the array containing the modbus registers
00024  *      tiny_regs[0] = 0; // Sets an initial value to register 0
00025  *
00026  *      while(1) {
00027  *          // call this periodically to process the messages
00028  *          tinymod_Check();
00029  *
00030  *          // make the coil reflect the internal modbus register
00031  *          if (tiny_regs[0] > 0) {
00032  *              coil1 = 1;
00033  *          }
00034  *          else {
00035  *              coil1 = 0;
00036  *          }
00037  *      } 
00038  *  }
00039  *  @endcode
00040  */
00041 
00042 #include "mbed.h"
00043 
00044 #define TINYMOD_NUM_REGS    16
00045 
00046 #define TINYMOD_MODE_SLAVE   0
00047 #define TINYMOD_MODE_MASTER  1
00048 
00049 #define MODBUS_FUNC_READ            3
00050 #define MODBUS_FUNC_WRITE_SINGLE    6
00051 #define MODBUS_FUNC_WRITE_MULT      16
00052 
00053 
00054 extern uint8_t tinymod_address;
00055 extern int tinymod_mode;
00056 
00057 
00058 /**
00059  *  Array storing the register values - your application will work with this.
00060  */
00061 extern uint16_t tiny_regs[TINYMOD_NUM_REGS];
00062 
00063 
00064 /**
00065  *  Initialises the internal Modbus receiver based on the specified serial port
00066  *  direction driver objects. You must initialise those objects yourself before
00067  *  instancing the tinymal modbus object.
00068  *
00069  *  @param ser_obj Serial instance of the Serial class.
00070  *  @param driver_obj Digital output pin instance of the DigitalOut class
00071  */
00072 void tinymod_Init(Serial * ser_obj, DigitalOut * driver_obj);
00073 
00074 /**
00075  *  Sets the listening address for this device. Valid addresses are in the
00076  *  range of 0 - 247 decimal (0x00 - 0xF7).
00077  *
00078  *  @param addr Address this device should respond to (0 - 247)
00079  */
00080 void tinymod_Address(uint8_t addr);
00081 
00082 /**
00083  *  Internally processes any received messages - you must call this as often
00084  *  as possible (ideally once per main loop if your main loop is short).
00085  *  Messages are not automatically processed to keep interrupt handling to the
00086  *  bare minimum, so you have the freedom to decide when you are going to spend
00087  *  processor time processing the message via this function call.
00088  */
00089 void tinymod_Check(void);