STM32-modbus rtu 之主机程序
考虑到modbus的使用场合大多为半双工而非全双工,所以,串口接收采用DMA+空闲中断,发送则直接发送。
/*serial.c*/
#include "serial.h"
#include "string.h"
_serialbuf_st serialRXbuf_st;
_serialbuf_st serialTXbuf_st;
/*DMA接收数据缓存*/
u8 g_uart1DmaRXBuf[UART_DMARX_SIZE];
/*
说明:3个串口直接发送函数
编写:林
*/
void myUSART_Sendbyte(USART_TypeDef* USARTx, uint16_t Data)
{
while((USARTx->SR&0X40)==0);
USARTx->DR = (Data & (uint16_t)0x01FF);
}
void myUSART_Sendstr(USART_TypeDef* USARTx, const char *s)
{
while(*s != '\0')
{
myUSART_Sendbyte( USARTx, *s) ;
s++;
}
}
void myUSART_Sendarr(USART_TypeDef* USARTx, u8 a[] ,uint8_t len)
{
uint8_t i=0;
while(i < len )
{
myUSART_Sendbyte( USARTx, a[i]) ;
i++;
}
}
/*
说明:
串口1初始化
串口1使用DMA 接收
编写:林
*/
void Usart1_init(u32 baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate =baud;//一秒发送BaudRate个bit
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(USART1, &USART_InitStructure);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(USART1->DR));
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)g_uart1DmaRXBuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = UART_DMARX_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
USART_ITConfig(USART1,USART_IT_TC,DISABLE);
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //
NVIC_Init(&NVIC_InitStructure);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
USART_Cmd(USART1, ENABLE);
DMA_Cmd(DMA1_Channel5,ENABLE);
memset( & serialRXbuf_st ,0, sizeof ( serialRXbuf_st ) ) ;
}
/*
等待发送完成
*/
void WaitForTransmitComplete(USART_TypeDef* USARTx)
{
while((USARTx->SR&0X40)==0){};
}
/*
说明:串口中断,DMA与空闲中断处理,用于串口接收
编写:林
*/
void USART1_IRQHandler(void)
{
_serialbuf_st *p= &serialRXbuf_st;
__IO u8 temp = 0;
u8 i=0;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
temp = USART1->SR;
temp = USART1->DR;
DMA_Cmd(DMA1_Channel5,DISABLE);
temp = UART_DMARX_SIZE - ((uint16_t)(DMA1_Channel5->CNDTR));
for (i = 0;i < temp;i++)
{
p->buf[i] =g_uart1DmaRXBuf[i];
}
p->len = temp ;
DMA_SetCurrDataCounter(DMA1_Channel5,UART_DMARX_SIZE);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
__nop();
}
/*serial.h*/
#ifndef __SERIALx_H
#define __SERIALx_H
#include "stm32f10x.h"
/*DMA接收数据缓存大小*/
#define UART_DMARX_SIZE 0xff
typedef struct
{
u8 buf[UART_DMARX_SIZE];
__IO u8 len;
} _serialbuf_st ; //串口数据结构
typedef struct
{
u8 addr;//从机地址
u8 start;//寄存器起始
u8 len; //接收到或待发送的寄存器数
u16 buf[UART_DMARX_SIZE/2];//寄存器数据
} _mbdata_st; //用户数据
extern _serialbuf_st serialRXbuf_st;
extern _serialbuf_st serialTXbuf_st;
void Usart1_init(u32 baud) ;
void WaitForTransmitComplete(USART_TypeDef* USARTx) ;
void myUSART_Sendbyte(USART_TypeDef* USARTx, uint16_t Data) ;
void myUSART_Sendstr(USART_TypeDef* USARTx, const char *s) ;
void myUSART_Sendarr(USART_TypeDef* USARTx, u8 a[] ,uint8_t len);
#endif
首先实现发送函数
/*
说明:
接收“读保持寄存器”的结果
命令0X03
返回:
res_OK 正确
res_ERR1 其他错误
res_ERR2 地址不符
res_ERR3 无反馈
*/
u8 mb_recv_readHoldingReg( _mbdata_st *mbp)
{
u8 i;
if( serialRXbuf_st.len == 0 ) return res_ERR3;
serialRXbuf_st.len=0;
if( mbp->addr == serialRXbuf_st.buf[0] )
{
if( 0x03 == serialRXbuf_st.buf[1] )
{
for(i=0;ibuf[mbp->start +i]= (u16)(serialRXbuf_st.buf[i*2+3]>>8) + serialRXbuf_st.buf[i*2+4];
}
mbp->len = serialRXbuf_st.buf [2]/2;//寄存器数
return res_OK;
}
else
{
return res_ERR1;
}
}
return res_ERR2;
}
/*
发送"写保持寄存器",命令0X10
*/
void mb_sent_writeHoldingReg( const _mbdata_st mbp)
{
u8 i=0;
u16 temp;
u8 len = mbp.len;
if(len>0x7d)len=0x7d;
serialTXbuf_st.buf[0] = mbp.addr;
serialTXbuf_st.buf[1] = 0x10;
serialTXbuf_st.buf[2] = mbp.start>>8;
serialTXbuf_st.buf[3] = mbp.start;
serialTXbuf_st.buf[4] = 0;
serialTXbuf_st.buf[5] = len;
serialTXbuf_st.buf[6] = len*2;
for(;i>8;
serialTXbuf_st.buf[i*2+8] = mbp.buf[i];
}
temp=usMBCRC16( serialTXbuf_st.buf, len*2+7 );
serialTXbuf_st.buf[ len*2+7] = temp; //低
serialTXbuf_st.buf[ len*2+8] = temp>>8;
myUSART_Sendarr( USART1, serialTXbuf_st.buf , len*2+9) ;
WaitForTransmitComplete(USART1) ; //发送完成
}
/*
说明:
接收“写保持寄存器”的从机反馈
返回:
res_OK 正确
res_ERR1 校验错误
res_ERR2 返回格式错误
res_ERR3 无反馈
*/
u8 mb_recv_writeHoldingReg( _mbdata_st *mbp )
{
u8 i=0;
if( serialRXbuf_st.len == 0 ) return res_ERR3;
serialRXbuf_st.len=0;
for(i=0;i<6;i++)
{
if ( serialTXbuf_st.buf[i] != serialRXbuf_st.buf[i] ) return res_ERR2;
}
if( serialRXbuf_st.buf[6] + (u16)(serialRXbuf_st.buf[7]<<8) != usMBCRC16( serialRXbuf_st.buf, 6 )) return res_ERR1;
return res_OK;
}
为了方便,将上面函数统一起来
//统一发送
void smb_sentHoldingReg(const _mbdata_st mbp )
{ mb_setMODRXorTX(1);//改为发送模式
if( gmod == 1 )
mb_sent_writeHoldingReg( HoldingReg_st);
else
mb_sent_readHoldingReg( HoldingReg_st );
mb_setMODRXorTX(0);//改为接收模式
gsync=1;
}
//统一接收
u8 smb_recvHoldingReg( _mbdata_st *mbp )
{
u8 rel=0xff;
if( 1 == gsync)//发送完成
{
gsync=0;
while( serialRXbuf_st.len == 0 )
{
if(TIM3->CNT >4900) break ;//从机无响应
};//接收完成
if( gmod == 1)
{
rel=mb_recv_writeHoldingReg( &HoldingReg_st ) ;
}else
{
rel=mb_recv_readHoldingReg( &HoldingReg_st ) ;
}
}
return rel ;
}
在定时器服务里调用发送函数
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
{
if(gmod == 1) HoldingReg_st.buf [1]++ ; //累增某个保持寄存器,用于测试观察
smb_sentHoldingReg( HoldingReg_st ) ;
}
}
}
在主循环里调用接收函数
while(1)
{
rel = smb_recvHoldingReg( &HoldingReg_st ) ;
if( res_OK == rel && HoldingReg_st.buf[0] == 1 ) LED0=!LED0; //观察结果
}
F=0X03
F=0X10,(虽然显示03)
这个从机软件不能显示F=0X10也就是16,但功能是可以使用的。
不过这毕竟令人心里不爽,所以我使用nmodbus类库编写了C#上位机软件进行验证,如下图所示,可见F=16也就是0X10