STM/Modbus/Baremetal/UART_RS485_Modbus_Slave/Source/Modbus/Slave/Modbus_Slave.c
void MBForceMultipleCoils(unsigned char *InArr, unsigned int InArrSize)
{
	//First check what is the count of bits requested
	unsigned int BitCount = MBRegisterCount();
	unsigned int StAddress = MBStartAddress();

	//As a safeguard we are also checking with maximum limits of query as per modbus function (m584 controller)
	unsigned char ByteCount = (
			(BitCount <= 8u) ?
					1u :
					((BitCount % 8u == 0) ?
							(BitCount / 8u) : ((BitCount / 8u) + 1u)));
	//| SLAVE_ID | FUNCTION_CODE | RETURN BYTES COUNT | DATA      | CRC     |
	//| 1 BYTE   |  1 BYTE       |  1 BYTE            | N BYTES   | 2 BYTES |
	if ((BitCount >= 1u) & (BitCount <= 800u) & (ByteCount == data_in[6]))
	{
		//to check if the requested start and end addresses are available in out controller
		//As an example we configure 50 holding registers = 100 bytes of data array HoldingRegister
		//Get to know the starting address of the requested data
		//start coil address
		unsigned int EndAddress = StAddress + BitCount - 1;	//end coil address

		if ((StAddress >= 0) & (EndAddress <= ((InArrSize * 8) - 1)))
		{
			//Process the requests
			WriteMBRegistersToBits(StAddress, BitCount, data_in, InArr);
			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 for writing multiple coils (Function code 15)

C function for writing multiple coils (Function code 15)
C function for writing multiple coils ( Function code 15 )
void MBForceMultipleCoils(unsigned char *InArr, unsigned int InArrSize)
{
	unsigned int BitCount = MBRegisterCount();
	unsigned int StAddress = MBStartAddress();
	unsigned char ByteCount = (
			(BitCount <= 8u) ?
					1u :
					((BitCount % 8u == 0) ?
							(BitCount / 8u) : ((BitCount / 8u) + 1u)));
	if ((BitCount >= 1u) & (BitCount <= 800u) & (ByteCount == data_in[6]))
	{
		unsigned int EndAddress = StAddress + BitCount - 1;	
		if ((StAddress >= 0) & (EndAddress <= ((InArrSize * 8) - 1)))
		{
			WriteMBRegistersToBits(StAddress, BitCount, data_in, InArr);
			ResponseFrameSize = CharCount;
		}
		else
			MBException(0x02);	
	}
	else
		MBException(0x03);	
	MBSendData(ResponseFrameSize);		
}
void MBForceMultipleCoils ( unsigned char * InArr , unsigned int InArrSize ) { unsigned int BitCount = MBRegisterCount ( ) ; unsigned int StAddress = MBStartAddress ( ) ; unsigned char ByteCount = ( ( BitCount <= 8u ) ? 1u : ( ( BitCount % 8u == 0 ) ? ( BitCount / 8u ) : ( ( BitCount / 8u ) + 1u ) ) ) ; if ( ( BitCount >= 1u ) & ( BitCount <= 800u ) & ( ByteCount == data_in [ 6 ] ) ) { unsigned int EndAddress = StAddress + BitCount - 1 ; if ( ( StAddress >= 0 ) & ( EndAddress <= ( ( InArrSize * 8 ) - 1 ) ) ) { WriteMBRegistersToBits ( StAddress , BitCount , data_in , InArr ) ; ResponseFrameSize = CharCount ; } else MBException ( 0x02 ) ; } else MBException ( 0x03 ) ; MBSendData ( ResponseFrameSize ) ; }
C function for writing multiple coils (Function code 15)
C function for writing multiple coils ( Function code 15 )
//First check what is the count of bits requested
 //As a safeguard we are also checking with maximum limits of query as per modbus function (m584 controller)
 //| SLAVE_ID | FUNCTION_CODE | RETURN BYTES COUNT | DATA      | CRC     |
 //| 1 BYTE   |  1 BYTE       |  1 BYTE            | N BYTES   | 2 BYTES |
 //to check if the requested start and end addresses are available in out controller
 //As an example we configure 50 holding registers = 100 bytes of data array HoldingRegister
 //Get to know the starting address of the requested data
 //start coil address
 //end coil address
 //Process the requests
 //Exception code 02 = ILLEGAL DATA ADDRESS
 //Exception code 03 = ILLEGAL DATA VALUE
 //send response if not broadcast message
