STM/Modbus/Baremetal/UART_RS485_Modbus_Slave/Source/Modbus/Slave/Modbus_Slave.c
void MBForceSingleCoil(unsigned char *InArr, unsigned int InArrSize)
{
	unsigned int StAddress = MBStartAddress();
	unsigned int outputvalue = MBRegisterCount();//using existing c function instead of creating a new one
	if ((outputvalue == 0x0000) | (outputvalue == 0xff00))
	{
		if (StAddress <= ((InArrSize * 8) - 1))
		{
			//Not calling separate function. Writing here itself
			if (outputvalue == 0xff00)
			{
				*(InArr + (StAddress / 8)) |= (1u
						<< (StAddress - ((StAddress / 8) * 8)));
			}
			else
				*(InArr + (StAddress / 8)) &= ~(1u
						<< (StAddress - ((StAddress / 8) * 8)));
			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
}
//*********Modbus Discrete Write Operations*************
//C function for writing one coil (Function code 05)

Modbus Discrete Write Operations
C function for writing one coil (Function code 05)
Modbus Discrete Write Operations C function for writing one coil ( Function code 05 )
void MBForceSingleCoil(unsigned char *InArr, unsigned int InArrSize)
{
	unsigned int StAddress = MBStartAddress();
	unsigned int outputvalue = MBRegisterCount();
	if ((outputvalue == 0x0000) | (outputvalue == 0xff00))
	{
		if (StAddress <= ((InArrSize * 8) - 1))
		{
			if (outputvalue == 0xff00)
			{
				*(InArr + (StAddress / 8)) |= (1u
						<< (StAddress - ((StAddress / 8) * 8)));
			}
			else
				*(InArr + (StAddress / 8)) &= ~(1u
						<< (StAddress - ((StAddress / 8) * 8)));
			ResponseFrameSize = CharCount;
		}
		else
			MBException(0x02);		
	}
	else
		MBException(0x03);		
	MBSendData(ResponseFrameSize);		
}
void MBForceSingleCoil ( unsigned char * InArr , unsigned int InArrSize ) { unsigned int StAddress = MBStartAddress ( ) ; unsigned int outputvalue = MBRegisterCount ( ) ; if ( ( outputvalue == 0x0000 ) | ( outputvalue == 0xff00 ) ) { if ( StAddress <= ( ( InArrSize * 8 ) - 1 ) ) { if ( outputvalue == 0xff00 ) { * ( InArr + ( StAddress / 8 ) ) |= ( 1u << ( StAddress - ( ( StAddress / 8 ) * 8 ) ) ) ; } else * ( InArr + ( StAddress / 8 ) ) &= ~ ( 1u << ( StAddress - ( ( StAddress / 8 ) * 8 ) ) ) ; ResponseFrameSize = CharCount ; } else MBException ( 0x02 ) ; } else MBException ( 0x03 ) ; MBSendData ( ResponseFrameSize ) ; }
Modbus Discrete Write Operations
C function for writing one coil (Function code 05)
Modbus Discrete Write Operations C function for writing one coil ( Function code 05 )
//using existing c function instead of creating a new one
 //Not calling separate function. Writing here itself
 //Exception code 02 = ILLEGAL DATA ADDRESS
 //Exception code 03 = ILLEGAL DATA VALUE
 //send response if not broadcast message
