一、简介及进展
modbus的从机就看了不少的文章了,不过主机的文章比较少,在这里我贡献出我的项目经验,自己做的第一个项目。还是需要继续学习。
首先定义一个发送的数组,data[7],data[8]是crc校验码,这个时候有人会有疑问了,为什么不做发送时间的判断,因为发送时间不能超过3.5ms。一旦超过3.5ms的话,modbus主机是不会处理这段数据的。我在这是按实测的来,仿真和实际操作都没问题
首先,看发送和接收的数据格式
主机发送码::地址 03 00 00 00 02 CRC低位 CRC高位(共8字节)
(1) 地址(1字节):与仪器设置地址相同 1~247
(2) 功能码(1字节):03 使用03功能码读数据
(3) 数据寄存器地址(2字节):0000
(4) 数据数量(2字节):0002 读2个16位数据
(5) CRC(2字节):校验码
仪器发送码:地址 03 04 XX XX XX XX CRC低位 CRC高位(共9字节)
(1) 地址(1字节):仪器设置地址1~247
(2) 功能码(1字节):03 使用03功能码读数据
(3) 数量(1字节):4 发送数据字节数
(4) 数据(4字节):32位标准IEEE754浮点数
(5) CRC(2字节):校验码
例:EXAMPLE
逻辑地址为1的表
读电量:10度
发送
01 03 00 00 00 02 c4 0b
接收
01 03 04 00 00 03 e8 crc16
c4 0b就是crc校验位,可以使用CRCTool工具进行计算
下面具体介绍数据的发送和接收,
定义发送的数组
modbus.databuf[0]=0x01;
modbus.databuf[1]=0x03;
modbus.databuf[2]=0x00;
modbus.databuf[3]=0x00;
modbus.databuf[4]=0x00;
modbus.databuf[5]=0x02;
modbus.databuf[6]=0xc4;
modbus.databuf[7]=0x0b;
发送16位数据,这里要用寄存器来操作,不能使用printf来发送16进制的数组。
void UartASendStr (u8 *pucStr, u8 ulNum)
{
u8 i;
for(i = 0;iwhile ((USART2->SR & USART_FLAG_TC) == (uint16_t)RESET);
USART_SendData(USART2,*pucStr++);
}
}
发送数据,放在了main函数里,目前是调试用。
加了延时,
UartASendStr(modbus.databuf,8);
delay_ms(200);
接收中断
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
modbus.rcbuf[modbus.recount++] = USART_ReceiveData(USART2);
}
}
接收到的数据进行处理
在这里调用了crc校验的算法,可以参考从机的ModbusCRC.c,我是直接用的。
#include "modbusCRC.h"
void Modbud_fun3()
{
u16 Regadd;
u16 Reglen;
u16 i=0;
u16 crc;
u16 rccrc;
Reglen=modbus.rcbuf[2];
if(Reglen==0x04)
{
Regadd=modbus.rcbuf[3]*256+modbus.rcbuf[4];
Regadd=modbus.rcbuf[5]*256+modbus.rcbuf[6];
}
if(Reglen==0x02)
{
Regadd=modbus.rcbuf[3]*256+modbus.rcbuf[4];
Regadd=modbus.rcbuf[5]*256+modbus.rcbuf[6];
}
crc= crc16(&modbus.rcbuf[0], modbus.recount-2);
rccrc=modbus.rcbuf[modbus.recount-2]*256 + modbus.rcbuf[modbus.recount-1];
if(crc == rccrc)
{
i++;//如果校验的crc和发送的数据相同,证明了接收的数据正常,可以使用
}
modbus.recount=0;
modbus.reflag=0;
}
目前已经调通。下面将所有的代码放了上来,供学习用。
主要功能
main函数
#include "bsp_usart.h"
#include "modbus.h"
#include "stm32f10x.h"
int main(void)
{
USART1_Config();
USART2_Config();
shuzu();
//Modbud_fun3();
while(1)
{
// RS485_byte();
UartASendStr(modbus.databuf,8);
delay_ms(200);
Modbud_fun3();
}
}
modbus.c
#include "modbus.h"
#include "modbusCRC.h"
#include "bsp_usart.h"
MODBUS modbus;
void UartASendStr (u8 *pucStr, u8 ulNum)
{
u8 i;
for(i = 0;i
{
while ((USART2->SR & USART_FLAG_TC) == (uint16_t)RESET);
USART_SendData(USART2,*pucStr++);
}
}
void Modbud_fun3()
{
u16 Regadd;
u16 Reglen;
u16 i=0;
u16 crc;
u16 rccrc;
Reglen=modbus.rcbuf[2]; //µÃµ½Òª¶ÁÈ¡µÄÊý¾ÝÊýÁ¿
if(Reglen==0x04)
{
Regadd=modbus.rcbuf[3]*256+modbus.rcbuf[4];
Regadd=modbus.rcbuf[5]*256+modbus.rcbuf[6];
}
if(Reglen==0x02)
{
Regadd=modbus.rcbuf[3]*256+modbus.rcbuf[4];
Regadd=modbus.rcbuf[5]*256+modbus.rcbuf[6];
}
crc= crc16(&modbus.rcbuf[0], modbus.recount-2);
rccrc=modbus.rcbuf[modbus.recount-2]*256 + modbus.rcbuf[modbus.recount-1]; //ÊÕµ½µÄУÑéÂë
if(crc == rccrc)
{
i++;
}
modbus.recount=0;
modbus.reflag=0;
}
void shuzu()
{
modbus.databuf[0]=0x01;
modbus.databuf[1]=0x03;
modbus.databuf[2]=0x00;
modbus.databuf[3]=0x00;
modbus.databuf[4]=0x00;
modbus.databuf[5]=0x02;
modbus.databuf[6]=0xc4;
modbus.databuf[7]=0x0b;
}
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--) ;
}
}
void RS485_byte()
{
RS485_RT_1;
UartASendStr(modbus.databuf,8);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
RS485_RT_0;
Modbud_fun3();
}
modbus.h
#ifndef _modbus_
#define _modbus_
#include "stm32f10x_conf.h"
#define RS485_RT_1 GPIO_SetBits(GPIOA, GPIO_Pin_0) //485·¢ËÍ״̬
#define RS485_RT_0 GPIO_ResetBits(GPIOA, GPIO_Pin_0) //485ÖýÓÊÕ״̬
typedef struct
{
u8 myadd;
u8 rcbuf[100];
u8 databuf[8];
u16 timout;
u8 recount;
u8 timrun;
u8 reflag;
u8 Sendbuf[100];
}MODBUS;
extern MODBUS modbus;
void Modbud_fun3(void );
void shuzu(void );
void UartASendStr (u8 *pucStr, u8 ulNum);
void delay_ms(u16 time);
void RS485_byte(void );
#endif
stm32f10x_it.c
#include "stm32f10x_it.h"
#include "bsp_usart.h"
#include "modbus.h"
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
modbus.rcbuf[modbus.recount++] = USART_ReceiveData(USART2);
}
}
void USART1_IRQHandler(void)
{
u8 a=0;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
a++;
}
}
modbusCRC.c
#include "modbusCRC.h"
const uchar 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, 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, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 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
} ;
uint crc16( uchar *puchMsg, uint usDataLen )
{
uchar uchCRCHi = 0xFF ;
uchar uchCRCLo = 0xFF ;
unsigned long uIndex ;
while ( usDataLen-- )
{
uIndex = uchCRCHi ^ *puchMsg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return ( uchCRCHi << 8 | uchCRCLo ) ;
}
modbusCRC.h
#ifndef _MODBUS_CRC_H_
#define _MODBUS_CRC_H_
#include "stm32f10x_conf.h"
#define uint u16
#define uchar u8
uint crc16( uchar *puchMsg, uint usDataLen );
#endif
bsp_usart.c
#include "bsp_usart.h"
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void NVIC1_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = DEBUG1_USART_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(DEBUG_USARTx, &USART_InitStructure);
NVIC_Configuration();
USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE);
USART_Cmd(DEBUG_USARTx, ENABLE);
}
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DEBUG1_USART_GPIO_APBxClkCmd(DEBUG1_USART_GPIO_CLK, ENABLE);
DEBUG1_USART_APBxClkCmd(DEBUG1_USART_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = DEBUG1_USART_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DEBUG1_USART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DEBUG1_USART_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DEBUG1_USART_RX_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = DEBUG1_USART_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(DEBUG1_USARTx, &USART_InitStructure);
NVIC1_Configuration();
USART_ITConfig(DEBUG1_USARTx, USART_IT_RXNE, ENABLE);
USART_Cmd(DEBUG1_USARTx, ENABLE);
}
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
USART_SendData(pUSARTx,ch);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
unsigned int k=0;
do
{
Usart_SendByte( pUSARTx, *(str + k) );
k++;
} while(*(str + k)!='\0');
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
{}
}
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
uint8_t temp_h, temp_l;
temp_h = (ch&0XFF00)>>8;
temp_l = ch&0XFF;
USART_SendData(pUSARTx,temp_h);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
USART_SendData(pUSARTx,temp_l);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
int fputc(int ch, FILE *f)
{
USART_SendData(DEBUG_USARTx, (uint8_t) ch);
while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
return (ch);
}
int fgetc(FILE *f)
{
while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(DEBUG_USARTx);
}
bsp_usart.h
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
#include
#define DEBUG1_USARTx USART1
#define DEBUG1_USART_CLK RCC_APB2Periph_USART1
#define DEBUG1_USART_APBxClkCmd RCC_APB2PeriphClockCmd
#define DEBUG1_USART_BAUDRATE 115200
#define DEBUG1_USART_GPIO_CLK (RCC_APB2Periph_GPIOA)
#define DEBUG1_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
#define DEBUG1_USART_TX_GPIO_PORT GPIOA
#define DEBUG1_USART_TX_GPIO_PIN GPIO_Pin_9
#define DEBUG1_USART_RX_GPIO_PORT GPIOA
#define DEBUG1_USART_RX_GPIO_PIN GPIO_Pin_10
#define DEBUG1_USART_IRQ USART1_IRQn
#define DEBUG1_USART_IRQHandler USART1_IRQHandler
#define DEBUG_USARTx USART2
#define DEBUG_USART_CLK RCC_APB1Periph_USART2
#define DEBUG_USART_APBxClkCmd RCC_APB1PeriphClockCmd
#define DEBUG_USART_BAUDRATE 9600
#define DEBUG_USART_GPIO_CLK (RCC_APB2Periph_GPIOA)
#define DEBUG_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
#define DEBUG_USART_TX_GPIO_PORT GPIOA
#define DEBUG_USART_TX_GPIO_PIN GPIO_Pin_2
#define DEBUG_USART_RX_GPIO_PORT GPIOA
#define DEBUG_USART_RX_GPIO_PIN GPIO_Pin_3
#define DEBUG_USART_IRQ USART2_IRQn
#define DEBUG_USART_IRQHandler USART2_IRQHandler
void USART1_Config(void);
void USART2_Config(void);
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
#endif
刚刚入手stm32一个多月,学习modbus的过程中学习了很多,希望能够接触的更多好玩的东西。如果觉得本文章对你有帮助,麻烦点个赞噢