.

     

MPI Application Template
template.c
 

SQNodeEvent1.c -- SynqNet Node Events
/* SQNodeEvent1.c */

/* Copyright(c) 1991-2006 by Motion Engineering, Inc.  All rights reserved.
 *
 * This software  contains proprietary and  confidential information  of
 * Motion Engineering Inc., and its suppliers.  Except as may be set forth
 * in the license agreement under which  this software is supplied, use,
 * disclosure, or  reproduction is prohibited without the prior express
 * written consent of Motion Engineering, Inc.
 */

/*

:Display SynqNet Node Events

This is a simple program to demonstrate how to use the EventMgr to catch
 SynqNet node events, read the event message and display the event's information.

Warning!  This is a sample program to assist in the integration of an
 MEI motion controller with your application.  It may not contain all 
 of the logic and safety features that your application requires.
 
The msgCHECK(...) macros used in the following sample code are intended
 to convey our strong belief that ALL error return codes should be checked.
 Actual application code should use specific error handling techniques (other
 than msgCHECKs) best suited to your internal error recovery methods.

*/

#include <stdlib.h>
#include <stdio.h>

#include "stdmpi.h"
#include "stdmei.h"

#include "apputil.h"

#define WAIT_TIME       (200)   /* msec */


/* Parse the command line */
long parseCommandLine(int                 argc,
                      char               *argv[],
                      long               *synqNetNumber,
                      MPIControlType     *controlType,
                      MPIControlAddress  *controlAddress)
{
    long    argIndex;

    /* Command line arguments and defaults */
    Arg argList[] =
    {
        {   "-synqNet", ArgTypeLONG,    &synqNetNumber, },
        {   NULL,       ArgTypeINVALID, NULL,           }
    };


    /* Parse command line for Control type and address */
    argIndex =
        argControl(argc,
        argv,
        controlType,
        controlAddress);

    /* Parse command line for application-specific arguments */
    while (argIndex < argc) {
        long    argIndexNew;

        argIndexNew = argSet(argList, argIndex, argc, argv);

        if (argIndexNew <= argIndex) {
            argIndex = argIndexNew;
            break;
        }
        else {
            argIndex = argIndexNew;
        }
    }

    /* Check for unknown/invalid command line arguments */
    if (argIndex < argc) {
        fprintf(stderr,"usage: %s %s\n"
            "\t\t[-synqNet # (0 .. %d)]\n",
            argv[0],
            ArgUSAGE,
            MEIXmpMaxSynqNets - 1);
        exit(MPIMessageARG_INVALID);
    }

    return 0;
}


/* Create and initialize MPI objects */
void programInit(MPIControl        *control,
                 MPIControlType     controlType,
                 MPIControlAddress *controlAddress,
                 MEISynqNet        *synqNet,
                 long               synqNetNumber,
             MEISqNode        *sqNode,
             long          *nodeCount,
                 MPINotify         *notify,
                 MPIEventMgr       *eventMgr,
                 Service           *service)
{
    MPIEventMask    eventMask;
   MEISynqNetInfo netInfo;
   long        index;
    long            returnValue;

    /* Create motion controller object */
    *control =
        mpiControlCreate(controlType,
                         controlAddress);
    msgCHECK(mpiControlValidate(*control));

    /* Initialize motion controller */
    returnValue =
        mpiControlInit(*control);
    msgCHECK(returnValue);

    /* Create SynqNet object */
    *synqNet =
        meiSynqNetCreate(*control,
                         synqNetNumber);
    msgCHECK(meiSynqNetValidate(*synqNet));

   /* Read SynqNet Info */
   returnValue =
      meiSynqNetInfo(*synqNet, &netInfo);
   msgCHECK(returnValue);

   *nodeCount = netInfo.nodeCount;

   /* Create SqNode objects */
   if (netInfo.nodeCount) {
      for (index = netInfo.nodeOffset;
          index < netInfo.nodeOffset + netInfo.nodeCount;
          index++) {

         sqNode[index] =
            meiSqNodeCreate(meiSynqNetControl(*synqNet), index);
         msgCHECK(meiSqNodeValidate(sqNode[index]));
      }
   }

    /* Request notification of all events from SqNodes */
   if (netInfo.nodeCount) {
      for (index = netInfo.nodeOffset;
          index < netInfo.nodeOffset + netInfo.nodeCount;
          index++) {

         mpiEventMaskCLEAR(eventMask);
         returnValue =
            meiSqNodeEventNotifyGet(sqNode[index],
                              &eventMask,
                              NULL);
         msgCHECK(returnValue);

         meiEventMaskSQNODE(eventMask);

         returnValue =
            meiSqNodeEventNotifySet(sqNode[index],
                              eventMask,
                              NULL);
         msgCHECK(returnValue);
      }
   }

    /* Create event notification object for NULL (all objects) */
    *notify =
        mpiNotifyCreate(eventMask,
                        NULL);
    msgCHECK(mpiNotifyValidate(*notify));

    /* Create event manager object */
    *eventMgr = mpiEventMgrCreate(*control);
    msgCHECK(mpiEventMgrValidate(*eventMgr));

    /* Append notify to event manager */
    returnValue =
        mpiEventMgrNotifyAppend(*eventMgr,
                                *notify);
    msgCHECK(returnValue);

    /* Create service thread */
    *service =
        serviceCreate(*eventMgr,
                      -1,   /* default (max) priority */
                      -1);  /* -1 => enable interrupts */
    if (*service == NULL) {
        msgCHECK(MPIMessageHANDLE_INVALID);
    }
}


