Petit Modbus 2.0
Loading...
Searching...
No Matches
PetitModbus.c File Reference
#include "PetitModbus.h"

Go to the source code of this file.

Macros

#define PETITMODBUS_READ_COILS   (1)
 
#define PETITMODBUS_READ_DISCRETE_INPUTS   (2)
 
#define PETITMODBUS_READ_HOLDING_REGISTERS   (3)
 
#define PETITMODBUS_READ_INPUT_REGISTERS   (4)
 
#define PETITMODBUS_WRITE_SINGLE_COIL   (5)
 
#define PETITMODBUS_WRITE_SINGLE_REGISTER   (6)
 
#define PETITMODBUS_WRITE_MULTIPLE_COILS   (15)
 
#define PETITMODBUS_WRITE_MULTIPLE_REGISTERS   (16)
 
#define PETIT_FALSE_FUNCTION   (0)
 
#define PETIT_FALSE_SLAVE_ADDRESS   (1)
 
#define PETIT_DATA_NOT_READY   (2)
 
#define PETIT_DATA_READY   (3)
 
#define PETIT_ERROR_CODE_01   (0x01)
 
#define PETIT_ERROR_CODE_02   (0x02)
 
#define PETIT_ERROR_CODE_03   (0X03)
 
#define PETIT_ERROR_CODE_04   (0x04)
 
#define PETIT_BUF_FN_CODE_I   (1)
 
#define PETIT_BUF_BYTE_CNT_I   (6)
 
#define PETIT_BUF_DAT_M(Idx)
 

Functions

void PetitRxBufferReset ()
 
pb_t PetitRxBufferInsert (pu8_t rcvd)
 
pb_t PetitTxBufferPop (pu8_t *tx)
 
void Petit_CRC16_Calc (const pu16_t Data)
 
pb_t PetitSendMessage (void)
 
void HandlePetitModbusError (pu8_t ErrorCode)
 
pu8_t CheckPetitModbusBufferComplete (void)
 
void Petit_RxRTU (void)
 
void Petit_TxRTU (void)
 
void Petit_ResponseProcess (void)
 
void ProcessPetitModbus (void)
 

Variables

PETIT_RXTX_STATE Petit_RxTx_State = PETIT_RXTX_RX
 
pu8_t PetitBuffer [PETITMODBUS_RXTX_BUFFER_SIZE]
 
pu16_t Petit_CRC16 = 0xFFFF
 
pu16_t PetitBufI = 0
 
pu16_t PetitBufJ = 0
 
pu8_tPetit_Ptr = &PetitBuffer[0]
 
pu16_t Petit_Tx_Delay = 0
 
pu16_t PetitExpectedReceiveCount = 0
 

Detailed Description

This file contains the core of PetitModbus.

Definition in file PetitModbus.c.

Macro Definition Documentation

◆ PETIT_BUF_BYTE_CNT_I

#define PETIT_BUF_BYTE_CNT_I   (6)

Definition at line 31 of file PetitModbus.c.

◆ PETIT_BUF_DAT_M

#define PETIT_BUF_DAT_M (   Idx)
Value:
(((pu16_t)(PetitBuffer[2*(Idx) + 2]) << 8) \
| (pu16_t) (PetitBuffer[2*(Idx) + 3]))
pu8_t PetitBuffer[PETITMODBUS_RXTX_BUFFER_SIZE]
Definition PetitModbus.c:41
#define pu16_t

This macro extracts the contents of the buffer at index as a 16-bit unsigned integer

Definition at line 36 of file PetitModbus.c.

◆ PETIT_BUF_FN_CODE_I

#define PETIT_BUF_FN_CODE_I   (1)

Definition at line 30 of file PetitModbus.c.

◆ PETIT_DATA_NOT_READY

#define PETIT_DATA_NOT_READY   (2)

Definition at line 22 of file PetitModbus.c.

◆ PETIT_DATA_READY

#define PETIT_DATA_READY   (3)

Definition at line 23 of file PetitModbus.c.

◆ PETIT_ERROR_CODE_01

#define PETIT_ERROR_CODE_01   (0x01)

Definition at line 25 of file PetitModbus.c.

◆ PETIT_ERROR_CODE_02

#define PETIT_ERROR_CODE_02   (0x02)

Definition at line 26 of file PetitModbus.c.

◆ PETIT_ERROR_CODE_03

#define PETIT_ERROR_CODE_03   (0X03)

Definition at line 27 of file PetitModbus.c.

◆ PETIT_ERROR_CODE_04

#define PETIT_ERROR_CODE_04   (0x04)

Definition at line 28 of file PetitModbus.c.

◆ PETIT_FALSE_FUNCTION

#define PETIT_FALSE_FUNCTION   (0)

Definition at line 20 of file PetitModbus.c.

◆ PETIT_FALSE_SLAVE_ADDRESS

#define PETIT_FALSE_SLAVE_ADDRESS   (1)

Definition at line 21 of file PetitModbus.c.

◆ PETITMODBUS_READ_COILS

#define PETITMODBUS_READ_COILS   (1)

Definition at line 10 of file PetitModbus.c.

◆ PETITMODBUS_READ_DISCRETE_INPUTS

#define PETITMODBUS_READ_DISCRETE_INPUTS   (2)

Definition at line 11 of file PetitModbus.c.

◆ PETITMODBUS_READ_HOLDING_REGISTERS

