STM/Modbus/Baremetal/UART_RS485_Modbus_Slave/Source/Modbus/Slave/Modbus_Slave.c
void CheckMBPDU(void)
{
	CharCount = 0;
	CharCount = TotalCharsReceived;
	TotalCharsReceived = 0;
	if (CharCount >= 4u)
	{
		//Check inbound frame CRC
		unsigned int crcvalue = CRC16(data_in, (CharCount - 2));

		if ((data_in[CharCount - 2] == (unsigned char) (crcvalue)) &      //lower byte at higher register
			(data_in[CharCount - 1] == (unsigned char) (crcvalue >> 8)))  //higher byte at lower register
		{
			BusMsgCount += 1;	//increment bus message counter
			if ((data_in[0] == MY_SLAVE_ID) | (data_in[0] == 0u))
			{
				SlaveMsgCount += 1;                         //Increment Slave message count
				SlaveNoRspCount += data_in[0] == 0 ? 1 : 0; //STEP 2: Check function code
				switch (data_in[1])
				{
					case 0x01:
						MBProcessBitsRead(Coils, CoilsRegsize);			//read coils
					break;

					case 0x02:
						MBProcessBitsRead(DiscreteInputs, DiscreteInputRegsize);//read discrete inputs
					break;

					case 0x03:
						MBProcessRegisterRead(HoldingRegisters, HoldingRegSize);//read holding register
					break;

					case 0x04:
						MBProcessRegisterRead(InputRegisters, InputRegSize);//read input register
					break;

					case 0x05:
						MBForceSingleCoil(Coils, CoilsRegsize);	//Write single coil
					break;

					case 0x06:
						MBPresetSingleRegister(HoldingRegisters, HoldingRegSize);//write single register
					break;

					case 0x08:
						MBProcessDiagnostics();
					break;

					case 0x0f:
						MBForceMultipleCoils(Coils, CoilsRegsize);//Write multiple coils
					break;

					case 0x10:
						MBPresetMultipleRegisters(HoldingRegisters, HoldingRegSize);//write multiple registers
					break;

					default:
					{
						MBException(0x01);             //Illegal function code 01
						MBSendData(ResponseFrameSize); //send data if not broadcast command
					}
					break;
				}
			}
		}
		else
		{
			BusCommErrCount += 1; //Increment bus communication error counter
		}
	}
	else
	{
		BusCommErrCount += 1; //Increment bus communication error counter
	}
}
//Starting Modbus function to check the PDU received

Starting Modbus function to check the PDU received
Starting Modbus function to check the PDU received
void CheckMBPDU(void)
{
	CharCount = 0;
	CharCount = TotalCharsReceived;
	TotalCharsReceived = 0;
	if (CharCount >= 4u)
	{
		unsigned int crcvalue = CRC16(data_in, (CharCount - 2));
		if ((data_in[CharCount - 2] == (unsigned char) (crcvalue)) &      
			(data_in[CharCount - 1] == (unsigned char) (crcvalue >> 8)))  
		{
			BusMsgCount += 1;	
			if ((data_in[0] == MY_SLAVE_ID) | (data_in[0] == 0u))
			{
				SlaveMsgCount += 1;                         
				SlaveNoRspCount += data_in[0] == 0 ? 1 : 0; 
				switch (data_in[1])
				{
					case 0x01:
						MBProcessBitsRead(Coils, CoilsRegsize);			
					break;
					case 0x02:
						MBProcessBitsRead(DiscreteInputs, DiscreteInputRegsize);
					break;
					case 0x03:
						MBProcessRegisterRead(HoldingRegisters, HoldingRegSize);
					break;
					case 0x04:
						MBProcessRegisterRead(InputRegisters, InputRegSize);
					break;
					case 0x05:
						MBForceSingleCoil(Coils, CoilsRegsize);	
					break;
					case 0x06:
						MBPresetSingleRegister(HoldingRegisters, HoldingRegSize);
					break;
					case 0x08:
						MBProcessDiagnostics();
					break;
					case 0x0f:
						MBForceMultipleCoils(Coils, CoilsRegsize);
					break;
					case 0x10:
						MBPresetMultipleRegisters(HoldingRegisters, HoldingRegSize);
					break;
					default:
					{
						MBException(0x01);             
						MBSendData(ResponseFrameSize); 
					}
					break;
				}
			}
		}
		else
		{
			BusCommErrCount += 1; 
		}
	}
	else
	{
		BusCommErrCount += 1; 
	}
}
void CheckMBPDU ( void ) { CharCount = 0 ; CharCount = TotalCharsReceived ; TotalCharsReceived = 0 ; if ( CharCount >= 4u ) { unsigned int crcvalue = CRC16 ( data_in , ( CharCount - 2 ) ) ; if ( ( data_in [ CharCount - 2 ] == ( unsigned char ) ( crcvalue ) ) & ( data_in [ CharCount - 1 ] == ( unsigned char ) ( crcvalue >> 8 ) ) ) { BusMsgCount += 1 ; if ( ( data_in [ 0 ] == MY_SLAVE_ID ) | ( data_in [ 0 ] == 0u ) ) { SlaveMsgCount += 1 ; SlaveNoRspCount += data_in [ 0 ] == 0 ? 1 : 0 ; switch ( data_in [ 1 ] ) { case 0x01 : MBProcessBitsRead ( Coils , CoilsRegsize ) ; break ; case 0x02 : MBProcessBitsRead ( DiscreteInputs , DiscreteInputRegsize ) ; break ; case 0x03 : MBProcessRegisterRead ( HoldingRegisters , HoldingRegSize ) ; break ; case 0x04 : MBProcessRegisterRead ( InputRegisters , InputRegSize ) ; break ; case 0x05 : MBForceSingleCoil ( Coils , CoilsRegsize ) ; break ; case 0x06 : MBPresetSingleRegister ( HoldingRegisters , HoldingRegSize ) ; break ; case 0x08 : MBProcessDiagnostics ( ) ; break ; case 0x0f : MBForceMultipleCoils ( Coils , CoilsRegsize ) ; break ; case 0x10 : MBPresetMultipleRegisters ( HoldingRegisters , HoldingRegSize ) ; break ; default : { MBException ( 0x01 ) ; MBSendData ( ResponseFrameSize ) ; } break ; } } } else { BusCommErrCount += 1 ; } } else { BusCommErrCount += 1 ; } }
Starting Modbus function to check the PDU received
Starting Modbus function to check the PDU received
//Check inbound frame CRC
 //lower byte at higher register
 //higher byte at lower register
 //increment bus message counter
 //Increment Slave message count
 //STEP 2: Check function code
 //read coils
 //read discrete inputs
 //read holding register
 //read input register
 //Write single coil
 //write single register
 //Write multiple coils
 //write multiple registers
 //Illegal function code 01
 //send data if not broadcast command
 //Increment bus communication error counter
 //Increment bus communication error counter
