Files
CHJ/user/Protocol/MODBUS.h
2026-03-20 21:19:53 +08:00

76 lines
2.8 KiB
C

#ifndef __MODBUS_h__
#define __MODBUS_h__
#define MAX 0
#define MIN 1
#define DEFAULT 2
//******************************************************************************
typedef struct
{
u16 StartAddr;
u16 EndAddr;
u16 ByteNumber;
u16 Index;
u16 DataByte;
u16 TxdLen;
u16 RxdLen;
u8 *RxPointer;
u8 *TxPointer;
u8 BusError;
} __MB_Buf_TypeDef;
//******************************************************************************
//error code
#define MUBUS_OK 0
#define ILLEGAL_FUNCTION 1 //未识别的功能代码
#define ILLEGAL_DATA_ADDRESS 2 //未识别或者非法的寄存器地址
#define ILLEGAL_DATA_VALUE 3 //非法的数据域,产生此错误的原因:获取点数不正确,发送数据不正确等。
#define SLAVE_DEVICE_FAILURE 4 //从机执行任务失败
#define ACKNOWLEDGE 5 //从机收到命令 并在处理中
#define SLAVE_DEVICE_BUSY 6 //从机正在忙 稍后再尝试查询信息
//******************************************************************************
#define MODBUS_PROTOCOL 1
#define MODBUS_MAX_DATA 110
//******************************************************************************
#define MAX_BUS_FRAME_ERROR 32
#define MAX_BUS_ADR_ERROR 255
#define SLAVER_ADDR_MAX 247
#define SLAVER_ADDR_MIN 1
//******************************************************************************
#define FUNCTION_CODE 1
//******************************************************************************
// MODBUS COMMAND
#define READ_HOLDING_REGISTERS 0x03
#define PRESET_SINGLE_REGISTER 0x06
#define PRESET_MULTIPLE_REGISTERS 0x10
#define MASK_WRITE_REGISTER 0x16
#define ERROR_TEST 0x08
// Custom function code
#define SKEP_READ_REGISTERS 0x41
#define READ_REALTIME_REGISTERS 0x42
#define READ_SETUP_REGISTERS 0x43
//******************************************************************************
#define ENABLE_REG_MAX 50
//******************************************************************************
extern __MB_Buf_TypeDef MBBuf; //ModBus协议帧存储缓存
extern u16 localAddress; //本机地址
extern u8 isOldProtocol; //
u16 ModBusCRC(u8 *Data_Array , u8 DataLength); //CRC校验码生成
void ComputeTxdDataCRC(void); //ModBusCRC校验码生成
void ModBusErrorInfo(u8 ErrorCode); //ModBus错误信息生成
u8 ModBusProtocolDetection(void); //判断数据帧是否满足ModBus协议
void ModBusDataProcessing(void); //开始处理ModBus协议
void ModBusReadMultiByte(void); //ModBus读操作
void ModBusWriteMultiByte(void); //ModBus写操作
u8 ModbusVariablePointerDec(void);
u8 ModbusVariablePointerProcess(void);
void CopyDataAndJudgeComplete(void);
#endif