MODBUS laajempi esimerkki
modbus.h File Reference

Header for modbus.c. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BUS_MESSAGE_COUNTER   0
 
#define BUS_COMMUNICATION_ERROR_COUNTER   4
 
#define EXCEPTION_ERROR_COUNTER   8
 
#define SLAVE_MESSAGE_COUNTER   12
 
#define BUS_CHARACTER_OVERRUN_COUNTER   16
 
#define FIRST_START_INDICATOR   16
 
#define BOOT_COUNTER   20
 
#define MAX_LENGTH_OF_MESSAGE   128
 
#define MAX_LENGTH_OF_BUFFER   MAX_LENGTH_OF_MESSAGE*2
 
#define QUANTITY_MEASUREMENT_REGISTERS   8
 
#define QUANTITY_OF_COUNTER_REGISTERS   3
 
#define START_OF_MEDIAN_REGISTERS   0
 
#define END_OF_MEDIAN_REGISTERS   8
 
#define START_OF_REAL_TIME_REGISTERS   100
 
#define END_OF_REAL_TIME_REGISTERS   108
 
#define START_OF_COUNTER_REGISTERS   200
 
#define END_OF_COUNTER_REGISTERS   203
 
#define PLACE_OF_BOOT_COUNTER_HIGH   3
 
#define PLACE_OF_BOOT_COUNTER_LOW   4
 
#define PLACE_OF_EXCEPTION_ERROR_COUNTER_HIGH   5
 
#define PLACE_OF_EXCEPTION_ERROR_COUNTER_LOW   6
 
#define PLACE_OF_BUS_COMMUNICATION_ERROR_COUNTER_HIGH   7
 
#define PLACE_OF_BUS_COMMUNICATION_ERROR_COUNTER_LOW   8
 
#define MEDIAN_MEASUREMENT   1
 
#define REAL_TIME_MEASUREMENT   2
 
#define COUNTER_QUERY   3
 
#define LENGTH_OF_CLEAR_COUNTERS_QUERY   7
 

Enumerations

enum  {
  WAIT, RECEIVE, WAITING_NEXT_CHAR, MESSAGE_RECEIVED,
  CHECK_END_COND, CHECK_LRC, CHECK_ADDRESS, CHECK_FUNCTION,
  GET_START_ADDRESS, GET_NUMBER_OF_POINTS, MAKE_RESPONSE_MESSAGE, ERROR,
  DONE, TRANSMIT
}
 
enum  {
  MODBUSS_ADDRESS_START = 1, MODBUSS_FUNCTION_START = 3, POSITION_OF_DATA_LENGTH = 3, MODBUS_REGISTER_START_ADDRESS_HI = 5,
  MODBUS_REGISTER_START_ADDRESS_LOW = 7, MODBUSS_SUB_FUNCTION_HIGH = 5, MODBUSS_SUB_FUNCTION_LOW = 7, MODBUS_NRO_POINTS_HI = 9,
  MODBUS_NRO_POINTS_LOW = 11
}
 
enum  { READ_HOLDING_REGISTERS =0x03, INFO =0x04, DIAGNOSTICS =0x08 }
 
enum  {
  CLEAR_COUNTERS_AND_DIAGNOSTIC_REGISTERS = 0x0A, RETURN_BUS_MESSAGE_COUNT = 0x0B, RETURN_BUS_COMMUNICATION_ERROR_COUNT = 0x0C, RETURN_SLAVE_EXCEPTION_ERROR_COUNT = 0x0D,
  RETURN_SLAVE_MESSAGE_COUNT = 0x0E, RETURN_SLAVE_NO_RESPONSE_COUNT = 0x0F, RETURN_SLAVE_NAK_COUNT = 0x10, RETURN_SLAVE_BUSY_COUNT = 0x11,
  RETURN_BUS_CHARACTER_OVERRUN_COUNT = 0x12, RETURN_IOP_OVERRUN_COUNT = 0x13, CLEAR_OVERRUN_COUNTER_AND_FLAG = 0x14, GET_CLEAR_MODBUS_PLUS_STATISTICS = 0x15
}
 
