STM/Modbus/Baremetal/UART_RS485_Modbus_Slave/Source/Modbus/Slave/Modbus_Slave.c
void MBProcessRegisterRead(unsigned int *InArr, unsigned int InArrSize)
{
	//First check what is the count of registers requested
	unsigned int RegCount = MBRegisterCount();

	//| SLAVE_ID | FUNCTION_CODE | RETURN BYTES COUNT | DATA      | CRC     |
	//| 1 BYTE   |  1 BYTE       |  1 BYTE            | N*2 BYTES | 2 BYTES |
	//So our final requested data should fit in above 256 size, so data should be max 256-6 bytes
	//As a safeguard we are also checking with maximum limits of query as per modbus function (m584 controller)
	if ((RegCount >= 1u) & (RegCount * 2 <= 251u) & (RegCount <= 125u))
	{
		//to check if the requested start and end addresses are available in out controller
		//Get to know the starting address of the requested data
		unsigned int StAddress = MBStartAddress();
		unsigned int EndAddress = StAddress + RegCount - 1u;

		//We will simply check if the end address is inside the size of our holding register
		if ((StAddress >= 0u) & (EndAddress <= (InArrSize - 1u)))
		{
			//Process the request
			data_in[2] = (unsigned char) (RegCount * 2);//fill the byte count in the data array
			AppendDatatoMBRegister(StAddress, RegCount, HoldingRegisters,
					data_in);	//fill data in the data register
			AppendCRCtoMBRegister(3 + RegCount * 2);
		}
		else
			MBException(0x02);	//Exception code 02 = ILLEGAL DATA ADDRESS
	}
	else
		MBException(0x03);	//Exception code 03 = ILLEGAL DATA VALUE

	MBSendData(ResponseFrameSize);		//send response if not broadcast message
}
//*********Modbus Register Read Operations*************
//C function related to Analog Read operations (Function code 03, 04)

Modbus Register Read Operations
C function related to Analog Read operations (Function code 03, 04)
Modbus Register Read Operations C function related to Analog Read operations ( Function code 03 04 )
void MBProcessRegisterRead(unsigned int *InArr, unsigned int InArrSize)
{
	unsigned int RegCount = MBRegisterCount();
	if ((RegCount >= 1u) & (RegCount * 2 <= 251u) & (RegCount <= 125u))
	{
		unsigned int StAddress = MBStartAddress();
		unsigned int EndAddress = StAddress + RegCount - 1u;
		if ((StAddress >= 0u) & (EndAddress <= (InArrSize - 1u)))
		{
			data_in[2] = (unsigned char) (RegCount * 2);
			AppendDatatoMBRegister(StAddress, RegCount, HoldingRegisters,
					data_in);	
			AppendCRCtoMBRegister(3 + RegCount * 2);
		}
		else
			MBException(0x02);	
	}
	else
		MBException(0x03);	
	MBSendData(ResponseFrameSize);		
}
void MBProcessRegisterRead ( unsigned int * InArr , unsigned int InArrSize ) { unsigned int RegCount = MBRegisterCount ( ) ; if ( ( RegCount >= 1u ) & ( RegCount * 2 <= 251u ) & ( RegCount <= 125u ) ) { unsigned int StAddress = MBStartAddress ( ) ; unsigned int EndAddress = StAddress + RegCount - 1u ; if ( ( StAddress >= 0u ) & ( EndAddress <= ( InArrSize - 1u ) ) ) { data_in [ 2 ] = ( unsigned char ) ( RegCount * 2 ) ; AppendDatatoMBRegister ( StAddress , RegCount , HoldingRegisters , data_in ) ; AppendCRCtoMBRegister ( 3 + RegCount * 2 ) ; } else MBException ( 0x02 ) ; } else MBException ( 0x03 ) ; MBSendData ( ResponseFrameSize ) ; }
Modbus Register Read Operations
C function related to Analog Read operations (Function code 03, 04)
Modbus Register Read Operations C function related to Analog Read operations ( Function code 03 04 )
//First check what is the count of registers requested
 //| SLAVE_ID | FUNCTION_CODE | RETURN BYTES COUNT | DATA      | CRC     |
 //| 1 BYTE   |  1 BYTE       |  1 BYTE            | N*2 BYTES | 2 BYTES |
 //So our final requested data should fit in above 256 size, so data should be max 256-6 bytes
 //As a safeguard we are also checking with maximum limits of query as per modbus function (m584 controller)
 //to check if the requested start and end addresses are available in out controller
 //Get to know the starting address of the requested data
 //We will simply check if the end address is inside the size of our holding register
 //Process the request
 //fill the byte count in the data array
 //fill data in the data register
 //Exception code 02 = ILLEGAL DATA ADDRESS
 //Exception code 03 = ILLEGAL DATA VALUE
 //send response if not broadcast message
