STM/Modbus/Baremetal/UART_RS485_Modbus_Slave/Source/Modbus/Slave/Modbus_Slave.c
void MBProcessBitsRead(unsigned char *InArr, unsigned int InArrSize)
{
	//First check what is the count of bits requested
	unsigned int BitCount = MBRegisterCount();
	//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 <= 2000u))
	{
		//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
		unsigned int StAddress = MBStartAddress();			//start coil address
		unsigned int EndAddress = StAddress + BitCount - 1;	//end coil address

		if (EndAddress <= ((InArrSize * 8) - 1))
		{
			//Process the request
			data_in[2] = (unsigned char) ByteCount;	//fill the byte count in the data array

			//Ex. if master request 4 coil statuses, this means that 1 register is response.
			//We need to clear the remaining 4 bits of the data if there is any.
			//Else there will be error
			unsigned char regtoclear = (
					(BitCount < 8u) ?
							3u :
							((BitCount % 8u > 0) ?
									((unsigned char) (BitCount / 8u + 3u)) : 0));
			//clearing last byte of response array
			if (regtoclear > 0)
				data_in[(unsigned char) regtoclear] = 0x00;

			AppendBitsToRegisters(StAddress, BitCount, InArr, data_in);
			AppendCRCtoMBRegister(3 + ByteCount);
		}
		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 Discrete Read Operations*************
//C function related to Discrete Read operations (Function code 01, 02)

Modbus Discrete Read Operations
C function related to Discrete Read operations (Function code 01, 02)
Modbus Discrete Read Operations C function related to Discrete Read operations ( Function code 01 02 )
void MBProcessBitsRead(unsigned char *InArr, unsigned int InArrSize)
{
	unsigned int BitCount = MBRegisterCount();
	unsigned char ByteCount = (
			(BitCount <= 8u) ?
					1u :
					((BitCount % 8u == 0) ?
							(BitCount / 8u) : ((BitCount / 8u) + 1u)));
	if ((BitCount >= 1u) & (BitCount <= 2000u))
	{
		unsigned int StAddress = MBStartAddress();			
		unsigned int EndAddress = StAddress + BitCount - 1;	
		if (EndAddress <= ((InArrSize * 8) - 1))
		{
			data_in[2] = (unsigned char) ByteCount;	
			unsigned char regtoclear = (
					(BitCount < 8u) ?
							3u :
							((BitCount % 8u > 0) ?
									((unsigned char) (BitCount / 8u + 3u)) : 0));
			if (regtoclear > 0)
				data_in[(unsigned char) regtoclear] = 0x00;
			AppendBitsToRegisters(StAddress, BitCount, InArr, data_in);
			AppendCRCtoMBRegister(3 + ByteCount);
		}
		else
			MBException(0x02);		
	}
	else
		MBException(0x03);			
	MBSendData(ResponseFrameSize);		
}
void MBProcessBitsRead ( unsigned char * InArr , unsigned int InArrSize ) { unsigned int BitCount = MBRegisterCount ( ) ; unsigned char ByteCount = ( ( BitCount <= 8u ) ? 1u : ( ( BitCount % 8u == 0 ) ? ( BitCount / 8u ) : ( ( BitCount / 8u ) + 1u ) ) ) ; if ( ( BitCount >= 1u ) & ( BitCount <= 2000u ) ) { unsigned int StAddress = MBStartAddress ( ) ; unsigned int EndAddress = StAddress + BitCount - 1 ; if ( EndAddress <= ( ( InArrSize * 8 ) - 1 ) ) { data_in [ 2 ] = ( unsigned char ) ByteCount ; unsigned char regtoclear = ( ( BitCount < 8u ) ? 3u : ( ( BitCount % 8u > 0 ) ? ( ( unsigned char ) ( BitCount / 8u + 3u ) ) : 0 ) ) ; if ( regtoclear > 0 ) data_in [ ( unsigned char ) regtoclear ] = 0x00 ; AppendBitsToRegisters ( StAddress , BitCount , InArr , data_in ) ; AppendCRCtoMBRegister ( 3 + ByteCount ) ; } else MBException ( 0x02 ) ; } else MBException ( 0x03 ) ; MBSendData ( ResponseFrameSize ) ; }
Modbus Discrete Read Operations
C function related to Discrete Read operations (Function code 01, 02)
Modbus Discrete Read Operations C function related to Discrete Read operations ( Function code 01 02 )
//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 request
 //fill the byte count in the data array
 //Ex. if master request 4 coil statuses, this means that 1 register is response.
 //We need to clear the remaining 4 bits of the data if there is any.
 //Else there will be error
 //clearing last byte of response array
 //Exception code 02 = ILLEGAL DATA ADDRESS
 //Exception code 03 = ILLEGAL DATA VALUE
 //send response if not broadcast message
