MODBUS laajempi esimerkki
modbus.c File Reference

Modbus functions. More...

#include "kosteus.h"
Include dependency graph for modbus.c:

Functions

unsigned char check_own_address (unsigned char message[])
 Checks if the address in the received ModBus message is the same as the address of this device. More...
 
unsigned char get_function (unsigned char *message)
 Retrieves the function code from the received ModBus message. More...
 
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...
 
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...
 
unsigned char hex_to_ascii_hex (unsigned char response[], unsigned char length)
 Converts characters from Hex to ASCII-Hex. More...
 
unsigned char ascii_hex_to_hex (unsigned char *message)
 Converts two ASCII-Hex characters into one Hex-character. 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 check_LRC (unsigned char *message, unsigned char length_of_message)
 Check the LRC in the received ModBus message. More...
 
unsigned char create_response_message (unsigned char *response)
 Creates a response message.Not in use. 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...
 
void create_diagnostic_response (unsigned int sub_function, unsigned int diagnostic_counter_value)
 Creates a response for ModBus Diagnostic query. More...
 
void create_echo_of_query (unsigned char *message, unsigned char *length_of_response_hex)
 Creates an echo of the received ModBus query. More...
 
void error_getting_message (unsigned char *s, unsigned char error_code)
 Sends a debug message based on a specific error. More...
 
void transmit_response (unsigned char *send_pointer, unsigned char length)
 Transmits the response to a ModBus query. More...
 
void clear_receive_buffer (void)
 Clears the receive buffer. More...
 
void clear_response_buffer (void)
 Clears the response buffer. More...
 

Variables

unsigned char message [MAX_LENGTH_OF_MESSAGE]
 
unsigned char response [MAX_LENGTH_OF_MESSAGE]
 
unsigned char response_in_ascii_hex [MAX_LENGTH_OF_MESSAGE]
 
unsigned char length_of_ascii_hex_response
 
unsigned char * send_pointer
 
volatile unsigned char receive_buffer [MAX_LENGTH_OF_BUFFER]
 
unsigned char received_character
 
unsigned int number_of_points
 
unsigned intstart_address
 
volatile unsigned char nro_characters_received
 
int previouse_received_character
 
unsigned int start_address
 
unsigned char length_of_response
 
unsigned char length_of_message
 
unsigned char modbuss_address
 
unsigned long int receive_timeout
 
unsigned char array_of_ascii_characters [16] = "0123456789ABCDEF"
 

Detailed Description

Modbus functions.


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[])

Checks if the address in the received ModBus message is the same as the address of this device.


Parameters
messageThe received ModBus message.
Returns
OK if the address is the same. Otherwise FALSE.

Here is the call graph for this function:

Here is the caller graph for this function:

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_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_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:

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 
)

Converts characters from Hex to ASCII-Hex.


Parameters
responseArray of characters to be converted.
lengthLength of the array.
Returns
Quantity of generated ASCII-Hex characters. Value used to determine the length of the ModBus (ASCII-Hex)response.

Here is the caller graph for this function:

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:

Variable Documentation

unsigned char array_of_ascii_characters[16] = "0123456789ABCDEF"
unsigned intstart_address
unsigned char length_of_ascii_hex_response
unsigned char length_of_message
unsigned char length_of_response
unsigned char message[MAX_LENGTH_OF_MESSAGE]

Totally received modbus frame

unsigned char modbuss_address
volatile unsigned char nro_characters_received
unsigned int number_of_points
int previouse_received_character
volatile unsigned char receive_buffer[MAX_LENGTH_OF_BUFFER]
unsigned long int receive_timeout
unsigned char received_character
unsigned char response[MAX_LENGTH_OF_MESSAGE]

Response frame to be sended to master

unsigned char response_in_ascii_hex[MAX_LENGTH_OF_MESSAGE]
unsigned char* send_pointer
unsigned int start_address