STM32F407单片机上开发MODBUS RTU 多主站程序(二)

       

  STM32F407单片机上开发MODBUS RTU 多主站程序(一)

  STM32F407单片机上开发MODBUS RTU 多主站程序(二)

         前面一篇文章 STM32F407单片机上开发MODBUS RTU 多主站程序(一),实现了MODBUS RTU 主站的底层代码,现在介绍如何编写MODBUS RTU 主站轮巡和解析MODBUS 从站程序。

          由于STM32系列单片机可以用DMA方式接收串口通信,因此本文的串口通信用DMA实现。

          一、主站轮巡超时定时器

                  由于主站发送完请求报文后,需要检测从站是否在规定的时间应答。

                  1、如果从站应答,就清除超时定时器的计时值同时让超时定时器停止,好主站下次开始发送请求报文时,重新开启超时定时器。

                  2、 如果从站未应答,则一直等待,直到超时定时器溢出,当超时定时器溢出,就能证明这个从站不在线,返回超时标识。

                  TIM1定时器作为超时定时器,超时时间为100毫秒。


void TIM1_Start(void)
{
	TIM_Cmd(TIM1, DISABLE);	
	TIM_SetCounter(TIM1,0x0000);
	TIM_Cmd(TIM1, ENABLE);
}



void TIM1_Stop(void)
{
	TIM_Cmd(TIM1, DISABLE);
	TIM_SetCounter(TIM1,0x0000);
}



//超时定时器超时时间=100毫秒
void TIM1_Configuration(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;


	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
    TIM_DeInit(TIM1);
 	TIM_InternalClockConfig(TIM1);
    TIM_TimeBaseStructure.TIM_Period = 10000 - 1;

	TIM_TimeBaseStructure.TIM_Prescaler = (1679);  //100毫秒
    TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
	TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM1, DISABLE);
}


void TIM1_UP_TIM10_IRQHandler(void)
{
		
	if (TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET)
	{
		TIM_ClearITPendingBit(TIM1,TIM_IT_Update);		
		TIM_Cmd(TIM1, DISABLE);
		if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
			MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_TIME_OUT;
	}
}

        二、DMA配置


#define USART1_DMA_RX_BUFFER_MAX_LENGTH  255
#define USART1_DMA_TX_BUFFER_MAX_LENGTH  255

uint8_t USART1_DMA_RX_Buffer[USART1_DMA_RX_BUFFER_MAX_LENGTH];
uint8_t USART1_DMA_TX_Buffer[USART1_DMA_TX_BUFFER_MAX_LENGTH];




void USART1_DMA_Tx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);					//DMA2时钟使能
	DMA_DeInit(DMA2_Stream7);
	while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);						//等待DMA可配置
	DMA_InitStructure.DMA_Channel = DMA_Channel_4; 							//DMA通道配置
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;		//DMA外设地址
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_DMA_TX_Buffer;//发送缓存指针
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;					//DMA传输方向:内存--->外设
	DMA_InitStructure.DMA_BufferSize = USART1_DMA_TX_BUFFER_MAX_LENGTH;		//数据传输字节数量
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;		//外设非增量模式
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;					//存储器增量模式
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;	//外设数据长度:8位
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;			//存储器数据长度:8位
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;							//使用普通模式 
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;					//中等优先级 DMA_Priority_High
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;				//存储器突发单次传输
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;		//外设突发单次传输
	DMA_Init(DMA2_Stream7, &DMA_InitStructure);								//初始化DMA Stream
	DMA_Cmd(DMA2_Stream7, DISABLE); 										//开启DMA传输
}



void USART1_DMA_Rx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;

	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);					//DMA2时钟使能
	DMA_DeInit(DMA2_Stream5);
	while (DMA_GetCmdStatus(DMA2_Stream5) != DISABLE);						//等待DMA可配置  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;  						//通道选择
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;		//DMA外设地址
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_DMA_RX_Buffer;//接收缓存指针
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;				//DMA传输方向:外设到存储器模式:外设--->内存
	DMA_InitStructure.DMA_BufferSize = USART1_DMA_RX_BUFFER_MAX_LENGTH;		//缓冲大小 
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;		//外设非增量模式
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;					//存储器增量模式
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;	//外设数据长度:8位
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;			//存储器数据长度:8位
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;							//使用普通模式 
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;					//中等优先级 DMA_Priority_VeryHigh
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;				//存储器突发单次传输
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;		//外设突发单次传输
	DMA_Init(DMA2_Stream5 , &DMA_InitStructure);							//初始化DMA_Stream5	
	DMA_Cmd(DMA2_Stream5, ENABLE);  										//开启DMA传输
}




