Simple, tiny minimal library to implement a ModBus slave in mbed.
tinymal_modbus.h@0:4f5da8a923c4, 2019-02-22 (annotated)
- Committer:
- fbcosentino
- Date:
- Fri Feb 22 14:31:13 2019 +0000
- Revision:
- 0:4f5da8a923c4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbcosentino | 0:4f5da8a923c4 | 1 | /** @file tinymal_modbus.h |
fbcosentino | 0:4f5da8a923c4 | 2 | * Library to implement a ModBus slave in mbed. |
fbcosentino | 0:4f5da8a923c4 | 3 | * @note Currently supports 38400 baud rate only. |
fbcosentino | 0:4f5da8a923c4 | 4 | * |
fbcosentino | 0:4f5da8a923c4 | 5 | * @author Fernando Cosentino |
fbcosentino | 0:4f5da8a923c4 | 6 | * os.mbed.com/users/fbcosentino |
fbcosentino | 0:4f5da8a923c4 | 7 | * |
fbcosentino | 0:4f5da8a923c4 | 8 | * Example: |
fbcosentino | 0:4f5da8a923c4 | 9 | * @code |
fbcosentino | 0:4f5da8a923c4 | 10 | * #include "mbed.h" |
fbcosentino | 0:4f5da8a923c4 | 11 | * #include "tinymal_modbus.h" |
fbcosentino | 0:4f5da8a923c4 | 12 | * |
fbcosentino | 0:4f5da8a923c4 | 13 | * // pins are for LPC11U35 |
fbcosentino | 0:4f5da8a923c4 | 14 | * Serial rs485(P0_19, P0_18); // a serial port object |
fbcosentino | 0:4f5da8a923c4 | 15 | * DigitalOut driver_pin(P0_2); // direction driver pin |
fbcosentino | 0:4f5da8a923c4 | 16 | * DigitalOut coil1(P0_20); // a coil - this is the application |
fbcosentino | 0:4f5da8a923c4 | 17 | * |
fbcosentino | 0:4f5da8a923c4 | 18 | * int main() { |
fbcosentino | 0:4f5da8a923c4 | 19 | * rs485.baud(38400); |
fbcosentino | 0:4f5da8a923c4 | 20 | * tinymod_Init(&rs485, &driver_pin); |
fbcosentino | 0:4f5da8a923c4 | 21 | * tinymod_Address(10); |
fbcosentino | 0:4f5da8a923c4 | 22 | * |
fbcosentino | 0:4f5da8a923c4 | 23 | * // tiny_regs is the array containing the modbus registers |
fbcosentino | 0:4f5da8a923c4 | 24 | * tiny_regs[0] = 0; // Sets an initial value to register 0 |
fbcosentino | 0:4f5da8a923c4 | 25 | * |
fbcosentino | 0:4f5da8a923c4 | 26 | * while(1) { |
fbcosentino | 0:4f5da8a923c4 | 27 | * // call this periodically to process the messages |
fbcosentino | 0:4f5da8a923c4 | 28 | * tinymod_Check(); |
fbcosentino | 0:4f5da8a923c4 | 29 | * |
fbcosentino | 0:4f5da8a923c4 | 30 | * // make the coil reflect the internal modbus register |
fbcosentino | 0:4f5da8a923c4 | 31 | * if (tiny_regs[0] > 0) { |
fbcosentino | 0:4f5da8a923c4 | 32 | * coil1 = 1; |
fbcosentino | 0:4f5da8a923c4 | 33 | * } |
fbcosentino | 0:4f5da8a923c4 | 34 | * else { |
fbcosentino | 0:4f5da8a923c4 | 35 | * coil1 = 0; |
fbcosentino | 0:4f5da8a923c4 | 36 | * } |
fbcosentino | 0:4f5da8a923c4 | 37 | * } |
fbcosentino | 0:4f5da8a923c4 | 38 | * } |
fbcosentino | 0:4f5da8a923c4 | 39 | * @endcode |
fbcosentino | 0:4f5da8a923c4 | 40 | */ |
fbcosentino | 0:4f5da8a923c4 | 41 | |
fbcosentino | 0:4f5da8a923c4 | 42 | #include "mbed.h" |
fbcosentino | 0:4f5da8a923c4 | 43 | |
fbcosentino | 0:4f5da8a923c4 | 44 | #define TINYMOD_NUM_REGS 16 |
fbcosentino | 0:4f5da8a923c4 | 45 | |
fbcosentino | 0:4f5da8a923c4 | 46 | #define TINYMOD_MODE_SLAVE 0 |
fbcosentino | 0:4f5da8a923c4 | 47 | #define TINYMOD_MODE_MASTER 1 |
fbcosentino | 0:4f5da8a923c4 | 48 | |
fbcosentino | 0:4f5da8a923c4 | 49 | #define MODBUS_FUNC_READ 3 |
fbcosentino | 0:4f5da8a923c4 | 50 | #define MODBUS_FUNC_WRITE_SINGLE 6 |
fbcosentino | 0:4f5da8a923c4 | 51 | #define MODBUS_FUNC_WRITE_MULT 16 |
fbcosentino | 0:4f5da8a923c4 | 52 | |
fbcosentino | 0:4f5da8a923c4 | 53 | |
fbcosentino | 0:4f5da8a923c4 | 54 | extern uint8_t tinymod_address; |
fbcosentino | 0:4f5da8a923c4 | 55 | extern int tinymod_mode; |
fbcosentino | 0:4f5da8a923c4 | 56 | |
fbcosentino | 0:4f5da8a923c4 | 57 | |
fbcosentino | 0:4f5da8a923c4 | 58 | /** |
fbcosentino | 0:4f5da8a923c4 | 59 | * Array storing the register values - your application will work with this. |
fbcosentino | 0:4f5da8a923c4 | 60 | */ |
fbcosentino | 0:4f5da8a923c4 | 61 | extern uint16_t tiny_regs[TINYMOD_NUM_REGS]; |
fbcosentino | 0:4f5da8a923c4 | 62 | |
fbcosentino | 0:4f5da8a923c4 | 63 | |
fbcosentino | 0:4f5da8a923c4 | 64 | /** |
fbcosentino | 0:4f5da8a923c4 | 65 | * Initialises the internal Modbus receiver based on the specified serial port |
fbcosentino | 0:4f5da8a923c4 | 66 | * direction driver objects. You must initialise those objects yourself before |
fbcosentino | 0:4f5da8a923c4 | 67 | * instancing the tinymal modbus object. |
fbcosentino | 0:4f5da8a923c4 | 68 | * |
fbcosentino | 0:4f5da8a923c4 | 69 | * @param ser_obj Serial instance of the Serial class. |
fbcosentino | 0:4f5da8a923c4 | 70 | * @param driver_obj Digital output pin instance of the DigitalOut class |
fbcosentino | 0:4f5da8a923c4 | 71 | */ |
fbcosentino | 0:4f5da8a923c4 | 72 | void tinymod_Init(Serial * ser_obj, DigitalOut * driver_obj); |
fbcosentino | 0:4f5da8a923c4 | 73 | |
fbcosentino | 0:4f5da8a923c4 | 74 | /** |
fbcosentino | 0:4f5da8a923c4 | 75 | * Sets the listening address for this device. Valid addresses are in the |
fbcosentino | 0:4f5da8a923c4 | 76 | * range of 0 - 247 decimal (0x00 - 0xF7). |
fbcosentino | 0:4f5da8a923c4 | 77 | * |
fbcosentino | 0:4f5da8a923c4 | 78 | * @param addr Address this device should respond to (0 - 247) |
fbcosentino | 0:4f5da8a923c4 | 79 | */ |
fbcosentino | 0:4f5da8a923c4 | 80 | void tinymod_Address(uint8_t addr); |
fbcosentino | 0:4f5da8a923c4 | 81 | |
fbcosentino | 0:4f5da8a923c4 | 82 | /** |
fbcosentino | 0:4f5da8a923c4 | 83 | * Internally processes any received messages - you must call this as often |
fbcosentino | 0:4f5da8a923c4 | 84 | * as possible (ideally once per main loop if your main loop is short). |
fbcosentino | 0:4f5da8a923c4 | 85 | * Messages are not automatically processed to keep interrupt handling to the |
fbcosentino | 0:4f5da8a923c4 | 86 | * bare minimum, so you have the freedom to decide when you are going to spend |
fbcosentino | 0:4f5da8a923c4 | 87 | * processor time processing the message via this function call. |
fbcosentino | 0:4f5da8a923c4 | 88 | */ |
fbcosentino | 0:4f5da8a923c4 | 89 | void tinymod_Check(void); |