连续好几天在调试modbus,我用的是原子的战舰stm32f407的板子,里面串口的例程+modbus通信,程序写好了以后用原子提供的串口调试助手可以看到回传的数值,但是我用modbus精灵和modscan调试不通。
实在找不到错误的原因了,工程打包如下,劳驾哪位大神能给看看啊
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "modbus.h"
#include "timer.h"
extern u8 USART_RX_FLAG;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(19200);
LED_Init();
TIM3_Int_Init(182,839);
LED0=!LED0;
while(1)
{
if(USART_RX_FLAG == 0x01)
{
USART_RX_FLAG = 0x00;
 
arseRecieve();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
}
}
这个事串口的
void USART1_IRQHandler(void)
{
#ifdef OS_TICKS_PER_SEC
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
if(USART_RX_NUM < 106)
{
USART_RX_BUF[USART_RX_NUM]=USART_ReceiveData(USART1);
USART_RX_NUM++;
usDataLen = USART_RX_NUM - 2 ;
TIM_Cmd(TIM3,ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
}
else
{
USART_RX_NUM =0;
TIM_Cmd(TIM3,ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
}
}
else
{
USART_ReceiveData(USART1);
}
#ifdef OS_TICKS_PER_SEC
OSIntExit();
#endif
}
#endif
下面是定时器的
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_Cmd(TIM3, DISABLE);
if(USART_RX_NUM >= 8)
{
USART_RX_NUM = 0;
USART_RX_FLAG = 1;
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
else
{
USART_RX_NUM = 0;
}
}
}
下面是modbus数据解析回传的
void ParseRecieve(void)
{
u8 crcDataHi;
u8 crcDataLo;
u8 i=0;
if(USART_RX_BUF[0] == 0x02)
{
crcData = crc16(USART_RX_BUF,usDataLen);
crcDataLo = crcData/256;
crcDataHi = crcData%256;
if(usDataLen == 6)
{
if((crcDataHi == USART_RX_BUF[7])&&(crcDataLo == USART_RX_BUF[6]))
{
LED0=!LED0;
if(USART_RX_BUF[1] == 0x01)
{
USART_SX_BUF[0] = USART_RX_BUF[0];
USART_SX_BUF[1] = USART_RX_BUF[1];
USART_SX_BUF[2] = USART_RX_BUF[2];
USART_SX_BUF[3] = USART_RX_BUF[3];
USART_SX_BUF[4] = USART_RX_BUF[4];
USART_SX_BUF[5] = USART_RX_BUF[5];
crcData = crc16(USART_SX_BUF,usDataLen);
crcDataLo = crcData/256;
crcDataHi = crcData%256;
USART_SX_BUF[6] = crcDataLo;
USART_SX_BUF[7] = crcDataHi;
for(i = 0;i < 8;i++)
{
USART_SendData(USART1,USART_SX_BUF
);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
}
}
}
}
}
下面附件是我的工程
一周热门 更多>