void USART1_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendCount)
{		
	if (nSendCount < USART1_DMA_TX_BUFFER_MAX_LENGTH)
	{
		memcpy(USART1_DMA_TX_Buffer , send_buffer , nSendCount);
		DMA_Cmd(DMA2_Stream7 , DISABLE);                    //关闭DMA传输
		while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);	//确保DMA可以被设置
		DMA_SetCurrDataCounter(DMA2_Stream7 , nSendCount);  //数据传输量
		DMA_Cmd(DMA2_Stream7 , ENABLE);               		//开启DMA传输
	}
}

三、USART1串口配置

void USART1_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	USART_DeInit(USART1);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);		
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	USART_InitStructure.USART_BaudRate = 9600;
	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);
	USART_Cmd(USART1, ENABLE);

	USART_ClearFlag(USART1, USART_FLAG_TC);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
	USART_ClearFlag(USART1, USART_FLAG_TC);

    USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
	USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
	USART_ITConfig(USART1, USART_IT_TC, ENABLE);	
	USART_DMACmd(USART1 ,   USART_DMAReq_Tx,ENABLE);   //使能DMA发送
	USART_DMACmd(USART1 ,   USART_DMAReq_Rx,ENABLE);    //    使能DMA接收
}



void USART1_Begin_Send(void)
{
	USART1_DMA_Begin_Send(MBCOM1.send_buffer , MBCOM1.sendBytes);
	
}

四、DMA接收串口不定长报文以及DMA方式发送报文


void USART1_IRQHandler(void)
{
	uint16_t ch;

	
	if (USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)
	{		
		USART_ClearITPendingBit(USART1 , USART_IT_IDLE);	//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		ch =  USART_ReceiveData(USART1);					//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。	
		
		DMA_Cmd(DMA2_Stream5 , DISABLE);
		DMA_ClearFlag(DMA2_Stream5 , DMA_FLAG_TCIF5 | DMA_FLAG_FEIF5 | DMA_FLAG_DMEIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5);
		ch = USART1_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA2_Stream5);
		if (ch > 0)
		{
			if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
			{
				MBCOM1.Outtime_mark = TRUE;
				MBCOM1.receCount = ch;
				memcpy(MBCOM1.mscomm_buffer , USART1_DMA_RX_Buffer , MBCOM1.receCount);
			}
		}
		DMA_SetCurrDataCounter(DMA2_Stream5 , USART1_DMA_RX_BUFFER_MAX_LENGTH);
		DMA_Cmd(DMA2_Stream5, ENABLE);
	}
	
	else if (USART_GetITStatus(USART1,USART_IT_TC)!= RESET) 
	{
		USART_ClearITPendingBit(USART1, USART_IT_TC);
		
		DMA_ClearFlag(DMA2_Stream7 , DMA_FLAG_TCIF7 | DMA_FLAG_FEIF7 | DMA_FLAG_DMEIF7 | DMA_FLAG_TEIF7 | DMA_FLAG_HTIF7);
		DMA_SetCurrDataCounter(DMA2_Stream7 , 0);
		
		if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_BEGINING)
		{
			MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_SEND_OVER;
			TIM1_Start();  //请求报文发送完毕,开启超时定时器TIM1

		}	
	}	
} 

四、主站轮巡发送请求报文

        使用状态机进行发送。


enum MODBUS_HOSTS_STATUS 
{	
	enum_MODBUS_STATUS_READY = 0,	
	enum_MODBUS_STATUS_SEND_BEGIN =1,
	enum_MODBUS_STATUS_SEND_BEGINING =2,	
	enum_MODBUS_STATUS_SEND_OVER =3,
	enum_MODBUS_STATUS_RECV_OVER =4,
	enum_MODBUS_STATUS_RECV_ERROR =5,
	enum_MODBUS_STATUS_TIME_OUT =6,
};

