Modbus rtu 从机代码主要包括五组:
modbus.c modbus.h
modbus_485.c modbus_485.h
modbus_crc.c modbus_crc.h
modbus_time.c modbus_time.h
modbus_uart.c modbus_uart.h
#include "modbus.h"
MODBUS modbus;
u16 Reg[] ={0x0001,
0x0002,
0x0003,
0x0004,
0x0005,
0x0006,
0x0007,
};
// Modbus初始化函数
void Modbus_Init()
{
modbus.myadd = 0x01; //从机设备地址为1
modbus.timrun = 0; //modbus定时器停止计算
Modbus_485_Init();
Modbus_Uart_Init();
Modbus_Time_Init();
}
// Modbus 3号功能码函数
// Modbus 主机读取寄存器值
void Modbus_Func3()
{
u16 Regadd,Reglen,crc;
u8 i,j;
//得到要读取寄存器的首地址
Regadd = modbus.rcbuf[2]*256+modbus.rcbuf[3];
//得到要读取寄存器的数据长度
Reglen = modbus.rcbuf[4]*256+modbus.rcbuf[5];
//发送回应数据包
i = 0;
modbus.sendbuf[i++] = modbus.myadd; //发送本机设备地址
modbus.sendbuf[i++] = 0x03; //发送功能码
modbus.sendbuf[i++] = ((Reglen*2)%256); //返回字节个数
for(j=0;j
#ifndef _modbus_h_
#define _modbus_h_
#include "stm32f10x.h"
#include "modbus_crc.h"
#include "modbus_uart.h"
#include "modbus_485.h"
#include "modbus_time.h"
typedef struct
{
u8 myadd; //本设备从机地址
u8 rcbuf[100]; //modbus接受缓冲区
u8 timout; //modbus数据持续时间
u8 recount; //modbus端口接收到的数据个数
u8 timrun; //modbus定时器是否计时标志
u8 reflag; //modbus一帧数据接受完成标志位
u8 sendbuf[100]; //modbus接发送缓冲区
}MODBUS;
extern MODBUS modbus;
extern u16 Reg[];
void Modbus_Init(void);
void Modbus_Func3(void);
void Modbus_Event(void);
void ISR_Init(void);
#endif
#include "modbus_485.h"
void Modbus_485_Init()
{
//Modbus_485_RT GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(Modbus_485_RT_Clock, ENABLE); //使能485_RT时钟
GPIO_InitStructure.GPIO_Pin = Modbus_485_RT_Pin; //485_RT端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(Modbus_485_RT_Port, &GPIO_InitStructure); //根据设定参数初始化
Modbus_485_RX_Mode; //初始化为接收状态
}
#ifndef _modbus_485_h_
#define _modbus_485_h_
#include "stm32f10x.h"
// modbus信号控制端口
#define Modbus_485_RT_Port GPIOC
#define Modbus_485_RT_Pin GPIO_Pin_2
#define Modbus_485_RT_Clock RCC_APB2Periph_GPIOC
#define Modbus_485_RX_Mode GPIO_ResetBits(Modbus_485_RT_Port,Modbus_485_RT_Pin)
#define Modbus_485_TX_Mode GPIO_SetBits(Modbus_485_RT_Port,Modbus_485_RT_Pin)
void Modbus_485_Init(void);
#endif
#include "modbus_crc.h"
/* CRC 高位字节值表 */
const u8 auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ;
/* CRC低位字节值表*/
const u8 auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19,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5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;
/******************************************************************
功能: CRC16校验
输入:
输出:
******************************************************************/
u16 Modbus_CRC16( u8 *puchMsg, u16 usDataLen )
{
u8 uchCRCHi = 0xFF ; // 高CRC字节初始化
u8 uchCRCLo = 0xFF ; // 低CRC 字节初始化
unsigned long uIndex ; // CRC循环中的索引
while ( usDataLen-- ) // 传输消息缓冲区
{
uIndex = uchCRCHi ^ *puchMsg++ ; // 计算CRC
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return ( uchCRCHi << 8 | uchCRCLo ) ;
}
#ifndef _modbus_crc_h_
#define _modbus_crc_h_
#include "stm32f10x.h"
u16 Modbus_CRC16( u8 *puchMsg, u16 usDataLen );
#endif
#include "modbus_time.h"
void Modbus_Time_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(Modbus_Time_Clock, ENABLE); //TIM时钟使能
TIM_TimeBaseStructure.TIM_Period = Modbus_Time_arr; //自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =Modbus_Time_psc; //预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(Modbus_Timer, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(Modbus_Timer,TIM_IT_Update,ENABLE); //使能或者失能指定的TIM中断
TIM_Cmd(Modbus_Timer, ENABLE); //使能TIMx外设
}
// Modbus 定时器中断函数 1ms中断一次
void Modbus_TIM_IRQHandler(void)
{
if (TIM_GetITStatus(Modbus_Timer, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(Modbus_Timer, TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源
if(modbus.timrun != 0)
{
modbus.timout++;
if(modbus.timout >=8)
{
modbus.timrun = 0;
modbus.reflag = 1;
}
}
}
}
#ifndef _modbus_time_h_
#define _modbus_time_h_
#include "stm32f10x.h"
#include "modbus.h"
/*************modbus默认定时器TIM3******************/
/*************可修改对应的宏来修改TIM****************/
#define Modbus_Time_arr (1000-1) //1us*1000=1ms
#define Modbus_Time_psc (72-1) //72M/72 =1us
#define Modbus_Time_Clock RCC_APB1Periph_TIM3
#define Modbus_Timer TIM3
#define Modbus_Time_IRQn TIM3_IRQn
#define Modbus_TIM_IRQHandler(void) TIM3_IRQHandler(void)
void Modbus_Time_Init(void);
#endif
#include "modbus_uart.h"
// Modbus串口初始化
void Modbus_Uart_Init()
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(Modbus_Uart_Clock,ENABLE); //使能Modbus串口时钟
RCC_APB2PeriphClockCmd(Modbus_Uart_GPIO_Clock|RCC_APB2Periph_AFIO,ENABLE); //使能Modbus串口GPIO时钟
//Moubud USART_TX
GPIO_InitStructure.GPIO_Pin = Modbus_Uart_TX_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(Modbus_Uart_TRX_Port, &GPIO_InitStructure); //初始化
//Moubud USART_RX
GPIO_InitStructure.GPIO_Pin = Modbus_Uart_RX_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(Modbus_Uart_TRX_Port, &GPIO_InitStructure); //初始化
//Moubud USART 初始化设置
USART_InitStructure.USART_BaudRate = Modbus_bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(Modbus_USART, &USART_InitStructure); //初始化Moubud USART
USART_ITConfig(Modbus_USART, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(Modbus_USART, ENABLE); //使能Moubud USART
}
//modbus串口发送一个字节数据
void Modbus_Send_Byte(u8 Modbus_byte)
{
USART_SendData(Modbus_USART,Modbus_byte);
while(USART_GetFlagStatus(Modbus_USART, USART_FLAG_TC) == RESET);
USART_ClearFlag(Modbus_USART, USART_FLAG_TC);
}
//modbus串口中断服务程序
void Modbus_USART_IRQHandler(void)
{
u8 st,Res;
st = USART_GetITStatus(Modbus_USART, USART_IT_RXNE);
if(st == SET)//接收中断
{
Res =USART_ReceiveData(Modbus_USART); //读取接收到的数据
if( modbus.reflag==1) //有数据包正在处理
{
return ;
}
modbus.rcbuf[modbus.recount++] = Res;
modbus.timout = 0;
if(modbus.recount == 1) //已经收到了第二个字符数据
{
modbus.timrun = 1; //开启modbus定时器计时
}
}
}
#ifndef _modbus_uart_h_
#define _modbus_uart_h_
#include "stm32f10x.h"
#include "modbus.h"
#define Modbus_bound 9600
#define Modbus_USART USART2
#define Modbus_Uart_Clock RCC_APB1Periph_USART2
#define Modbus_Uart_GPIO_Clock RCC_APB2Periph_GPIOA
#define Modbus_Uart_TRX_Port GPIOA
#define Modbus_Uart_TX_Pin GPIO_Pin_2
#define Modbus_Uart_RX_Pin GPIO_Pin_3
#define Modbus_USART_IRQn USART2_IRQn
#define Modbus_USART_IRQHandler(void) USART2_IRQHandler(void)
void Modbus_Uart_Init(void);
void Modbus_Send_Byte(u8 Modbus_byte);
#endif