Solar panel
Loading...
Searching...
No Matches
modbus.c File Reference
#include "modbus.h"
#include "crc.h"

Go to the source code of this file.

Macros

#define READ_INPUT_REGISTERS   0x04
 Modbus function for read input register = 04.
 
#define READ_HOLDING_REGISTERS   0x03
 Modbus function for read holding register = 03.
 
#define WRITE_SINGLE_REGISTER   0x06
 Modbus function for write a single register = 06.
 
#define ILLEGAL_FUNCTION   1
 
#define ILLEGAL_DATA_ADDRESS   2
 
#define ILLEGAL_DATA_VALUE   3
 
#define SLAVE_DEVICE_FAILURE   4
 

Functions

void modbus_timer (void)
 
uint16_t measure_voltage ()
 
uint8_t modbus_analyse_and_answer (void)
 
void modbus_char_recvd (void)
 
void modbus_send (uint8_t length)
 
void modbus_init (uint8_t address)
 

Variables

uint8_t modbusAddress
 
uint16_t input_registers [2]
 
uint16_t holding_registers [2]
 
uint8_t rx_buf [256]
 
uint8_t tx_buf [256]
 
uint8_t recPtr = 0
 

Macro Definition Documentation

◆ ILLEGAL_DATA_ADDRESS

#define ILLEGAL_DATA_ADDRESS   2

Definition at line 19 of file modbus.c.

◆ ILLEGAL_DATA_VALUE

#define ILLEGAL_DATA_VALUE   3

Definition at line 20 of file modbus.c.

◆ ILLEGAL_FUNCTION

#define ILLEGAL_FUNCTION   1

Definition at line 18 of file modbus.c.

◆ READ_HOLDING_REGISTERS

#define READ_HOLDING_REGISTERS   0x03

Modbus function for read holding register = 03.

Definition at line 8 of file modbus.c.

◆ READ_INPUT_REGISTERS

#define READ_INPUT_REGISTERS   0x04

Modbus function for read input register = 04.

Definition at line 7 of file modbus.c.

◆ SLAVE_DEVICE_FAILURE

#define SLAVE_DEVICE_FAILURE   4

Definition at line 21 of file modbus.c.

◆ WRITE_SINGLE_REGISTER

#define WRITE_SINGLE_REGISTER   0x06

Modbus function for write a single register = 06.

Definition at line 9 of file modbus.c.

Function Documentation

◆ measure_voltage()

uint16_t measure_voltage ( )

Measure the voltage on the channel for the current. This function is expected to do some filtering.

Returns
The offset

Definition at line 37 of file measure.c.

◆ modbus_analyse_and_answer()

uint8_t modbus_analyse_and_answer ( void  )

Analyses the received frame and answer to server

Returns
an error code if frame not valid

Definition at line 40 of file modbus.c.

◆ modbus_char_recvd()

void modbus_char_recvd ( void  )

This function will be called every time a character has been received on the Modbus serial port. It is called from the interrupt hander.

Parameters
c: the received char // removed !
See also
interrupts.c.

Definition at line 85 of file modbus.c.

◆ modbus_init()

void modbus_init ( uint8_t  address)

Initializes this Modbus library.

Initialize pins, timer, serial port and variables. Interrupts MUST be initialized for the timer and the serial port.

Parameters
address: the Modbus address of this device
See also
modbus_char_recvd
modbus_timer
interrupts.c

Definition at line 108 of file modbus.c.

◆ modbus_send()

void modbus_send ( uint8_t  length)

Send the Modbus frame on tx_buf with CRC added

Parameters
length: length of the frame without the CRC

Definition at line 92 of file modbus.c.

◆ modbus_timer()

void modbus_timer ( void  )

Modbus timer finished. This function is called by the interrupt handler when a MODBUS frame is finished. It must analyse the received packet and reply if required.

See also
interrupts.c

Definition at line 33 of file modbus.c.

Variable Documentation

◆ holding_registers

uint16_t holding_registers[2]

Definition at line 14 of file modbus.c.

◆ input_registers

uint16_t input_registers[2]

Definition at line 13 of file modbus.c.

◆ modbusAddress

uint8_t modbusAddress

Definition at line 12 of file modbus.c.

◆ recPtr

uint8_t recPtr = 0

Definition at line 31 of file modbus.c.

◆ rx_buf

uint8_t rx_buf[256]

Buffers for serial receive and send operations which are more than one byte long

Definition at line 27 of file modbus.c.

◆ tx_buf

uint8_t tx_buf[256]

Definition at line 28 of file modbus.c.