前言
在单片机方面,针对于通讯常用的协议之一modbus,这里将modbus协议和硬件之间的关系完全独立出来,硬件和协议之间的联系采用的是回调的方式进行一个关联。
说明
1、此协议可直接移植,并不需要关心硬件相关的。
2、modbus相关协议概念自行查找,本文只做代码的实现。
代码
宏定义(modbus_core_define)
modbus_core_define.h
此头文件定义枚举和回调以及相关的功具类
#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 "modbus_core_slave.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接口(modbus_core_master)
头文件
#ifndef PROCOTOCOLBUILDPROJ_MODBUS_CORE_MASTER_H
#define PROCOTOCOLBUILDPROJ_MODBUS_CORE_MASTER_H
#include "stdio.h"
#include "stdint.h"
/**
* @brief 06
* @param slaveAddr
* @param regAddr
* @param val
* @param errorCode 错误码,根据返回状态,如果正确,错误码不生效
* @return
*/
bool Master_WriteOneRegister(uint8_t slaveAddr, uint16_t regAddr, uint16_t val, uint8_t *errorCode);
/**
* @brief 10
* @param slaveAddr
* @param regAddr
* @param data
* @param dataLen
* @param errorCode
* @return
*/
bool Master_WriteMulRegister(uint8_t slaveAddr, uint16_t regAddr, uint8_t *data, uint16_t dataLen, uint8_t *errorCode);
/**
* @brief 05
* @param slaveAddr
* @param regAddr
* @param val
* @param errorCode
* @return
*/
bool Master_WriteOneCoil(uint8_t slaveAddr, uint16_t regAddr, bool val, uint8_t *errorCode);
/**
* @brief 0F
* @param slaveAddr
* @param regAddr
* @param val
* @param dataLen
* @param errorCode
* @return
*/
bool Master_WriteMulCoil(uint8_t slaveAddr, uint16_t regAddr, bool *val, uint16_t dataLen, uint8_t *errorCode);
/**
* @brief 读保持寄存器
* @param slaveAddr
* @param regAddr
* @param regNum
* @param retData 返回的数据(数据长度为寄存器个数*2)
* @param errorCode
* @return
*/
bool Master_ReadHoldRegisters(uint8_t slaveAddr, uint16_t regAddr, uint16_t regNum, uint8_t *retData, uint8_t *errorCode);
/**
* @brief 读输入寄存器
* @param slaveAddr
* @param regAddr
* @param regNum
* @param retData 返回的数据(数据长度为寄存器个数*2)
* @param errorCode
* @return
*/
bool Master_ReadInputRegisters(uint8_t slaveAddr, uint16_t regAddr, uint16_t regNum, uint8_t *retData, uint8_t *errorCode);
/**
* @brief 读线圈状态
* @param slaveAddr
* @param regAddr
* @param regNum 寄存器个数
* @param retData 返回的数据(数据长度为寄存器个数)
* @param errorCode
* @return
*/
bool Master_ReadCoilStatus(uint8_t slaveAddr, uint16_t regAddr, uint16_t regNum, uint8_t *retData, uint8_t *errorCode);
#endif //PROCOTOCOLBUILDPROJ_MODBUS_CORE_MASTER_H
源文件(RTU实现)文章来源:https://www.toymoban.com/news/detail-525866.html
#include "modbus_core.h"
// 错误指令响应长度
#define ERROR_RESP_DATA_LEN (5)
// 错误响应码下标位置
#define ERROR_RESP_CODE_IDX (2)
#define MODBUS_PACKET_BUF_LEN (100)
// modbus 包缓存数组
static uint8_t modbus_packet_buf[MODBUS_PACKET_BUF_LEN] = {0};
static uint8_t core_build_packet(uint8_t slaveAddr, uint16_t regAddr, uint16_t val, uint8_t funcCode, uint8_t *retBuf);
/**
* 从机地址(1)-功能码(1)-寄存器地址(2)--数据(2)-CRC(2)
* @brief 写一个寄存器
* @param slaveAddr 从机地址
* @param regAddr 寄存器地址
* @param val 值
* @param errorCode 错误码
* @return
*/
bool Master_WriteOneRegister(uint8_t slaveAddr, uint16_t regAddr, uint16_t val, uint8_t *errorCode) {
bool retFlag = true;
uint8_t idx = core_build_packet(slaveAddr, regAddr, val, WriteOneReg, modbus_packet_buf);
modbusSendCallBack(modbus_packet_buf, idx);
if (modbusRecCallBack != NULL) {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
if (recLen == idx && Modbus_Crc_Compute(modbus_packet_buf, recLen) == 0) {
} else {
retFlag = false;
if (errorCode != NULL) {
if (recLen == ERROR_RESP_DATA_LEN) {
*errorCode = modbus_packet_buf[ERROR_RESP_CODE_IDX];
} else {
*errorCode = RecDataLenErrorCode; // 接收数据格式不争取
}
}
}
}
return retFlag;
}
/**
* 从机地址(1)-功能码(1)-寄存器地址(2)-寄存器个数(2)-字节数(1)-数据(n)-CRC(2)
* @brief 串口协议包(0x10) 写多个寄存器
* @param slaveAddr 从机地址
* @param regAddr 寄存器地址
* @param data 数据(长度为2的倍数)
* @param dataLen 数据长度()
* @param errorCode 错误码
* @return
*/
bool Master_WriteMulRegister(uint8_t slaveAddr, uint16_t regAddr, uint8_t *data, uint16_t dataLen, uint8_t *errorCode) {
uint8_t idx = 0;
bool retFlag = true;
modbus_packet_buf[idx++] = slaveAddr;
modbus_packet_buf[idx++] = WriteMulReg;
modbus_packet_buf[idx++] = (uint8_t) (regAddr >> 8);
modbus_packet_buf[idx++] = (uint8_t) (regAddr);
uint16_t regNum = (uint16_t) (dataLen / 2);
modbus_packet_buf[idx++] = (uint8_t) (regNum >> 8);
modbus_packet_buf[idx++] = (uint8_t) (regNum);
modbus_packet_buf[idx++] = (uint8_t) (dataLen);
for (int i = 0; i < dataLen; ++i) {
modbus_packet_buf[idx++] = data[i];
}
uint16_t crc = Modbus_Crc_Compute(modbus_packet_buf, idx);
modbus_packet_buf[idx++] = (uint8_t) (crc);
modbus_packet_buf[idx++] = (uint8_t) (crc >> 8);
modbusSendCallBack(modbus_packet_buf, idx);
if (modbusRecCallBack != NULL) {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
if (recLen != 8 || Modbus_Crc_Compute(modbus_packet_buf, recLen) != 0) {
retFlag = false;
if (errorCode != NULL) {
if (recLen == ERROR_RESP_DATA_LEN) {
*errorCode = modbus_packet_buf[ERROR_RESP_CODE_IDX];
} else {
*errorCode = RecDataLenErrorCode; // 接收数据格式不争取
}
}
}
}
return retFlag;
}
/**
* 从机地址(1)-功能码(1)-寄存器地址(2)--数据(2)-CRC(2)
* @brief 写一个线圈
* @param slaveAddr 从机地址
* @param regAddr 寄存器地址
* @param val 值
* @param errorCode 错误码
* @return
*/
bool Master_WriteOneCoil(uint8_t slaveAddr, uint16_t regAddr, bool val, uint8_t *errorCode) {
uint16_t data = val ? 0xFF00 : 0x0000;
bool retFlag = true;
uint8_t idx = core_build_packet(slaveAddr, regAddr, data, WriteOneCoil, modbus_packet_buf);
modbusSendCallBack(modbus_packet_buf, idx);
if (modbusRecCallBack != NULL) {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
if (recLen == idx && Modbus_Crc_Compute(modbus_packet_buf, recLen) == 0) {
} else {
retFlag = false;
if (errorCode != NULL) {
if (recLen == ERROR_RESP_DATA_LEN) {
*errorCode = modbus_packet_buf[ERROR_RESP_CODE_IDX];
} else {
*errorCode = RecDataLenErrorCode; // 接收数据格式不争取
}
}
}
}
return retFlag;
}
/**
* @brief 写多个线圈
* @param slaveAddr
* @param regAddr
* @param val
* @param dataLen
* @param errorCode
* @return
*/
bool Master_WriteMulCoil(uint8_t slaveAddr, uint16_t regAddr, bool *val, uint16_t dataLen, uint8_t *errorCode) {
uint8_t idx = 0;
bool retFlag = true;
// 计算数据占几个字节
uint16_t realDataLen = PackBoolArray(modbus_packet_buf + 7, val, dataLen);
modbus_packet_buf[idx++] = slaveAddr;
modbus_packet_buf[idx++] = WriteMulCoilsReg;
modbus_packet_buf[idx++] = (uint8_t) (regAddr >> 8);
modbus_packet_buf[idx++] = (uint8_t) (regAddr);
modbus_packet_buf[idx++] = (uint8_t) (dataLen >> 8);
modbus_packet_buf[idx++] = (uint8_t) (dataLen);
modbus_packet_buf[idx++] = (uint8_t) (realDataLen);
idx += realDataLen;
uint16_t crc = Modbus_Crc_Compute(modbus_packet_buf, idx);
modbus_packet_buf[idx++] = (uint8_t) (crc);
modbus_packet_buf[idx++] = (uint8_t) (crc >> 8);
modbusSendCallBack(modbus_packet_buf, idx);
if (modbusRecCallBack != NULL) {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
if (recLen == idx && Modbus_Crc_Compute(modbus_packet_buf, recLen) == 0) {
} else {
retFlag = false;
if (errorCode != NULL) {
if (recLen == ERROR_RESP_DATA_LEN) {
*errorCode = modbus_packet_buf[ERROR_RESP_CODE_IDX];
} else {
*errorCode = RecDataLenErrorCode; // 接收数据格式不争取
}
}
}
}
return retFlag;
}
/**
* @brief 读保持寄存器
* @param slaveAddr
* @param regAddr
* @param regNum
* @param retData
* @param errorCode
* @return
*/
bool Master_ReadHoldRegisters(uint8_t slaveAddr, uint16_t regAddr, uint16_t regNum, uint8_t *retData, uint8_t *errorCode) {
bool retFlag = true;
uint8_t packetLen = core_build_packet(slaveAddr, regAddr, regNum, ReadHoldReg, modbus_packet_buf);
modbusSendCallBack(modbus_packet_buf, packetLen);
if (modbusRecCallBack == NULL) {
// todo 回调未注册
return false;
} else {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
// 验证CRC
if (Modbus_Crc_Compute(modbus_packet_buf, recLen) == 0) {
if (recLen == ERROR_RESP_DATA_LEN) {
retFlag = false;
if (errorCode != NULL) *errorCode = modbus_packet_buf[2];
} else {
// todo 拷贝数据
uint8_t cpyDataLen = modbus_packet_buf[2];
memcpy(retData, modbus_packet_buf + 3, cpyDataLen);
}
} else {
// CRC 不正确
return false;
}
}
return retFlag;
}
/**
* @brief
* @param slaveAddr
* @param regAddr
* @param regNum
* @param retData
* @param errorCode
* @return
*/
bool Master_ReadInputRegisters(uint8_t slaveAddr, uint16_t regAddr, uint16_t regNum, uint8_t *retData, uint8_t *errorCode) {
bool retFlag = true;
uint8_t packetLen = core_build_packet(slaveAddr, regAddr, regNum, ReadInputReg, modbus_packet_buf);
modbusSendCallBack(modbus_packet_buf, packetLen);
if (modbusRecCallBack == NULL) {
// todo 回调未注册
return false;
} else {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
// 验证CRC
if (Modbus_Crc_Compute(modbus_packet_buf, recLen) == 0) {
if (recLen == ERROR_RESP_DATA_LEN) {
retFlag = false;
if (errorCode != NULL) *errorCode = modbus_packet_buf[2];
} else {
// todo 拷贝数据
uint8_t cpyDataLen = modbus_packet_buf[2];
memcpy(retData, modbus_packet_buf + 3, cpyDataLen);
}
} else {
// CRC 不正确
return false;
}
}
return retFlag;
}
bool Master_ReadCoilStatus(uint8_t slaveAddr, uint16_t regAddr, uint16_t regNum, uint8_t *retData, uint8_t *errorCode) {
bool retFlag = true;
uint8_t packetLen = core_build_packet(slaveAddr, regAddr, regNum, ReadHoldReg, modbus_packet_buf);
modbusSendCallBack(modbus_packet_buf, packetLen);
if (modbusRecCallBack == NULL) {// 回调未注册
return false;
} else {
uint16_t recLen = modbusRecCallBack(modbus_packet_buf);
// 验证CRC
if (Modbus_Crc_Compute(modbus_packet_buf, recLen) == 0) {
if (recLen == ERROR_RESP_DATA_LEN) {
retFlag = false;
if (errorCode != NULL) *errorCode = modbus_packet_buf[2];
} else {
// todo 拷贝数据
uint8_t cpyDataLen = modbus_packet_buf[2];
memcpy(retData, modbus_packet_buf + 3, cpyDataLen);
}
} else {
// CRC 不正确
return false;
}
}
return retFlag;
}
static uint8_t core_build_packet(uint8_t slaveAddr, uint16_t regAddr, uint16_t val, uint8_t funcCode, uint8_t *retBuf) {
uint8_t idx = 0;
retBuf[idx++] = slaveAddr;
retBuf[idx++] = funcCode;
retBuf[idx++] = (uint8_t) (regAddr >> 8);
retBuf[idx++] = (uint8_t) (regAddr);
retBuf[idx++] = (uint8_t) (val >> 8);
retBuf[idx++] = (uint8_t) (val);
uint16_t crc = Modbus_Crc_Compute(retBuf, idx);
retBuf[idx++] = (uint8_t) (crc);
retBuf[idx++] = (uint8_t) (crc >> 8);
return idx;
}
结尾
1、此代码可直接移植到单片机工程,不受硬件相关的配置文章来源地址https://www.toymoban.com/news/detail-525866.html
到了这里,关于STM32开发之Modbus协议(主站RTU)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!