weAut_01 / weAutSys    R 2.2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Files | Data Structures | Defines | Typedefs | Functions
Modbus server
+ + Application (layer) support + +

Overview

At the upper stack levels uIP brings some protocol and application support. weAutSys adapted and uses some of those (DHCP, ARP, DNS) as well as implementing some other protocols.

weAutSys' Modbus server implementation is a comfortable hook for user / application software to expose process and / or other values for reading and writing via Modbus TCP using that well known open protocol published 1979.


Files

file  modbus.h
 

weAutSys' system calls, services and types for the Modbus server


Data Structures

struct  modConfData_t
 The configuration data (type) for a Modbus handling. More...
struct  modTelegr_t
 The (start of a) Modbus TCP/IP telegram. More...
struct  modThr_data_t
 The organisational data for a Modbus handler thread. More...

Defines

#define COIL_SINGLE
 Modbus data model: Single coil bit addressed 'C'.
#define COILS_BYMAPD
 Modbus data model: Coils byte mapped 'c'.
#define DISC_INP_BYMAPD
 Modbus data model: Discrete inputs byte mapped 'i'.
#define HLD_MASK
 Modbus data model: Single Holding register for mask 'H'.
#define HLD_REGISTERS
 Modbus data model: Holding registers 'h'.
#define INP_REGISTERS
 Modbus data model: Input registers 'r'.
#define MODB_FCIND   7
 Modbus: index of function code in the TCP/IP telegram.
#define MODB_MBAB_LEN   7
 Modbus: length of the TCP/IP telegram start (MBAB header)

Defines for Modbus function codes and the like

This is a subset of the function codes defined for the Modbus protocol.

The others not put here are not implemented by this server.

#define WRITE_COIL   0x05
 Modbus function code: write one bit output.
#define READ_COILS   0x01
 Modbus function code: read back bitwise output.
#define WRITE_COILS   0x0F
 Modbus function code: write bitwise output.
#define READ_DISCRETE_INPUTS   0x02
 Modbus function code: read bitwise inputs.
#define READ_INPUT_REGISTERS   0x04
 Modbus function code: read word (16 bit) inputs.
#define READ_HOLDING_REGISTERS   0x03
 Modbus function code: read back word (16 bit) outputs.
#define WRITE_HOLDING_REGISTERS   0x10
 Modbus function code: write word (16 bit) outputs.
#define WRITE_HOLDING_REGISTER   0x06
 Modbus function code: write (one) word (16 bit) output.
#define MASK_WRITE_REGISTER   0x16
 Modbus function code: and and or (one) word (16 bit) output.
#define WRITE_READ_REGISTERS   0x17
 Modbus function code: write than read word (16 bit) outputs.
#define MODB_EXC_FUNC   1
 Modbus exception: unimplemented function code.
#define MODB_EXC_ADDR   2
 Modbus exception: illegal address.
#define MODB_EXC_DATA   3
 Modbus exception: invalid data (or length)
#define MODB_EXC_OPER   4
 Modbus exception: the (partly) performed operation failed.

Typedefs

typedef ptfnct_tfunM_t )(struct modThr_data_t *thrData)
 Type of a protothread function (modThr_data_t *)
typedef funM_tp2ptFunM
 Pointer to a protothread function (modThr_data_t *)

Functions

ptfnct_t appModFun (struct modThr_data_t *m)
 Handle Modbus server events.
ptfnct_t modbusAppcall (void)
 Handle Modbus server events.
void modbusInit (void)
 Initialise the Modbus (server)
void registerAppModFun (p2ptFunM appModFun)
 Register the application Modbus handler function.

Define Documentation

#define WRITE_COIL   0x05

Modbus function code: write one bit output.

This function is to write one single bit output. The lower 3 bits in the target address would be the bit number within a byte and the address / 8 would be the byte number if mapped to bytes.

#define READ_COILS   0x01

Modbus function code: read back bitwise output.

The Modbus standard considers this function code's address as bit number and the quantity as bit count. This implementation will ignore the lower three bits of both values, thus accepting just whole (mapped) bytes as both source address and quantity. Requests with the lower three bits set will be rejected as erroneous.

Rationale: This implementations target processor has neither word operations nor multi-bit shifts. To implement requests not obeying the above restrictions would bring a resource burden not worth the wile.

The same restrictions aplly to WRITE_COILS and READ_DISCRETE_INPUTS. To set a single output bit use WRITE_COIL and to manipulate a selection of bits within a word use MASK_WRITE_REGISTER.

#define WRITE_COILS   0x0F

Modbus function code: write bitwise output.

For the interpretation of target address and quantity and the respective restrictions see READ_COILS.

#define READ_DISCRETE_INPUTS   0x02

Modbus function code: read bitwise inputs.

For the interpretation of source address and quantity and the respective restrictions see READ_COILS.

#define WRITE_READ_REGISTERS   0x17

Modbus function code: write than read word (16 bit) outputs.

This is a time and communication saving combination of WRITE_HOLDING_REGISTERS followed by READ_HOLDING_REGISTERS. This combined output / input usually makes sense if the application maps all (four) Modbus data models to the same address space or uses Modbus' so called holding registers for all analogue, digital or other (process image) input and output.


Typedef Documentation

typedef ptfnct_t( funM_t)(struct modThr_data_t *thrData)

Type of a protothread function (modThr_data_t *)

funM_t is a function returning PT_WAITING, PT_YIELDED, PT_EXITED or PT_ENDED and taking a pointer to a Modbus handler (thread) state (modThr_data_t) as parameter.

typedef funM_t* p2ptFunM

Pointer to a protothread function (modThr_data_t *)

p2ptFunM is a pointer to a function returning PT_WAITING, PT_YIELDED, PT_EXITED or PT_ENDED and taking a pointer to a Modbus handler (thread) state (modThr_data_t) as parameter.


Function Documentation

void modbusInit ( void  )

Initialise the Modbus (server)

This function initialises the Modbus server. It is (indirectly) called whenever the Ethernet link goes up and hence usually after reset.

At present the implementation does nothing, but that may change in future releases.

void registerAppModFun ( p2ptFunM  appModFun)

Register the application Modbus handler function.

Parameters:
appModFunpointer to the function to register; NULL to de-register
See also:
modConfData_t
Examples:
main.c.
ptfnct_t modbusAppcall ( void  )

Handle Modbus server events.

This (system) function handles all Modbus events. And it is a as a protothread (part of) the Modbus server state machine. At present this feature is hardly used as this implementation does its work in one step as is recommended for a registered application / user software handler (see modConfData_t.appModFun).

This Modbus server implements the basic communication with the Modbus clients and calls the appropriate registered application software hooks.

ptfnct_t appModFun ( struct modThr_data_t m)

Handle Modbus server events.

This application function handles the Modbus events prepared and forwarded by the system handler. This function may or may be not implemented by user software. The name could be chosen freely; this declaration is for clarity's sake. If implemented it must be registered prior to incoming Modbus requests to be handled.

For this functions conditions see modConfData_t.appModFun, modConfData, p2ptFunM and funM_t.

Parameters:
mpointer to a Modbus handler (thread) state structure
All details of the request to handle are prepared there. The result has to be put there in the appropriate fields respectively via the pointers provided.
Returns:
PT_WAITING, PT_YIELDED, PT_EXITED or PT_ENDED
If, as very strongly recommended, all work is done in one stint there's no need for thread organisation and the return value is one of the latter two.
Examples:
main.c.