int main(int argc, char** argv) { pwr_tStatus sts; io_tCtx io_ctx; float ctime = 1; pwr_sClass_EplHandler* plhp; pwr_tOid oid; int tmo; char mp[2000]; qcom_sGet get; qcom_sQattr qAttr; qcom_sQid qini; qcom_sQid qid = qcom_cNQid; if (argc > 1) { if (streq(argv[1], "-m")) { io_methods_print(); exit(0); } if (streq(argv[1], "-h")) { usage(); exit(0); } } // Make connection to error handler errh_Init("pwr_powerlink", errh_eAnix_powerlink); errh_SetStatus(PWR__SRVSTARTUP); if (!qcom_Init(&sts, 0, "pwr_powerlink")) { errh_Fatal("qcom_Init, %m", sts); exit(sts); } qAttr.type = qcom_eQtype_private; qAttr.quota = 100; if (!qcom_CreateQ(&sts, &qid, &qAttr, "events")) { errh_Fatal("qcom_CreateQ, %m", sts); exit(sts); } qini = qcom_cQini; if (!qcom_Bind(&sts, &qid, &qini)) { errh_Fatal("qcom_Bind(Qini), %m", sts); exit(-1); } // Make connection to realtime database sts = gdh_Init("rt_powerlink"); if (EVEN(sts)) { errh_Fatal("rt_powerlink aborted, gdh_Init failed\n%m", sts); errh_SetStatus(PWR__SRVTERM); exit(sts); } sts = gdh_GetClassList(pwr_cClass_Epl_CNServer, &oid); if (ODD(sts)) { system("rt_powerlink_cn &"); exit(0); } // Get Powerlink handler object sts = io_get_plhandler_object(&plhp, &oid); if (EVEN(sts)) { errh_SetStatus(0); errh_Info("rt_powerlink terminated, no EplHandler object found"); exit(sts); } // Create context and call init functions of all agent, // rack and cardobjects sts = io_init(io_mProcess_Powerlink, pwr_cNObjid, &io_ctx, 1, ctime); if (EVEN(sts)) { errh_SetStatus(PWR__SRVTERM); errh_Fatal("rt_powerlink aborted, io_init() failed\n%m", sts); exit(sts); } tmo = (plhp->CycleTime) * 1000; aproc_TimeStamp(plhp->CycleTime, 5.0); errh_SetStatus(PWR__SRUN); aproc_RegisterObject(oid); // Call IoAgentRead() IoAgentWrite() IoCardRead() IoCardWrite() // IoModuleRead() IoModuleWrite() forever for (;;) { get.maxSize = sizeof(mp); get.data = mp; qcom_Get(&sts, &qid, &get, tmo); if (sts == QCOM__TMO || sts == QCOM__QEMPTY) { sts = io_read(io_ctx); sts = io_write(io_ctx); aproc_TimeStamp(plhp->CycleTime, 5.0); } else { ini_mEvent new_event; qcom_sEvent* ep = (qcom_sEvent*)get.data; new_event.m = ep->mask; if (new_event.b.oldPlcStop) { // TODO } else if (new_event.b.swapDone) { // TODO } else if (new_event.b.terminate) { // io_close(io_ctx); exit(0); } } } }
int main (int argc, char **argv) { pwr_tStatus sts = 1; io_tCtx io_ctx; io_tCtx io_ctx_swap; pwr_sClass_IOHandler *ihp; int swap_io; int close_io; int init_io; qcom_sQid qid = qcom_cNQid; int tmo; char mp[2000]; qcom_sGet get; pwr_tTime now; pwr_tTime next; pwr_tTime after; pwr_tDeltaTime cycle; lst_sEntry *csup_lh; int delay_action = 0; pwr_sNode *nodep; pwr_tBoolean old_emergency_break = 0; if ( argc > 1) { if ( strcmp( argv[1], "-m") == 0) { io_methods_print(); exit(0); } if ( strcmp( argv[1], "-h") == 0) { usage(); exit(0); } } ihp = init(&qid, &csup_lh, &nodep); plc_UtlWaitForPlc(); /* Prepare the swap context */ sts = io_init_swap(io_mProcess_IoComm, pwr_cNObjid, &io_ctx_swap, 1, ihp->CycleTimeBus); for (close_io = swap_io = 0, init_io = 1;;) { if (init_io) { double f; sts = io_init(io_mProcess_IoComm, pwr_cNObjid, &io_ctx, 1, ihp->CycleTimeBus); if ( ODD(sts)) errh_SetStatus( PWR__SRUN); #if defined(OS_ELN) ker$clear_event( &sts, io_comm_terminate); io_dioc_init(); io_dioc_start(); #endif init_io = 0; tmo = ihp->CycleTimeBus * 1000.; f = floor(ihp->CycleTimeBus); cycle.tv_sec = f; cycle.tv_nsec = (ihp->CycleTimeBus - f) * 1.0e9; cycle.tv_nsec++; time_GetTimeMonotonic(&next); time_Aadd(NULL, &next, &cycle); } get.maxSize = sizeof(mp); get.data = mp; qcom_Get(&sts,&qid, &get, tmo); if (sts == QCOM__TMO || sts == QCOM__QEMPTY) { if ( nodep->EmergBreakTrue && !old_emergency_break) sts = io_swap(io_ctx_swap, io_eEvent_IoCommEmergencyBreak); sts = io_read(io_ctx); if (EVEN(sts)) { ihp->IOReadWriteFlag = FALSE; errh_Error("IO read, %m", sts); } sts = io_write(io_ctx); if (EVEN(sts)) { ihp->IOReadWriteFlag = FALSE; errh_Error("IO write, %m", sts); } if ( nodep->EmergBreakTrue && !old_emergency_break) sts = io_swap(io_ctx, io_eEvent_EmergencyBreak); old_emergency_break = nodep->EmergBreakTrue; if (swap_io) { sts = io_swap(io_ctx_swap, io_eEvent_IoCommSwap); } io_ScanSupLst( io_ctx->SupCtx); time_GetTime(&now); time_GetTimeMonotonic(&after); next = after; time_Aadd(NULL, &next, &cycle); delay_action = csup_Exec(&sts, csup_lh, (pwr_tDeltaTime *) &next, (pwr_tDeltaTime *) &after, &now); if (delay_action == 2) ihp->IOReadWriteFlag = FALSE; aproc_TimeStamp(ihp->CycleTimeBus, 5); } else { ini_mEvent new_event; qcom_sEvent *ep = (qcom_sEvent*) get.data; new_event.m = ep->mask; if (new_event.b.oldPlcStop && !swap_io) { swap_io = 1; close_io = 1; errh_SetStatus(PWR__SRVRESTART); } else if (new_event.b.swapDone && swap_io) { swap_io = 0; init_io = 1; } else if (new_event.b.terminate) { exit(0); } if (close_io) { io_close(io_ctx); #if defined(OS_ELN) ker$signal( &sts, io_comm_terminate); #endif close_io = 0; } } } }