RTU从站协议
说明
1、本文不做协议格式的讲解,只做实现,如需了解协议格式,自行搜索
2、本文不依赖于硬件相关的资源,建立在硬件通讯之上,通过回调的形式和对应的硬件进行关联
3、相关协议内容参照,上一篇RTU主站
代码
宏定义(modbus_core_define)
#ifndef PROCOTOCOLBUILDPROJ_MODBUS_CORE_DEFINE_H
#define PROCOTOCOLBUILDPROJ_MODBUS_CORE_DEFINE_H
#include <stdint.h>
#include "stdio.h"
typedef enum {
/**
* @brief 读线圈状态
*/
ReadCoilState = 0x1,
/**
* @brief 读输入状态
*/
ReadInputState = 0x2,
/**
* @brief 读保持寄存器
*/
ReadHoldReg = 0x3,
/**
* @brief 读输入寄存器
*/
ReadInputReg = 0x4,
/**
* @brief 写一个线圈
*/
WriteOneCoil = 0x05,
/**
* @brief 写一个寄存器
*/
WriteOneReg = 0x06,
/**
* @brief 写多个寄存器
*/
WriteMulReg = 0x10,
/**
* @brief 写多个线圈
*/
WriteMulCoilsReg = 0x0F,
} Modbus_FuncCode_TypeDef;
typedef enum {
// 非法数据
InvalidDataErrorCode = 0x2,
// 接收数据长度不正确
RecDataLenErrorCode = 0xFF,
} Modbus_RecCode_TypeDef;
#define modbus_u8_to_u16(a, b) (uint16_t)((uint16_t)((a)<<8)+(b))
typedef void (*SwSendDataCallBack)(void *data, uint16_t len);
typedef uint16_t (*SwRecDataCallBack)(void *recDataBuf);
#endif //PROCOTOCOLBUILDPROJ_MODBUS_CORE_DEFINE_H
crc校验(modbus_core_crc)
头文件
#ifndef PROCOTOCOLBUILDPROJ_MODBUS_CORE_CRC_H
#define PROCOTOCOLBUILDPROJ_MODBUS_CORE_CRC_H
#include "modbus_core_define.h"
uint16_t Modbus_Crc_Compute(const uint8_t *buf, uint16_t bufLen);
#endif //PROCOTOCOLBUILDPROJ_MODBUS_CORE_CRC_H
源文件
#include "modbus_core_crc.h"
static uint16_t crcTb[];
/**
* @brief Modbus crc计算
* @param buf
* @param bufLen
* @return
*/
uint16_t Modbus_Crc_Compute(const uint8_t *buf, uint16_t bufLen) {
uint8_t num;
uint16_t modbus16 = UINT16_MAX;
for (uint16_t index = 0; index < bufLen; ++index) {
num = (uint8_t) (modbus16 & UINT32_MAX);
modbus16 = (uint16_t) (((uint32_t) modbus16 >> 8) ^ crcTb[(num ^ buf[index]) & UINT8_MAX]);
}
return modbus16;
}
static uint16_t crcTb[] = {
0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
};
核心文件(modbus_core)
头文件
#ifndef PROCOTOCOLBUILDPROJ_MODBUS_CORE_H
#define PROCOTOCOLBUILDPROJ_MODBUS_CORE_H
#include "string.h"
#include "stdbool.h"
#include "modbus_core_define.h"
#include "modbus_core_crc.h"
#include "modbus_core_master.h"
#include "slave_rtu.h"
extern SwSendDataCallBack modbusSendCallBack;
extern SwRecDataCallBack modbusRecCallBack;
__attribute__((unused)) void Modbus_RegistrySendCallBack(SwSendDataCallBack sendDataCallBack);
__attribute__((unused)) void Modbus_RegistryRecCallBack(SwRecDataCallBack recDataCallBack);
/**
* @brief 将bool型指针转字节数组
* @param dst 存储转换的字节数据
* @param data bool数据
* @param dataLen 数据长度
* @return
*/
uint16_t PackBoolArray(uint8_t *dst, const bool *data, uint16_t dataLen);
/**
* @brief bool类型数组转字节数组
* @param dst 存储
* @param cnt 转换的个数
* @param data 字节数据
* @param dataLen 数据长度
*/
void UnPackBool(bool *dst, uint16_t cnt, const uint8_t *data, uint16_t dataLen);
#endif //PROCOTOCOLBUILDPROJ_MODBUS_CORE_H
源文件
#include "modbus_core.h"
// 发送数据回调指针
SwSendDataCallBack modbusSendCallBack = NULL;
// 接收数据回调指针
SwRecDataCallBack modbusRecCallBack = NULL;
/**
* @brief 注册发送数据回调
* @param sendDataCallBack
*/
__attribute__((unused)) void Modbus_RegistrySendCallBack(SwSendDataCallBack sendDataCallBack) {
modbusSendCallBack = sendDataCallBack;
}
/**
* @brief 注册接收数据回调
* @param recDataCallBack
*/
__attribute__((unused)) void Modbus_RegistryRecCallBack(SwRecDataCallBack recDataCallBack) {
modbusRecCallBack = recDataCallBack;
}
uint16_t PackBoolArray(uint8_t *dst, const bool *data, uint16_t dataLen) {
uint8_t retLen = 0;
uint8_t curDat = 0;
uint8_t bitIdx = 0;
for (int i = 0; i < dataLen; ++i) {
if (data[i]) {
curDat |= (uint8_t) (1 << bitIdx);
}
if (++bitIdx == 8) {
dst[retLen++] = curDat;
curDat = 0;
bitIdx = 0;
}
}
if (bitIdx != 8) {
dst[retLen++] = curDat;
}
return retLen;
}
/**
* @brief bool类型数组转字节数组
* @param dst 存储
* @param cnt 转换的个数
* @param data 字节数据
* @param dataLen 数据长度
*/
void UnPackBool(bool *dst, uint16_t cnt, const uint8_t *data, uint16_t dataLen) {
uint16_t retLen = 0;
uint8_t curDat = 0;
uint8_t bitIdx = 0;
for (int i = 0; i < dataLen; ++i) {
curDat = data[i];
for (int j = 0; j < 8; ++j) {
if (cnt == 0) { return; };
*dst++ = (curDat & (1 << j)) != 0;
cnt--;
}
}
}
从站统一API接口(slave_rtu)
头文件
#ifndef PROCOTOCOLBUILDPROJ_SLAVE_RTU_H
#define PROCOTOCOLBUILDPROJ_SLAVE_RTU_H
#include "modbus_core_define.h"
typedef enum {
RSP_OK = 0x0,
// 不支持的功能码
RSP_ERR_CMD = 0x1,
// 寄存器地址错误
RSP_ERR_REG_ADDR = 0x2,
// 数据值域错误
RSP_ERR_VALUE = 0x3,
// 写入失败
RSP_ERR_WRITE = 0x4,
} Slave_Rsp_Code;
/**
* @brief 从机地址
* @param addr
*/
void Modbus_Poll_Init(uint8_t addr);
/**
* @brief 主调用函数
* @param data
* @param len
*/
void Modbus_Poll(uint8_t *data, uint16_t len);
/**
* @brief
* @param regAddr
* @param regNum
* @param retData
*/
Slave_Rsp_Code Slave_ReadHoldReg(uint16_t regAddr, uint16_t regNum, uint8_t *retData);
/**
* @brief 0x4
* @param regAddr
* @param regNum
* @param retData
* @return
*/
Slave_Rsp_Code Slave_ReadInputReg(uint16_t regAddr, uint16_t regNum, uint8_t *retData);
/**
* @brief 读取线圈状态
* @param regAddr
* @param regNum 单字节个数
* @param retData
* @return
*/
Slave_Rsp_Code Slave_ReadCoilState(uint16_t regAddr, uint16_t regNum, bool *retData);
/**
* @brief 读取输入状态
* @param regAddr
* @param regNum 单字节个数
* @param retData
* @return
*/
Slave_Rsp_Code Slave_ReadInputState(uint16_t regAddr, uint16_t regNum, bool *retData);
/**
* @brief 写一个寄存器
* @param regAddr
* @param data
* @return
*/
Slave_Rsp_Code Slave_WriteOneReg(uint16_t regAddr, uint16_t data);
/**
* @brief 连续写多个寄存器
* @param regAddr
* @param regNum
* @param data
* @return
*/
Slave_Rsp_Code Slave_WriteMulReg(uint16_t regAddr, uint16_t regNum, uint8_t *data);
/**
* @brief 写多个寄存器
* @param regAddr
* @param regNum 单字节个数
* @param data
* @return
*/
Slave_Rsp_Code Slave_WriteMulCoilsReg(uint16_t regAddr, uint16_t regNum, bool *data);
/**
* @brief 写一个线圈寄存器
* @param regAddr
* @param val
* @return
*/
Slave_Rsp_Code Slave_WriteOneCoil(uint16_t regAddr, bool val);
#endif //PROCOTOCOLBUILDPROJ_SLAVE_RTU_H
源文件文章来源:https://www.toymoban.com/news/detail-681056.html
#include "modbus_core.h"
#include "slave_rtu.h"
#define FIX_MIN_DATA_LEN 8
typedef struct {
uint8_t slaveAddr;
uint8_t funcCode;
uint8_t regAddr;
uint8_t regNum;
uint8_t *data;
uint16_t dataLen;
} slave_packet_t;
static uint8_t slaveAddr = 1;
static uint8_t modbus_slave_buf[256] = {0};
static bool status_buf[256] = {0};
static slave_packet_t packet;
static void core_slave_data_parse(uint8_t *data, uint16_t len, slave_packet_t *ptr);
static uint16_t core_build_err_resp(uint8_t code, slave_packet_t *pkt, uint8_t *retData);
static uint8_t core_slave_read(uint8_t funcCode, uint16_t regAddr, uint16_t regNum, uint8_t *retData);
void Modbus_Poll_Init(uint8_t addr) {
slaveAddr = addr;
}
/**
* @brief 添加crc
* @param retData
* @param len
* @return
*/
uint16_t add_crc(uint8_t *retData, uint16_t len) {
uint16_t crc = Modbus_Crc_Compute(modbus_slave_buf, len);
modbus_slave_buf[len++] = crc;
modbus_slave_buf[len++] = crc >> 8;
return len;
}
void Modbus_Poll(uint8_t *data, uint16_t len) {
if (len < FIX_MIN_DATA_LEN) return;
if (slaveAddr != data[0] || Modbus_Crc_Compute(data, len) != 0) return;
core_slave_data_parse(data, len, &packet);
uint16_t rspLen = 0;
uint8_t respCode = RSP_OK;
// 获取功能码
switch (packet.funcCode) {
case ReadHoldReg: // 0x3
case ReadInputReg: // 0x4
modbus_slave_buf[rspLen++] = packet.slaveAddr;
modbus_slave_buf[rspLen++] = packet.funcCode;
modbus_slave_buf[rspLen++] = packet.regNum * 2;
respCode = core_slave_read(packet.funcCode, packet.regAddr, packet.regNum, modbus_slave_buf + rspLen);
if (respCode == RSP_OK) {
rspLen += (packet.regNum * 2);
rspLen = add_crc(modbus_slave_buf, rspLen);
}
break;
case ReadCoilState:
case ReadInputState:
// 01 01 00 01 00 01 AC 0A
// 01 01 00 01 00 0A ED CD
modbus_slave_buf[rspLen++] = packet.slaveAddr;
modbus_slave_buf[rspLen++] = packet.funcCode;
// 返回的字节数
if (packet.regNum % 8 == 0) {
modbus_slave_buf[rspLen++] = packet.regNum / 8;
} else {
modbus_slave_buf[rspLen++] = packet.regNum / 8 + 1;
}
if (packet.funcCode == ReadCoilState) {
respCode = Slave_ReadCoilState(packet.regAddr, packet.regNum, status_buf);
} else {
respCode = Slave_ReadInputState(packet.regAddr, packet.regNum, status_buf);
}
if (respCode == RSP_OK) {
uint16_t cnt = PackBoolArray(modbus_slave_buf + rspLen, status_buf, packet.regNum);;
rspLen += cnt;
rspLen = add_crc(modbus_slave_buf, rspLen);
}
break;
case WriteOneReg: // 0x06
respCode = Slave_WriteOneReg(packet.regAddr, modbus_u8_to_u16(packet.data[0], packet.data[1]));
if (respCode == RSP_OK) {
modbusSendCallBack(data, len);
return;
}
break;
case WriteMulReg: // 0x10
case WriteMulCoilsReg:
if (packet.funcCode == WriteMulCoilsReg) {
UnPackBool(status_buf, packet.regNum, packet.data, packet.dataLen);
respCode = Slave_WriteMulCoilsReg(packet.regAddr, packet.regNum, status_buf);
} else {
respCode = Slave_WriteMulReg(packet.regAddr, packet.regNum, packet.data);
}
if (respCode == RSP_OK) {
modbus_slave_buf[rspLen++] = packet.slaveAddr;
modbus_slave_buf[rspLen++] = packet.funcCode;
modbus_slave_buf[rspLen++] = packet.regAddr >> 8;
modbus_slave_buf[rspLen++] = packet.regAddr;
modbus_slave_buf[rspLen++] = packet.regNum >> 8;
modbus_slave_buf[rspLen++] = packet.regNum;
rspLen = add_crc(modbus_slave_buf, rspLen);
modbusSendCallBack(modbus_slave_buf, rspLen);
return;
}
break;
bool stat;
case WriteOneCoil:
stat = modbus_u8_to_u16(*packet.data, *(packet.data + 1)) == 0xFF;
respCode = Slave_WriteOneCoil(packet.regAddr, stat);
if (respCode == RSP_OK) {
modbusSendCallBack(modbus_slave_buf, rspLen);
return;
}
break;
}
// 错误响应
if (respCode != RSP_OK) {
rspLen = core_build_err_resp(respCode, &packet, modbus_slave_buf);
}
modbusSendCallBack(modbus_slave_buf, rspLen);
}
/**
* @brief 核心数据封装
* @param funcCode
* @param regAddr
* @param regNum
* @param retData
* @return
*/
uint8_t core_slave_read(uint8_t funcCode, uint16_t regAddr, uint16_t regNum, uint8_t *retData) {
switch (funcCode) {
case ReadHoldReg:
return Slave_ReadHoldReg(packet.regAddr, packet.regNum, retData);
case ReadInputReg:
return Slave_ReadInputReg(packet.regAddr, packet.regNum, retData);
}
return 0;
}
/**
* @brief 数据长度
* @param data
* @param len
* @param ptr
*/
static void core_slave_data_parse(uint8_t *data, uint16_t len, slave_packet_t *ptr) {
uint8_t idx = 0;
ptr->slaveAddr = data[idx++];
ptr->funcCode = data[idx++];
ptr->regAddr = modbus_u8_to_u16(data[idx++], data[idx++]);
switch (ptr->funcCode) {
case ReadHoldReg:
case ReadInputReg:
case ReadCoilState:
case ReadInputState:
ptr->regNum = modbus_u8_to_u16(data[idx++], data[idx++]);
break;
case WriteOneReg:
case WriteOneCoil:
ptr->data = data + idx;
ptr->dataLen = 2;
break;
case WriteMulReg:
case WriteMulCoilsReg:
ptr->regNum = modbus_u8_to_u16(data[idx++], data[idx++]);
ptr->dataLen = data[idx++];
ptr->data = data + idx;
break;
}
}
/**
* @brief
* @param code
* @param pkt
* @param retData
* @return
*/
static uint16_t core_build_err_resp(uint8_t code, slave_packet_t *pkt, uint8_t *retData) {
uint8_t idx = 0;
retData[idx++] = pkt->slaveAddr;
retData[idx++] = pkt->funcCode | 0x80;
retData[idx++] = 2;
uint16_t crc = Modbus_Crc_Compute(retData, idx);
retData[idx++] = crc;
retData[idx++] = crc >> 8;
return idx;
}
uint16_t reg_start_addr = 0x000;
uint8_t reg_buf[200] = {0, 1};
/**
* @brief 读寄存器
* @param regAddr
* @param regNum
* @param retData
* @return
*/
Slave_Rsp_Code Slave_ReadHoldReg(uint16_t regAddr, uint16_t regNum, uint8_t *retData) {
if (regAddr < reg_start_addr) {
return RSP_ERR_REG_ADDR;
} else {
memcpy(retData, reg_buf, regNum * 2);
return RSP_OK;
}
}/***************************************************************具体实现********************************************************/
/**
* @brief 读取输入寄存器
* @param regAddr
* @param regNum
* @param retData
* @return
*/
Slave_Rsp_Code Slave_ReadInputReg(uint16_t regAddr, uint16_t regNum, uint8_t *retData) {
return 0;
}
Slave_Rsp_Code Slave_ReadCoilState(uint16_t regAddr, uint16_t num, bool *retData) {
printf_s("Slave_ReadCoilState reg: %X; num:%d\n", regAddr, num);
for (int i = 0; i < num; ++i) {
retData[i] = true;
}
return RSP_OK;
}
Slave_Rsp_Code Slave_ReadInputState(uint16_t regAddr, uint16_t regNum, bool *retData) {
printf_s("Slave_ReadInputState reg: %X; num:%d\n", regAddr, regNum);
return RSP_OK;
}
Slave_Rsp_Code Slave_WriteOneReg(uint16_t regAddr, uint16_t data) {
printf_s("reg: %X; val:%d\n", regAddr, data);
return RSP_ERR_REG_ADDR;
}
Slave_Rsp_Code Slave_WriteMulReg(uint16_t regAddr, uint16_t regNum, uint8_t *data) {
uint8_t idx = 0;
for (int i = 0; i < regNum; ++i) {
printf("val:%d\n", modbus_u8_to_u16(data[idx++], data[idx++]));
}
return RSP_OK;
}
Slave_Rsp_Code Slave_WriteMulCoilsReg(uint16_t regAddr, uint16_t regNum, bool *data) {
printf_s("Slave_WriteMulCoilsReg reg: %X; num:%d\n", regAddr, regNum);
return RSP_OK;
}
Slave_Rsp_Code Slave_WriteOneCoil(uint16_t regAddr, bool val) {
return RSP_ERR_REG_ADDR;
}
结尾
1、此代码已测试,无问题。后续更新测试用例文章来源地址https://www.toymoban.com/news/detail-681056.html
到了这里,关于STM32开发之Modbus协议(RTU从站)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!