Blame | Last modification | View Log | Download
/*name test4.c*//* Copyright (C) Modicon, Inc. 1989, All Rights Reserved. *//*This test program uses the SA85.SYS device driver to read 125 holdingregisters from the MODBUS slave which is found at the given node number.In order to use this program, enter the following command:A>TEST4 12where "12" is the slave node we want to read from.*//*include*/#define STRICT#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <conio.h>#include <signal.h>#include "netbios.h"#include "netlib.h"/*prototypes*/int main(int argc, char *argv[] );void dump(int qty, PUSHORT buff);void control_c(int sig);/*global variables*/char mbuffer[256];char path[ 80 ];int completed;/*main()*/int main( argc, argv )int argc;char *argv[];{NCB *nd;int ret_val;int i;printf( "MODBUS Command/Response test. Strike any key to abort.\n" );if( argc < 2 ) {printf( "Usage is: A>TEST4 <slave node>\n");exit( 1 );}ret_val = sscanf( argv[ 1 ], "%d", &i );if( ret_val != 1 || i < 1 || i > 64 ) {printf( "Node number must be in the range 1 to 64. " );exit( 1 );}if( signal( SIGINT, control_c ) == SIG_ERR ) {printf( "Unable to redirect Control-C." );exit( 1 );}sprintf( path, "DM.%d.0.0.0.0", i );if ((nd = ncb_open( path, 0 )) == NULL) {printf("Unable to open DATA MASTER path.\n");exit(1);}printf("Path %02X opened\n", nd->NCB_NUM);printf("Routing info: %c%c.%d.%d.%d.%d.%d\n",nd->NCB_CALLNAME[0],nd->NCB_CALLNAME[1],nd->NCB_CALLNAME[2],nd->NCB_CALLNAME[3],nd->NCB_CALLNAME[4],nd->NCB_CALLNAME[5],nd->NCB_CALLNAME[6]);completed = 0; /* global variable, do while zero */while( !completed ) {if( kbhit() )completed = 1;/** Note: filling in mbuffer[0] with the slave address* is actually unecessary, since it was specified above* in the ncb_open. It is done here, and the buffer* pointer adjusted by 1 in ncb_send, to provide ease-of-use* and compatibility with existing modbus applications.*/mbuffer[0] = 0x5; /*slave address*/mbuffer[1] = 0x3; /*command*/mbuffer[2] = 0x0; /*offset high*/mbuffer[3] = 0x0; /*offset low*/mbuffer[4] = 0x0; /*reg count high*/mbuffer[5] = 125; /* reg count low*/if (ncb_send(nd, 6, mbuffer, 10) != 0) {/*send the command*/printf("Send error: %d.\n", nd->NCB_RETCODE);ret_val = ncb_close(nd); /*close the path*/exit(1);}if (ncb_receive_wait(nd, mbuffer, 10) != 0)/*try to receive*/printf("\nReceive error: %d.\n", nd->NCB_RETCODE);elsedump( 125, (PUSHORT) &mbuffer[ 3 ]);}ncb_close(nd); /*close the path*/exit(0); /* good exit */return( 0 );}/*dumpDump the contents of the buffer for the length specified.*/void dump(qty, buff)int qty; /* qty of bytes to dump */PUSHORT buff; /* buffer with the data */{ /* dump_lines */int i = 0, j;unsigned int out_value;printf("\n");do {printf("\n");for(j = 0; j < 8 && i < qty; j++, i++) {out_value = ((*buff << 8) & 0xff00) | ((*buff >> 8) & 0xff);printf("%04X ", out_value);buff++;}} while (i < qty);printf("\n");} /* dump_lines *//*control_cThis routine replaces the control-C handler supplied by DOS. When youtype a control_C, the program will vector to this routine, which willset the completed flag to non-zero. In this test program, this will causethe infinite loop within main to terminate.*/void control_c(int sig){signal( SIGINT, SIG_IGN ); /* disable control-c */completed = 1; /* will cause main loop to complete */signal( SIGINT, control_c ); /* reset control-c handler */}