#define PETITMODBUS_READ_HOLDING_REGISTERS   (3)

Definition at line 12 of file PetitModbus.c.

◆ PETITMODBUS_READ_INPUT_REGISTERS

#define PETITMODBUS_READ_INPUT_REGISTERS   (4)

Definition at line 13 of file PetitModbus.c.

◆ PETITMODBUS_WRITE_MULTIPLE_COILS

#define PETITMODBUS_WRITE_MULTIPLE_COILS   (15)

Definition at line 16 of file PetitModbus.c.

◆ PETITMODBUS_WRITE_MULTIPLE_REGISTERS

#define PETITMODBUS_WRITE_MULTIPLE_REGISTERS   (16)

Definition at line 17 of file PetitModbus.c.

◆ PETITMODBUS_WRITE_SINGLE_COIL

#define PETITMODBUS_WRITE_SINGLE_COIL   (5)

Definition at line 14 of file PetitModbus.c.

◆ PETITMODBUS_WRITE_SINGLE_REGISTER

#define PETITMODBUS_WRITE_SINGLE_REGISTER   (6)

Definition at line 15 of file PetitModbus.c.

Function Documentation

◆ CheckPetitModbusBufferComplete()

CheckPetitModbusBufferComplete ( void  )
Returns
DATA_READY If data is ready FALSE_SLAVE_ADDRESS If slave address is wrong DATA_NOT_READY If data is not ready FALSE_FUNCTION If functions is wrong

Definition at line 586 of file PetitModbus.c.

◆ HandlePetitModbusError()

void HandlePetitModbusError ( pu8_t  ErrorCode)

Definition at line 172 of file PetitModbus.c.

◆ Petit_CRC16_Calc()

void Petit_CRC16_Calc ( const pu16_t  Data)

Definition at line 126 of file PetitModbus.c.

◆ Petit_ResponseProcess()

Petit_ResponseProcess ( void  )

This function processes the modbus response once it has been determined that the message is for this node and the length is correct.

Note
Only use this function if rx is clear for processing

Definition at line 696 of file PetitModbus.c.

◆ Petit_RxRTU()

Petit_RxRTU ( void  )

Check for data ready, if it is good return answer

Definition at line 630 of file PetitModbus.c.

◆ Petit_TxRTU()

Petit_TxRTU ( void  )

If data is ready send answers!

Definition at line 672 of file PetitModbus.c.

◆ PetitRxBufferInsert()

pb_t PetitRxBufferInsert ( pu8_t  rcvd)

Inserts bits into the buffer on device receive.

Parameters
[in]rcvdthe byte to insert into the buffer
Returns
bytes "left" to insert into buffer (1 if byte insertion failed)

Definition at line 75 of file PetitModbus.c.

◆ PetitRxBufferReset()

void PetitRxBufferReset ( void  )

Reset the modbus buffer.

This function is called by the interrupt code that handles byte to byte time overrun. It is also called by the validation function to reject data that is not for this device before more system resources are taken.

Definition at line 62 of file PetitModbus.c.

◆ PetitSendMessage()

PetitSendMessage ( void  )

This function starts to send messages.

Returns
always returns true

Definition at line 157 of file PetitModbus.c.

◆ PetitTxBufferPop()

pb_t PetitTxBufferPop ( pu8_t tx)

This function removes a byte from the buffer and places it on "tx" to be sent over rs485.

Parameters
[out]tx
Returns
1 if there is a byte to be sent, 0 otherwise if the buffer is empty

Definition at line 94 of file PetitModbus.c.

◆ ProcessPetitModbus()

ProcessPetitModbus ( void  )

ModBus main core! Call this function into main!

stateDiagram-v2 [*] --> PETIT_RXTX_RX PETIT_RXTX_RX --> PETIT_RXTX_RX: Message not ready PETIT_RXTX_RX --> PETIT_RXTX_PROCESS: Message Ready PETIT_RXTX_PROCESS --> PETIT_RXTX_TX_DATABUF PETIT_RXTX_TX_DATABUF --> PETIT_RXTX_TX_DLY PETIT_RXTX_TX_DLY --> PETIT_RXTX_TX PETIT_RXTX_TX --> PETIT_RXTX_RX

Definition at line 750 of file PetitModbus.c.

Variable Documentation

◆ Petit_CRC16

pu16_t Petit_CRC16 = 0xFFFF

Definition at line 42 of file PetitModbus.c.

◆ Petit_Ptr

pu8_t* Petit_Ptr = &PetitBuffer[0]

Definition at line 48 of file PetitModbus.c.

◆ Petit_RxTx_State

PETIT_RXTX_STATE Petit_RxTx_State = PETIT_RXTX_RX

Definition at line 40 of file PetitModbus.c.

◆ Petit_Tx_Delay

pu16_t Petit_Tx_Delay = 0

Definition at line 50 of file PetitModbus.c.

◆ PetitBuffer

Definition at line 41 of file PetitModbus.c.

◆ PetitBufI

pu16_t PetitBufI = 0

Definition at line 45 of file PetitModbus.c.

◆ PetitBufJ

pu16_t PetitBufJ = 0

Definition at line 46 of file PetitModbus.c.

◆ PetitExpectedReceiveCount

pu16_t PetitExpectedReceiveCount = 0

Definition at line 52 of file PetitModbus.c.