typedef struct
{
    BOOL volatile Outtime_mark;	
	uint8_t volatile MBHosts_Query_isr;
	uint16_t volatile MBHosts_Poll_Time;
    uint16_t volatile sendBytes;
    uint16_t volatile sendPosi;
    uint16_t volatile receCount;
    uint8_t send_buffer[MSCOMM_BUFFER_LENGTH];
    uint8_t mscomm_buffer[MSCOMM_BUFFER_LENGTH];
} USART_DataType;



USART_DataType MBCOM1;

MODBUS_ELEMENT MBCOM1_MBHOSTS_Node;

MODBUS_ELEMENT MBCOM1_MODBUS_ELEMENT[MBCOM1_MODBUS_QUEUE_LENGTH];
MODBUS_QUEUE_STRUCT MBCOM1_MODBUS_QUEUE_STRUCT;
MODBUS_QUEUE_STRUCT *MBCOM1_MODBUS_QUEUE;



void MBCOM1_MODBUS_QUEUE_Init(void)
{	
	uint16_t i;
	
		
	MBCOM1_MODBUS_QUEUE = &MBCOM1_MODBUS_QUEUE_STRUCT;
	MBCOM1_MODBUS_QUEUE->size = MBCOM1_MODBUS_QUEUE_LENGTH;
	MBCOM1_MODBUS_QUEUE->MODBUS_Element = MBCOM1_MODBUS_ELEMENT;
	
	for (i = 0; i < MBCOM1_MODBUS_QUEUE->size ; i++)
	{
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].Reserved = 0x00;
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].DeviceAddr = 0x00;		
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].Func = 0x00;
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].StartAddr = 0x00;		
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].RegisterAmount = 0x00;
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].ByteCount = 0x00;
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].SendBytes = 0x00;
		MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].SendCount = 0x00;
		memset(MBCOM1_MODBUS_QUEUE->MODBUS_Element[i].Data , 0x00 , MODBUS_ELEMENT_DATA_LENGTH);
	}
	MBCOM1_MODBUS_QUEUE->front = 0x00;
	MBCOM1_MODBUS_QUEUE->rear = 0x00;
}


void MBCOM1_Init(void)
{
	uint8_t i;
	
	
	MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_READY; //初始状态机	
	memset(&MBCOM1 , 0x00 , sizeof(USART_DataType));	
	memset(&MBCOM1_MBHOSTS_Node  , 0x00 , sizeof(MODBUS_ELEMENT));
}


#define MBHOSTS_MAX_REPEAT_SEND_COUNT   5    //自动重发次数=5(超时或接收错误,自动重发次数)
BOOL MBCOM1_150ms_mark = FALSE;

void MBCOM1_MODBUS_Hosts_Poll(void)
{	

	MBCOM1_MODBUS_Hosts_Analysize();

    if (MBCOM1_150ms_mark)    //150毫秒轮巡1次
    {
        MBCOM1_150ms_mark = FALSE;
	switch (MBCOM1.MBHosts_Query_isr)
	{			
		case enum_MODBUS_STATUS_READY:
			if (!MODBUS_QUEUE_Empty(MBCOM1_MODBUS_QUEUE))
			{
				MODBUS_QUEUE_Out(MBCOM1_MODBUS_QUEUE , &MBCOM1_MBHOSTS_Node);
				MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_SEND_BEGIN;
				break;
			}
            else
            {
                MBHOSTS_ReadInputRegisters( 1 ,                                                  0 , 125  , MBHOSTS_MAX_REPEAT_SEND_COUNT , MBCOM1_MODBUS_QUEUE);
			MBHOSTS_ReadHoldingRegisters( 2,  0    , 125     , MBHOSTS_MAX_REPEAT_SEND_COUNT , MBCOM1_MODBUS_QUEUE);


            }
			break;
		
		case enum_MODBUS_STATUS_SEND_BEGIN:	    
			MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_SEND_BEGINING;
			memcpy(MBCOM1.send_buffer , MBCOM1_MBHOSTS_Node.Data , MBCOM1_MBHOSTS_Node.SendBytes);
			MBCOM1.sendBytes = MBCOM1_MBHOSTS_Node.SendBytes;
			USART1_Begin_Send();
			break;		
				
		case enum_MODBUS_STATUS_RECV_OVER:
			TIM1_Stop();
			MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_READY;
			break;		
		
		case enum_MODBUS_STATUS_RECV_ERROR:
		case enum_MODBUS_STATUS_TIME_OUT:
			TIM1_Stop();
		
            //超时或错误时,自动重发  
			if (--MBCOM1_MBHOSTS_Node.SendCount > 0)
				MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_SEND_BEGIN;					
			else
				MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_READY;						
			break;						
			}//switch (MBCOM1.MBHosts_Query_isr)
			break;
    }	
    }		
}