enum  {
  ILLEGAL_FUNCTION = 0x01, ILLEGAL_DATA_ADDRESS = 0x02, ILLEGAL_DATA_VALUE = 0x03, SLAVE_DEVICE_FAILURE = 0x04,
  NO_LF, MAX_SIZE_EXEEDED, LRC_ERROR, BAD_CHARACTER,
  UNKNOWN_FUNCTION, UNKNOWN_STATE, UNKNOWN_ADDRESS, CHAR_RECEIVE_TIMEOUT,
  ILLECAL_END_CONDITION, ILLECAL_SUB_FUNCTION, ILLEGAL_START_CHARACTER
}
 

Functions

unsigned char create_response_message (unsigned char *response)
 Creates a response message.Not in use. More...
 
void clear_receive_buffer (void)
 Clears the receive buffer. More...
 
void clear_response_buffer (void)
 Clears the response buffer. More...
 
unsigned char check_LRC (unsigned char *response, unsigned char length_of_message)
 Check the LRC in the received ModBus message. More...
 
unsigned char calculate_LRC (unsigned char *message, unsigned char nro_characters)
 Calculates LRC checksum from received ModBus message. More...
 
unsigned char calculate_response_LRC (unsigned char *message, unsigned char nro_characters)
 Calculates LRC checksum for the response to be sended. Called only once. More...
 
unsigned char ascii_hex_to_hex (unsigned char *start_address)
 Converts two ASCII-Hex characters into one Hex-character. More...
 
unsigned char get_function (unsigned char *message)
 Retrieves the function code from the received ModBus message. More...
 
unsigned char check_own_address (unsigned char *message)
 
void create_echo_of_query (unsigned char *message, unsigned char *length_of_response_hex)
 Creates an echo of the received ModBus query. More...
 
void create_diagnostic_response (unsigned int sub_function, unsigned int diagnostic_counter_value)
 Creates a response for ModBus Diagnostic query. More...
 
void create_exception_response (unsigned char function, unsigned char error_condition)
 Creates a ModBus Exceprion Response message. Described in page 95 Modicon Modbus Protocol Reference Guide. More...
 
unsigned int get_start_address_of_holding_registers (unsigned char *message)
 Retrieves the Start Address value for Holding Register reading. More...
 
unsigned int get_number_of_points (unsigned char *message)
 Retrieves the Number of Points value for Holding Register reading. More...
 
void create_and_send_exception_response (unsigned char function, unsigned char error_condition)
 
unsigned int get_sub_function (unsigned char *message)
 Retrieves the sub function code from the received ModBus message. Is called only if function code is 0x08. More...
 
void transmit_response (unsigned char *send_pointer, unsigned char length)
 Transmits the response to a ModBus query. More...
 
unsigned char hex_to_ascii_hex (unsigned char *response, unsigned char length)
 
void Fatal_error (unsigned char error_code)
 On fatal error executin comes here and watch dog will handle the reset. More...
 
void error (unsigned char error_code)
 
void error_getting_message (unsigned char *message, unsigned char error_code)
 Sends a debug message based on a specific error. More...
 

Detailed Description

Header for modbus.c.


Macro Definition Documentation

#define BOOT_COUNTER   20
#define BUS_CHARACTER_OVERRUN_COUNTER   16
#define BUS_COMMUNICATION_ERROR_COUNTER   4
#define BUS_MESSAGE_COUNTER   0
#define COUNTER_QUERY   3
#define END_OF_COUNTER_REGISTERS   203
#define END_OF_MEDIAN_REGISTERS   8
#define END_OF_REAL_TIME_REGISTERS   108
#define EXCEPTION_ERROR_COUNTER   8
#define FIRST_START_INDICATOR   16
#define LENGTH_OF_CLEAR_COUNTERS_QUERY   7
#define MAX_LENGTH_OF_BUFFER   MAX_LENGTH_OF_MESSAGE*2
#define MAX_LENGTH_OF_MESSAGE   128
#define MEDIAN_MEASUREMENT   1
#define PLACE_OF_BOOT_COUNTER_HIGH   3
#define PLACE_OF_BOOT_COUNTER_LOW   4
#define PLACE_OF_BUS_COMMUNICATION_ERROR_COUNTER_HIGH   7
#define PLACE_OF_BUS_COMMUNICATION_ERROR_COUNTER_LOW   8
#define PLACE_OF_EXCEPTION_ERROR_COUNTER_HIGH   5
#define PLACE_OF_EXCEPTION_ERROR_COUNTER_LOW   6
#define QUANTITY_MEASUREMENT_REGISTERS   8
#define QUANTITY_OF_COUNTER_REGISTERS   3
#define REAL_TIME_MEASUREMENT   2
#define SLAVE_MESSAGE_COUNTER   12
#define START_OF_COUNTER_REGISTERS   200
#define START_OF_MEDIAN_REGISTERS   0
#define START_OF_REAL_TIME_REGISTERS   100