/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl  *control,
                    MEISynqNet  *synqNet,
               MEISqNode   *sqNode,
               long     nodeCount,
                    MPINotify   *notify,
                    MPIEventMgr *eventMgr,
                    Service     *service)
{
   long index;
    long returnValue;

    /* Delete service thread */
    returnValue =
        serviceDelete(*service);
    msgCHECK(returnValue);

    *service = MPIHandleVOID;

    /* Delete event manager object */
    returnValue =
        mpiEventMgrDelete(*eventMgr);
    msgCHECK(returnValue);

    *eventMgr = MPIHandleVOID;

    /* Delete event notification object */
    returnValue =
        mpiNotifyDelete(*notify);
    msgCHECK(returnValue);

    *notify = MPIHandleVOID;

   /* Delete SqNode objects */
   for (index = 0; index < nodeCount; index++) {
      returnValue =
         meiSqNodeDelete(sqNode[index]);
      msgCHECK(returnValue);
   }

   /* Delete SynqNet object */
    returnValue =
        meiSynqNetDelete(*synqNet);
    msgCHECK(returnValue);

    *synqNet = MPIHandleVOID;

    /* Delete control handle */
    returnValue =
        mpiControlDelete(*control);
    msgCHECK(returnValue);

    *control = MPIHandleVOID;
}


/* Display SynqNet events until the user presses a key */
void displaySynqNetEventsUntilKeyPressed(MEISynqNet   synqNet,
                               MEISqNode  *sqNode,
                               long    nodeCount,
                               MPINotify  notify)
{
   MPIEventStatus  eventStatus;
   long nodeNumber;
    long returnValue;

    printf("Waiting for SynqNet Node Events...  <Press any key to exit>\n");

    while (meiPlatformKey(MPIWaitPOLL) <= 0) {

        /* Wait for synqNet event */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               WAIT_TIME);

        if (returnValue == MPIMessageOK) {
         /* find the node number from the event */
         returnValue =
            meiSqNodeNumber(eventStatus.source,
                        &nodeNumber);
         msgCHECK(returnValue);

         printf("SynqNet Node Number: %ld, ", nodeNumber);

            switch (eventStatus.type) {
                case MEIEventTypeSQNODE_IO_ABORT: {
                    printf("Node IO Abort.\n");
                    break;
                }
                case MEIEventTypeSQNODE_NODE_DISABLE: {
                    printf("Node Disable Input.\n");
                    break;
                }
                case MEIEventTypeSQNODE_NODE_ALARM: {
                    printf("Node Alarm Output.\n");
                    break;
                }
                case MEIEventTypeSQNODE_ANALOG_POWER_FAULT: {
                    printf("Node Analog Power Fault.\n");
                    break;
                }
                case MEIEventTypeSQNODE_USER_FAULT: {
                    printf("Node User Fault.\n");
                    break;
                }
                case MEIEventTypeSQNODE_NODE_FAILURE: {
               MEISynqNetStatus synqNetStatus;

               /* Read the network status */
               returnValue =
                  meiSynqNetStatus(synqNet,
                               &synqNetStatus);
               msgCHECK(returnValue);

                    printf("Node Failure. Failed Node Mask:0x%X\n",
                           synqNetStatus.failedNodeMask[0]);    /* First 32 nodes */
                    break;
                }
                default: {
                    printf("Unexpected Event  %ld\n", eventStatus.type);
                    break;
                }
            }
        }
        else if (returnValue == MPIMessageTIMEOUT) {        /* ignore timeouts */
            returnValue = MPIMessageOK;
        }
        msgCHECK(returnValue);
    }
}


int main(int    argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MEISynqNet          synqNet;
   MEISqNode         sqNode[MEISynqNetMaxNODE_COUNT];
    MPINotify           notify;
    MPIEventMgr         eventMgr;
    Service             service;
   long           nodeCount = 0;
    long                synqNetNumber = 0;
    long                returnValue;

    /* Parse command line */
    returnValue =
        parseCommandLine(argc,
                         argv,
                         &synqNetNumber,
                         &controlType,
                         &controlAddress);
    msgCHECK(returnValue);

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &synqNet,
                synqNetNumber,
            sqNode,
            &nodeCount,
                &notify,
                &eventMgr,
                &service);

    /* Display SynqNet events until the user presses a key */
    displaySynqNetEventsUntilKeyPressed(synqNet,
                              sqNode,
                              nodeCount,
                              notify);

    /* Perform certain cleanup actions and delete MPI objects */
    programCleanup(&control,
                   &synqNet,
               sqNode,
               nodeCount,
                   &notify,
                   &eventMgr,
                   &service);

    return ((int)returnValue);
}


 
      
       Legal Notice  |  Tech Email  |  Feedback
      
Copyright ©
2001-2009 Motion Engineering