五、主站解析从站报文

        使用状态机进行解析。


void MBCOM1_MODBUS_Hosts_Analysize(void)
{
	uint16_t i,j,k;
	uint16_t startAddr;
	uint16_t registerAmount;
	uint16_t tempData;
	uint16_t crcData;
	int16_t  bad;
	
		
	if (MBCOM1.Outtime_mark)
	{
		MBCOM1.Outtime_mark = FALSE;

		if (MBCOM1.receCount >= 5)
        {			
            switch (MBCOM1.mscomm_buffer[1])
            {
                case MODBUS_HOSTS_FUNC_ReadCoil:					
					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
					{
						tempData = MBCOM1.mscomm_buffer[2] + 5;
						if (MBCOM1.receCount == tempData)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , tempData - 2);
							if (crcData == (uint16_t)(MBCOM1.mscomm_buffer[tempData - 2] << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							{
								k = 0;
								for (i = 0 ; i < MBCOM1_MBHOSTS_Node.ByteCount ; i++)
								{
									for (j = 0 ; j < 8 ; j++)
									{
										if (MBCOM1.mscomm_buffer[3+i] & (0x01 << j))
											__NOP();//reg0x_buffer[k++] = 0x01;
										else
											__NOP();//reg0x_buffer[k++] = 0x00;
										
										if (k >= MBCOM1_MBHOSTS_Node.RegisterAmount)
											break;
									}
								}//for (i = 0 ; i < MBCOM1_MBHOSTS_Node.ByteCount ; i++)
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif	
							}//if (crcData == (uint16_t)(MBCOM1.mscomm_buffer[tempData - 2] << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
                        }//if (MBCOM1.receCount == tempData)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
                    break;
									
					
               case MODBUS_HOSTS_FUNC_ReadDiscrete:
				   	if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
					{
						tempData = MBCOM1.mscomm_buffer[2] + 5;
						if (MBCOM1.receCount == tempData)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , tempData - 2);
							if (crcData ==(((uint16_t)MBCOM1.mscomm_buffer[tempData - 2]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							{
								k = 0;
								for (i = 0 ; i < MBCOM1_MBHOSTS_Node.ByteCount ; i++)
								{
									for (j = 0 ; j < 8 ; j++)
									{
										if (MBCOM1.mscomm_buffer[3+i] & (0x01 << j))
											__NOP();//reg1x_buffer[k++] = 0x01;
										else
											__NOP();//reg1x_buffer[k++] = 0x00;

										if (k >= MBCOM1_MBHOSTS_Node.RegisterAmount)
											break;
									}
								}//for (i = 0 ; i < MBCOM1_MBHOSTS_Node.ByteCount ; i++)
								
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif									
                            }//if (crcData ==(((uint16_t)MBCOM1.mscomm_buffer[tempData - 2]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
                        }//if (MBCOM1.receCount == tempData)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
                    break;

					
                case MODBUS_HOSTS_FUNC_ReadHoldingRegisters:
					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
					{
						tempData = MBCOM1.mscomm_buffer[2] + 5;
						if (MBCOM1.receCount == tempData)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , tempData - 2);
							if (crcData ==(((uint16_t)MBCOM1.mscomm_buffer[tempData - 2]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							{
								
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif
								
									tempData = ((int16_t)MBCOM1.mscomm_buffer[3+2*4] << 0x08) + (int16_t)MBCOM1.mscomm_buffer[3+2*4+1];

								
							}//if (crcData ==(((uint16_t)MBCOM1.mscomm_buffer[tempData - 2]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == tempData)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
                    break;
					
				
                case MODBUS_HOSTS_FUNC_ReadInputRegisters:					
 					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
					{
						tempData = MBCOM1.mscomm_buffer[2] + 5;
						if (MBCOM1.receCount == tempData)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , tempData - 2);
							if (crcData ==(((uint16_t)MBCOM1.mscomm_buffer[tempData - 2]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
									tempData = ((int16_t)MBCOM1.mscomm_buffer[3+2*0] << 0x08) + (int16_t)MBCOM1.mscomm_buffer[3+2*0+1];
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif									
							}//if (crcData ==(((uint16_t)MBCOM1.mscomm_buffer[tempData - 2]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[tempData - 1])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == tempData)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func && MBCOM1.mscomm_buffer[2] == MBCOM1_MBHOSTS_Node.ByteCount)
                    break;


				case MODBUS_HOSTS_FUNC_ForceSingleCoil:					
 					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					{
						startAddr = (uint16_t)(MBCOM1.mscomm_buffer[2] << 8) + (uint16_t)MBCOM1.mscomm_buffer[3];
						if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , 6);
							if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							{								
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif									
							}//if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
                    break;
				
					
				case MODBUS_HOSTS_FUNC_PresetSingleHoldingRegister:					
  					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					{
						startAddr = (uint16_t)(MBCOM1.mscomm_buffer[2] << 8) + (uint16_t)MBCOM1.mscomm_buffer[3];
						if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , 6);
							if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							{								
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif									
							}//if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
                    break;

				
				case MODBUS_HOSTS_FUNC_ForceMultipleCoil:
  					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					{
						startAddr = (uint16_t)(MBCOM1.mscomm_buffer[2] << 8) + (uint16_t)MBCOM1.mscomm_buffer[3];
						registerAmount = (uint16_t)(MBCOM1.mscomm_buffer[4] << 8) + (uint16_t)MBCOM1.mscomm_buffer[5];						
						if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr && registerAmount == MBCOM1_MBHOSTS_Node.RegisterAmount)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , 6);
							if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							{																	
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
								
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif									
							}//if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr && registerAmount == MBCOM1_MBHOSTS_Node.RegisterAmount)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					break;
				
					
				case MODBUS_HOSTS_FUNC_PresetMultipleHoldingRegisters:
  					if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					{
						startAddr = (uint16_t)(MBCOM1.mscomm_buffer[2] << 8) + (uint16_t)MBCOM1.mscomm_buffer[3];
						registerAmount = (uint16_t)(MBCOM1.mscomm_buffer[4] << 8) + (uint16_t)MBCOM1.mscomm_buffer[5];						
						if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr && registerAmount == MBCOM1_MBHOSTS_Node.RegisterAmount)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , 6);
							if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							{								
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
																						
								#ifdef __DEBUG_stm32f407__
									__DEBUG_MBCOM1_MODBUS_Recv_Count++;
								#endif									
							}//if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[6]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[7])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == 8 && startAddr == MBCOM1_MBHOSTS_Node.StartAddr && registerAmount == MBCOM1_MBHOSTS_Node.RegisterAmount)
                    }//if (MBCOM1.mscomm_buffer[0] == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)					
					break;
				
				
				case 0x81:
				case 0x82:
				case 0x83:
				case 0x84:
				case 0x85:
				case 0x8F:
				case 0x90:					
 					if ((MBCOM1.mscomm_buffer[0] - 0x80) == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					{
						if (MBCOM1.receCount == 5)
						{
							crcData = CRC16(MBCOM1.mscomm_buffer , 3);
							if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[3]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[4])
							{								
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_OVER;
							}//if (crcData == (((uint16_t)MBCOM1.mscomm_buffer[3]) << 8) + (uint16_t)MBCOM1.mscomm_buffer[4])
							else
							{
								if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
									MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
							}
						}//if (MBCOM1.receCount == 5)
						else
						{
							if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
								MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;						
						}
					}//if ((MBCOM1.mscomm_buffer[0] - 0x80) == MBCOM1_MBHOSTS_Node.DeviceAddr && MBCOM1.mscomm_buffer[1] == MBCOM1_MBHOSTS_Node.Func)
					break;	

				default:
					if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
						MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;
					break;				
            }//switch (MBCOM1.mscomm_buffer[1])
			
			//USART_ITConfig(MBCOM1, USART_IT_RXNE, ENABLE);//DMA通信,则删除
			
        }//if (MBCOM1.receCount >= 5)
		else
		{
			if (MBCOM1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
				MBCOM1.MBHosts_Query_isr = enum_MODBUS_STATUS_RECV_ERROR;		
		}				
    }//if (MBCOM1.Outtime_mark)
}

本程序已经在很多工程项目上成功运用。

源程序参见:MODBUS_RTU_Master.zip(STM32F407单片机上开发的MODBUSRTU多主站源程序)_modbusRTU主站程序-C文档类资源-CSDN下载

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值