Enumeration Type Documentation

anonymous enum
Enumerator
WAIT 

Waiting for ModBus message

RECEIVE 

Message is coming, but not received totally yet

WAITING_NEXT_CHAR 

Inhibits main loop to handle partly received modbus frame

MESSAGE_RECEIVED 
CHECK_END_COND 

CR received and waiting for LF to be come

CHECK_LRC 

Total message received and LRC is checked

CHECK_ADDRESS 

Check if this message belongs to me

CHECK_FUNCTION 

Check what is the modbus function

GET_START_ADDRESS 

Get Start Address of Holding Registers

GET_NUMBER_OF_POINTS 

Get Number of Points of Holding Registers

MAKE_RESPONSE_MESSAGE 

Generate response message

ERROR 

If an error occures in the received ModBus message

DONE 

Message is received and the response is sent.

TRANSMIT 

Transmit respoinse to master

anonymous enum
Enumerator
MODBUSS_ADDRESS_START 
MODBUSS_FUNCTION_START 
POSITION_OF_DATA_LENGTH 
MODBUS_REGISTER_START_ADDRESS_HI 
MODBUS_REGISTER_START_ADDRESS_LOW 
MODBUSS_SUB_FUNCTION_HIGH 
MODBUSS_SUB_FUNCTION_LOW 
MODBUS_NRO_POINTS_HI 
MODBUS_NRO_POINTS_LOW 
anonymous enum
Enumerator
READ_HOLDING_REGISTERS 

Read Holding Registers

INFO 

Report Slave ID (Currently not implemented)

DIAGNOSTICS 

Diagnostics

anonymous enum
Enumerator
CLEAR_COUNTERS_AND_DIAGNOSTIC_REGISTERS 
RETURN_BUS_MESSAGE_COUNT 
RETURN_BUS_COMMUNICATION_ERROR_COUNT 
RETURN_SLAVE_EXCEPTION_ERROR_COUNT 
RETURN_SLAVE_MESSAGE_COUNT 
RETURN_SLAVE_NO_RESPONSE_COUNT 
RETURN_SLAVE_NAK_COUNT 
RETURN_SLAVE_BUSY_COUNT 
RETURN_BUS_CHARACTER_OVERRUN_COUNT 
RETURN_IOP_OVERRUN_COUNT 
CLEAR_OVERRUN_COUNTER_AND_FLAG 
GET_CLEAR_MODBUS_PLUS_STATISTICS 
anonymous enum
Enumerator
ILLEGAL_FUNCTION 

ModBus Exception Error code = 0x01

ILLEGAL_DATA_ADDRESS 

ModBus Exception Error code = 0x02

ILLEGAL_DATA_VALUE 

ModBus Exception Error code = 0x03

SLAVE_DEVICE_FAILURE 

ModBus Exception Error code = 0x04

NO_LF 

Message had CR but LF was not followed

MAX_SIZE_EXEEDED 

Modbus message was too long

LRC_ERROR 

LRC was calculated differently than was sent

BAD_CHARACTER 

Character was not HEX digit

UNKNOWN_FUNCTION 

Modbus function was not understand by this code

UNKNOWN_STATE 

If fatal error occurs

UNKNOWN_ADDRESS 

The ModBus message was sent to someone else

CHAR_RECEIVE_TIMEOUT 

Character receive timeout occurs

ILLECAL_END_CONDITION 

Character receive timeout occurs

ILLECAL_SUB_FUNCTION 

Illegal Diagnostic subfunction

ILLEGAL_START_CHARACTER 

No ":" character at the beginning of ModBus message. (Not used in main State Machine, only defined here)

Function Documentation

unsigned char ascii_hex_to_hex ( unsigned char *  message)

Converts two ASCII-Hex characters into one Hex-character.


Parameters
messagePointer to two ASCII-Hex characters to be converted.
Returns
The generated Hex character.
Warning
The ASCII-Hex characters must be in an array sequentially.

Here is the caller graph for this function:

