STM/Modbus/Baremetal/UART_RS485_Modbus_Slave/Source/Modbus/Slave/Modbus_Slave.c
void MBPresetMultipleRegisters(unsigned int *InArr, unsigned int InArrSize)
{

	//Get to know the starting address of the requested data
	unsigned int StAddress = MBStartAddress();
	unsigned int RegCount = MBRegisterCount();
	unsigned int EndAddress = StAddress + RegCount - 1;

	//| 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 <= 100u) & (data_in[6] == RegCount * 2))//for fc16, number of bytes requested is embedded in modbus message
	{
		//We will simply check if the end address is inside the size of our holding register
		if ((StAddress >= 0) & (EndAddress <= (InArrSize - 1u)))
		{
			//Process the request
			WriteMBRegistertoData(StAddress, RegCount, data_in,
					HoldingRegisters);
			ResponseFrameSize = CharCount;
		}
		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
}
//C function to Write Multiple Analog registers (Function code 16)

C function to Write Multiple Analog registers (Function code 16)
C function to Write Multiple Analog registers ( Function code 16 )
void MBPresetMultipleRegisters(unsigned int *InArr, unsigned int InArrSize)
{
	unsigned int StAddress = MBStartAddress();
	unsigned int RegCount = MBRegisterCount();
	unsigned int EndAddress = StAddress + RegCount - 1;
	if ((RegCount >= 1u) & (RegCount <= 100u) & (data_in[6] == RegCount * 2))
	{
		if ((StAddress >= 0) & (EndAddress <= (InArrSize - 1u)))
		{
			WriteMBRegistertoData(StAddress, RegCount, data_in,
					HoldingRegisters);
			ResponseFrameSize = CharCount;
		}
		else
			MBException(0x02);	
	}
	else
		MBException(0x03);	
	MBSendData(ResponseFrameSize);		
}
void MBPresetMultipleRegisters ( unsigned int * InArr , unsigned int InArrSize ) { unsigned int StAddress = MBStartAddress ( ) ; unsigned int RegCount = MBRegisterCount ( ) ; unsigned int EndAddress = StAddress + RegCount - 1 ; if ( ( RegCount >= 1u ) & ( RegCount <= 100u ) & ( data_in [ 6 ] == RegCount * 2 ) ) { if ( ( StAddress >= 0 ) & ( EndAddress <= ( InArrSize - 1u ) ) ) { WriteMBRegistertoData ( StAddress , RegCount , data_in , HoldingRegisters ) ; ResponseFrameSize = CharCount ; } else MBException ( 0x02 ) ; } else MBException ( 0x03 ) ; MBSendData ( ResponseFrameSize ) ; }
C function to Write Multiple Analog registers (Function code 16)
C function to Write Multiple Analog registers ( Function code 16 )
//Get to know the starting address of the requested data
 //| 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)
 //for fc16, number of bytes requested is embedded in modbus message
 //We will simply check if the end address is inside the size of our holding register
 //Process the request
 //Exception code 02 = ILLEGAL DATA ADDRESS
 //Exception code 03 = ILLEGAL DATA VALUE
 //send response if not broadcast message
