#include <mil.h>
#include "CclinkSlave.h"
const MIL_INT TO_PLC_DATA_READY_FLAG(0);
const MIL_INT TO_PLC_DATA_REGISTER(0);
const MIL_INT FROM_PLC_TRIGGER_FLAG(0);
const MIL_INT FROM_PLC_DATA_ACKNOWLEDGE_FLAG(1);
const MIL_INT FROM_PLC_DATA_REGISTER(0);
MIL_UINT16 _internalProcessingValue(0);
static MIL_INT WaitForTriggerFromPLC(CclinkSlave &slave);
static MIL_UINT16 DoProcessing(void);
static void WriteResultToPLC(CclinkSlave &slave, const MIL_UINT16 result);
int MosMain(void)
{
MIL_ID MilApplication(M_NULL);
MIL_ID MilSystem(M_NULL);
MIL_ID MilCom(M_NULL);
MIL_UINT16 processingResult(0);
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
McomAlloc(MilSystem, M_COM_PROTOCOL_CCLINK, MIL_TEXT("M_DEFAULT"), M_DEFAULT, M_DEFAULT, &MilCom);
auto slave = CclinkSlave(MilCom);
if (slave.isSlaveStopped())
{
MosPrintf(MIL_TEXT("Please start CC-Link Master Cyclic Transmission\n"));
MosPrintf(MIL_TEXT("Aborting sample code\n"));
goto abort;
}
MosPrintf(MIL_TEXT("Press <Enter> to end loop.\n"));
slave.ClearFlag(TO_PLC_DATA_READY_FLAG);
slave.WriteRegister(TO_PLC_DATA_REGISTER, 0);
while (!MosKbhit())
{
if (!WaitForTriggerFromPLC(slave))
{
processingResult = DoProcessing();
WriteResultToPLC(slave, processingResult);
}
}
slave.ClearFlag(TO_PLC_DATA_READY_FLAG);
slave.WriteRegister(TO_PLC_DATA_REGISTER, 0);
abort:
McomFree(MilCom);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
MIL_INT WaitForTriggerFromPLC(CclinkSlave &slave)
{
auto _isTriggerSet = MIL_BOOL(false);
auto _KeyHit = MIL_INT(0);
MosPrintf(MIL_TEXT("Waiting for trigger from PLC.\n"));
do
{
_isTriggerSet = slave.ReadFlag(FROM_PLC_TRIGGER_FLAG);
MosSleep(10);
_KeyHit = MosKbhit();
} while (!_isTriggerSet && !_KeyHit);
if (!_KeyHit)
MosPrintf(MIL_TEXT("Received Trigger from PLC!\n"));
return _KeyHit;
}
MIL_UINT16 DoProcessing(void)
{
MosPrintf(MIL_TEXT("Computing new data value\n"));
_internalProcessingValue++;
return _internalProcessingValue;
}
void WriteResultToPLC(CclinkSlave &slave, const MIL_UINT16 result)
{
auto _isResultRead = MIL_BOOL(false);
auto _KeyHit = MIL_INT(0);
slave.WriteRegister(TO_PLC_DATA_REGISTER, result);
slave.SetFlag(TO_PLC_DATA_READY_FLAG);
MosPrintf(MIL_TEXT("Waiting fo PLC to acknowledge the data.\n"));
do
{
_isResultRead = slave.ReadFlag(FROM_PLC_DATA_ACKNOWLEDGE_FLAG);
MosSleep(10);
_KeyHit = MosKbhit();
} while (!_isResultRead && !_KeyHit);
if (!_KeyHit)
{
auto valueFromPLC = slave.ReadRegister(FROM_PLC_DATA_REGISTER);
MosPrintf(MIL_TEXT("Received result ACK! Value:%d CopyBack:%d\n"), result, valueFromPLC);
slave.ClearFlag(TO_PLC_DATA_READY_FLAG);
}
}