can1.c -- Toggle digital outputs on a CAN node
/* can1.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.
*/
/*
:Toggle digital outputs on a CAN node
This sample application describes how to access a CAN node and toggle its
digital outputs.
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 "stdmpi.h"
#include "stdmei.h"
#include "apputil.h"
/* Modify this macro to specify a node with digital outputs. */
#define OUTPUT_NODE_NUMBER (11)
/* Modify this to specify the number of digital output channels. */
#define CHANNEL_COUNT (16)
/* Modify this to specify the CAN network number */
#define CAN_NETWORK_NUMBER (0) /* Currently, only one CAN network is supported */
/* Perform basic command line parsing. (-control -server -port -trace) */
void basicParsing(int argc,
char *argv[],
MPIControlType *controlType,
MPIControlAddress *controlAddress)
{
long argIndex;
/* Parse command line for Control type and address */
argIndex = argControl(argc, argv, controlType, controlAddress);
/* Check for unknown/invalid command line arguments */
if (argIndex < argc) {
fprintf(stderr,"usage: %s %s\n", argv[0], ArgUSAGE);
exit(MPIMessageARG_INVALID);
}
}
/* Create and initialize MPI objects */
void programInit(MPIControl *control,
MPIControlType controlType,
MPIControlAddress *controlAddress,
MPICan *can,
long canNumber)
{
long returnValue;
/* Obtain a control handle */
*control =
mpiControlCreate(controlType, controlAddress);
msgCHECK(mpiControlValidate(*control));
/* Initialize the controller */
returnValue =
mpiControlInit(*control);
msgCHECK(returnValue);
/* Create and validate a handle to the CAN object. */
*can =
mpiCanCreate(*control, canNumber);
msgCHECK(mpiCanValidate(*can));
}
/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl *control,
MPICan *can)
{
long returnValue;
/* Delete CAN handle */
returnValue =
mpiCanDelete(*can);
msgCHECK(returnValue);
*can = MPIHandleVOID;
/* Delete control handle */
returnValue =
mpiControlDelete(*control);
msgCHECK(returnValue);
*control = MPIHandleVOID;
}
/* Toggle CAN Outputs */
void toggleCanOutputs(MPICan can, long nodeNumber, long channelCount)
{
long returnValue;
long channel;
MPICanNodeInfo canNodeInfo;
/* Find out how many digital outputs this node supports. */
returnValue =
mpiCanNodeInfo(can, nodeNumber, &canNodeInfo);
msgCHECK(returnValue);
/* Turn off all the digital outputs. */
if( canNodeInfo.digitalOutputCount < 32 ) {
mpiCanNodeDigitalOutSet(can, nodeNumber, 0, canNodeInfo.digitalOutputCount, 0, 1 );
} else {
mpiCanNodeDigitalOutSet(can, nodeNumber, 0, 32, 0, 1 );
mpiCanNodeDigitalOutSet(can, nodeNumber, 32, canNodeInfo.digitalOutputCount - 32, 0, 1 );
}
/* Set each output bit in turn. */
for(channel = 0; channel<CHANNEL_COUNT; channel++) {
/* Turn digital output "on" */
returnValue =
mpiCanNodeDigitalOutSet(can, nodeNumber, channel, 1, 1, 1 );
msgCHECK(returnValue);
/* Pause for one half second */
mpiPlatformSleep(500);
/* Turn digital output "off" */
returnValue =
mpiCanNodeDigitalOutSet(can, nodeNumber, channel, 1, 0, 1 );
msgCHECK(returnValue);
}
}
int main(int argc,
char *argv[])
{
MPIControl control;
MPIControlType controlType;
MPIControlAddress controlAddress;
MPICan can;
long canNumber = CAN_NETWORK_NUMBER;
long channelCount = CHANNEL_COUNT;
/* Perform basic command line parsing. (-control -server -port -trace) */
basicParsing(argc,
argv,
&controlType,
&controlAddress);
/* Create and initialize MPI objects */
programInit(&control,
controlType,
&controlAddress,
&can,
canNumber);
/* Toggle CAN Outputs */
toggleCanOutputs(can, OUTPUT_NODE_NUMBER, CHANNEL_COUNT);
/* Perform certain cleanup actions and delete MPI objects */
programCleanup(&control,
&can);
/* return a success. */
return 0;
}
|