SQCable1.c -- Display SynqNet Cable Lengths
/* SQCable1.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 and Configure SynqNet Cable Lengths
This is a simple program to demonstrate how to read the SynqNet Cable
Length information and configuration. Then the discovered cable
length parameters are written to the controller's configuration.
Later, SynqNet initializations will compare the discovered cable
lengths to the configured min and max values. If a cable is outside
the configured range, an error will be returned.
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"
/* 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)
{
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));
}
/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl *control,
MEISynqNet *synqNet)
{
long returnValue;
returnValue =
meiSynqNetDelete(*synqNet);
msgCHECK(returnValue);
*synqNet = MPIHandleVOID;
/* Delete control handle */
returnValue =
mpiControlDelete(*control);
msgCHECK(returnValue);
*control = MPIHandleVOID;
}
/* Display SynqNet Cable Lengths */
void displaySynqNetCables(MPIControl control,
MEISynqNet synqNet)
{
MEISynqNetInfo netInfo;
long index;
long cableCount;
long returnValue;
/* Read SynqNet Info */
returnValue =
meiSynqNetInfo(synqNet, &netInfo);
msgCHECK(returnValue);
/* calculate cable count */
cableCount = netInfo.nodeCount;
if (cableCount) {
cableCount += 1;
}
else {
printf("No SynqNet Nodes found.\n");
}
/* Display cable length information/configuration */
printf("\nDiscovered Cable Lengths (meters)\n\n");
printf("Cable\tMin\tMax\tNom\n");
for (index = 0; index < cableCount; index++) {
printf("%ld\t%ld\t%ld\t%ld\n",
index,
netInfo.cableLength[index].minimum,
netInfo.cableLength[index].maximum,
netInfo.cableLength[index].nominal);
}
printf("\n");
}
void configureSynqNetCables(MPIControl control,
MEISynqNet synqNet)
{
MEISynqNetInfo netInfo;
MEISynqNetConfig netConfig;
long index;
long cableCount;
long returnValue;
/* Read SynqNet Info */
returnValue =
meiSynqNetInfo(synqNet, &netInfo);
msgCHECK(returnValue);
/* calculate cable count */
cableCount = netInfo.nodeCount;
if (cableCount) {
cableCount += 1;
}
else {
printf("No SynqNet Nodes found.\n");
}
/* Read SynqNet Configurations */
returnValue =
meiSynqNetConfigGet(synqNet,
&netConfig);
msgCHECK(returnValue);
printf("\nOLD Configured Cable Lengths (meters)\n\n");
printf("Cable\tMin\tMax\tNom\n");
for (index = 0; index < cableCount; index++) {
printf("%ld\t%ld\t%ld\t%ld\n",
index,
netConfig.cableLength[index].minimum,
netConfig.cableLength[index].maximum,
netConfig.cableLength[index].nominal);
}
printf("\n");
/* Fill-in the config structure */
for (index = 0; index < cableCount; index++) {
netConfig.cableLength[index].minimum = netInfo.cableLength[index].minimum;
netConfig.cableLength[index].maximum = netInfo.cableLength[index].maximum;
netConfig.cableLength[index].nominal = netInfo.cableLength[index].nominal;
}
/* Write SynqNet Configurations */
returnValue =
meiSynqNetConfigSet(synqNet,
&netConfig);
msgCHECK(returnValue);
printf("\nNEW Configured Cable Lengths (meters)\n\n");
printf("Cable\tMin\tMax\tNom\n");
for (index = 0; index < cableCount; index++) {
printf("%ld\t%ld\t%ld\t%ld\n",
index,
netConfig.cableLength[index].minimum,
netConfig.cableLength[index].maximum,
netConfig.cableLength[index].nominal);
}
printf("\n");
}
int main(int argc,
char *argv[])
{
MPIControl control;
MPIControlType controlType;
MPIControlAddress controlAddress;
MEISynqNet synqNet;
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);
/* Display SynqNet Cable Lengths */
displaySynqNetCables(control,
synqNet);
/* Configure Cable Lengths */
configureSynqNetCables(control,
synqNet);
/* Perform certain cleanup actions and delete MPI objects */
programCleanup(&control,
&synqNet);
return ((int)returnValue);
}
|