You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
41 lines
1.2 KiB
#ifndef __MODBUS_SLAVE_H
|
|
#define __MODBUS_SLAVE_H
|
|
|
|
#include "stm32g4xx_hal.h"
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "com.h"
|
|
|
|
/* Modbus 相关参数 */
|
|
#define MODBUS_SLAVE_ADDRESS 0x34
|
|
#define MODBUS_FUNCTION_CODE_01 0x01
|
|
#define MODBUS_FUNCTION_CODE_03 0x03
|
|
#define MODBUS_FUNCTION_CODE_06 0x06
|
|
#define MODBUS_FUNCTION_CODE_10 0x10
|
|
#define MODBUS_BUFFER_SIZE 256
|
|
#define MODBUS_TIMEOUT_MS 2
|
|
#define MODBUS_CRC_OFF 0 /* ==1 关闭CRC校验 */
|
|
|
|
/* 寄存器相关参数 */
|
|
#define REGISTER_START_ADDRESS 0x0000
|
|
#define REGISTER_COUNT 12
|
|
|
|
/* 全局变量声明 */
|
|
extern uint16_t holdingRegisters[REGISTER_COUNT];
|
|
extern uint8_t RS485_1_RX;
|
|
extern uint8_t RS485_1_Host_Existed;
|
|
extern char dmk_read_state;
|
|
|
|
/* TComCtrl 回调 — 注册到 RS485_1,由 rd_ComRead 自动调用 */
|
|
int Modbus_Slave_Check(char *_pBuffer, uint32_t _iSize);
|
|
void Modbus_Slave_Decode(const char *_pBuffer, uint32_t _iSize);
|
|
|
|
/* Modbus 主机接口 */
|
|
void Modbus_Master_Init(TComCtrl *ptRS485);
|
|
void MB_WriteHoldingReg(uint8_t _addr, uint16_t _reg, uint16_t _data);
|
|
void calculate_dmk(void);
|
|
|
|
/* CRC 计算 */
|
|
uint16_t CalculateCRC(uint8_t *data, uint8_t length);
|
|
|
|
#endif /* __MODBUS_SLAVE_H */
|
|
|