#include <mil.h>
MIL_ID MilApplication,
MilSystem,
MilCom;
MIL_UINT8 ResultReady;
void WriteControl(MIL_UINT8 trigger, MIL_UINT8 resultACK, MIL_UINT8 resultCopy);
void ReadStatus(MIL_UINT8 *result);
void SetInitialControl(void);
void GenerateTrigger(void);
MIL_UINT WaitForResultFromSlave(MIL_UINT8 *result);
void WriteResultCopy(MIL_UINT8 result);
int MosMain(void)
{
MIL_UINT8 processingResult;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
McomAlloc(MilSystem, M_COM_PROTOCOL_MODBUS, MIL_TEXT("M_DEFAULT"), M_DEFAULT, M_DEFAULT, &MilCom);
MosPrintf(MIL_TEXT("Sending triggers to ModBus slave.\nPress <Enter> to end.\n"));
SetInitialControl();
while (!MosKbhit())
{
GenerateTrigger();
if (!WaitForResultFromSlave(&processingResult))
{
WriteResultCopy(processingResult);
}
}
SetInitialControl();
McomFree(MilCom);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
void WriteControl(MIL_UINT8 trigger, MIL_UINT8 resultACK, MIL_UINT8 resultCopy)
{
MIL_UINT8 control[2];
control[0] = ((resultACK & 0x01) << 1) | (trigger & 0x01);
control[1] = resultCopy;
McomWrite(MilCom, MIL_TEXT("1/") M_HOLDING_REGISTER, 0, sizeof(control) / sizeof(MIL_UINT16), control);
}
void ReadStatus(MIL_UINT8 *result)
{
MIL_UINT8 status[2];
McomRead(MilCom, MIL_TEXT("1/") M_INPUT_REGISTER, 0, sizeof(status) / sizeof(MIL_UINT16), status);
ResultReady = (status[0] & 0x2) >> 1;
*result = status[1];
}
void SetInitialControl(void)
{
WriteControl(0, 0, 0);
}
void GenerateTrigger(void)
{
MosSleep(1000);
WriteControl(1, 0, 0);
MosPrintf(MIL_TEXT("Send Trigger!\n"));
}
MIL_UINT WaitForResultFromSlave(MIL_UINT8 *result)
{
MIL_INT KeyHit;
MIL_UINT8 ResultValue = 0;
do
{
ReadStatus(&ResultValue);
MosSleep(10);
KeyHit = MosKbhit();
} while (ResultReady == 0 && !KeyHit);
if (!KeyHit)
{
MosPrintf(MIL_TEXT("Received result ready! Value:%d\n"), ResultValue);
WriteControl(0, 1, ResultValue);
}
*result = ResultValue;
return KeyHit;
}
void WriteResultCopy(MIL_UINT8 result)
{
WriteControl(0, 1, result);
}