unsigned char calculate_LRC ( unsigned char *  message,
unsigned char  nro_characters 
)

Calculates LRC checksum from received ModBus message.


Parameters
messageThe received ModBus message in ASCII-Hex.
nro_charactersNumber of characters in the received ModBus message.
Returns
Calculated LRC value.
Warning
Must be noted that *message parameter must be in ASCII-Hex format without START- and STOP-characters.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char calculate_response_LRC ( unsigned char *  message,
unsigned char  nro_characters 
)

Calculates LRC checksum for the response to be sended. Called only once.


Parameters
messageThe response message in HEX.
nro_charactersNumber of characters to be included in the calculation.
Returns
Calculated LRC value.
Warning
Must be noted that *message parameter must in Hex format.

Here is the caller graph for this function:

unsigned char check_LRC ( unsigned char *  message,
unsigned char  length_of_message 
)

Check the LRC in the received ModBus message.


Parameters
messageThe received ModBus message.
length_of_messageLength of the received message.
Returns
OK if received messages LRC is correct. Otherwise FALSE.
Warning
Must be noted that *message parameter must in ASCII-Hex format with STAR- and STOP-characters..

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char check_own_address ( unsigned char *  message)
void clear_receive_buffer ( void  )

Clears the receive buffer.


Here is the caller graph for this function:

void clear_response_buffer ( void  )

Clears the response buffer.


Here is the caller graph for this function:

void create_and_send_exception_response ( unsigned char  function,
unsigned char  error_condition 
)
void create_diagnostic_response ( unsigned int  sub_function,
unsigned int  diagnostic_counter_value 
)

Creates a response for ModBus Diagnostic query.


Parameters
sub_functionThe sub function value from the received query.
diagnostic_counter_valueDiagnostic counter value responding to the received query.
Warning
kovakoodattu Diagnostic function code

Here is the caller graph for this function:

void create_echo_of_query ( unsigned char *  message,
unsigned char *  length_of_response_hex 
)

Creates an echo of the received ModBus query.


Parameters
messagePointer to the received ModBus query.
length_of_response_hexLength of the query in Hex.

Here is the call graph for this function:

Here is the caller graph for this function:

void create_exception_response ( unsigned char  function,
unsigned char  error_condition 
)

Creates a ModBus Exceprion Response message. Described in page 95 Modicon Modbus Protocol Reference Guide.


Parameters
functionThe ModBus function where the exception occurred.
error_conditionThe condition why the error occurred (Standard ModBus Exception Error code).

Here is the caller graph for this function:

unsigned char create_response_message ( unsigned char *  response)

Creates a response message.Not in use.


Parameters
responsePointer to response message.
Returns
The length of response message.
Warning
Not in use.!!! This function is never called.
Warning
mita tama oikein on ????
void error ( unsigned char  error_code)
void error_getting_message ( unsigned char *  s,
unsigned char  error_code 
)

Sends a debug message based on a specific error.


Parameters
sPointer to message that caused the error.
error_codeSpecific error code.

Here is the caller graph for this function:

void Fatal_error ( unsigned char  error_code)

On fatal error executin comes here and watch dog will handle the reset.


Parameters
error_codeError code for error definition.

Here is the caller graph for this function:

unsigned char get_function ( unsigned char *  message)

Retrieves the function code from the received ModBus message.


Parameters
messagePointer to the received ModBus message.
Returns
The function code.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int get_number_of_points ( unsigned char *  message)

Retrieves the Number of Points value for Holding Register reading.


Parameters
messagePointer to the received ModBus message.
Returns
Number of Points value.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int get_start_address_of_holding_registers ( unsigned char *  message)

Retrieves the Start Address value for Holding Register reading.


Parameters
messagePointer to the received ModBus message.
Returns
Start Address value.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int get_sub_function ( unsigned char *  message)

Retrieves the sub function code from the received ModBus message. Is called only if function code is 0x08.


Parameters
messagePointer to the received ModBus message.
Returns
The sub function code.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char hex_to_ascii_hex ( unsigned char *  response,
unsigned char  length 
)
void transmit_response ( unsigned char *  send_pointer,
unsigned char  length 
)

Transmits the response to a ModBus query.


Parameters
send_pointerResponse in ASCII-Hex without START- and STOP-characters.
lengthLength of the response.

Here is the call graph for this function:

Here is the caller graph for this function: