Subversion Repositories factorylink.mb_plus

Rev

Blame | Last modification | View Log | Download

/*name test4b.c*/


/* Copyright (C) Modicon, Inc. 1989,  All Rights Reserved. */
/*
This test program uses the SA85.SYS device driver to read 125 holding
registers from the MODBUS slave which is found at the given node number.

In order to use this program, enter the following command:

    A>TEST4B 12 14

where "12" is the first slave node we want to read from, and 14 is the second
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 control_c(int sig);

/*
global variables


*/
char mbuffer[256];
char path1[ 80 ];
char path2[ 80 ];
int completed;

/*
main()


*/

int main( argc, argv )
int argc;
char *argv[];
  {
  NCB *nd1;
  NCB *nd2;
  int ret_val;
  int i;
    int j;

    printf( "MODBUS Command/Response test.\n" );
    printf( "\"+\" Indicates First Command Issued.\n" );
    printf( "\"<\" Indicates Second Command Issued.\n" );
    printf( "\"-\" Indicates First Response Received.\n" );
    printf( "\">\" Indicates Second Response Received.\n" );

    if( argc < 3 ) {
        printf( "Usage is: A>TEST4B <first slave node> <second slave node>\n");
        exit( 1 );
    }
    ret_val = sscanf( argv[ 1 ], "%d", &i );
    ret_val += sscanf( argv[ 2 ], "%d", &j );
    if( ret_val != 2 || i < 1 || i > 64 ) {
        printf( "Node numbers 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( path1, "DM.%d.0.0.0.0", i );
    sprintf( path2, "DM.%d.0.0.0.0", j );

  if ((nd1 = ncb_open( path1, 0 )) == NULL) {
    printf("Unable to open first DATA MASTER path.\n");
    exit(1);
    }
  if ((nd2 = ncb_open( path2, 1 )) == NULL) {
    printf("Unable to open second DATA MASTER path.\n");
    exit(1);
    }
  printf("Path %02X opened\n", nd1->NCB_NUM);
  printf("Routing info: %c%c.%d.%d.%d.%d.%d\n",
    nd1->NCB_CALLNAME[0],
    nd1->NCB_CALLNAME[1],
    nd1->NCB_CALLNAME[2],
    nd1->NCB_CALLNAME[3],
    nd1->NCB_CALLNAME[4],
    nd1->NCB_CALLNAME[5],
    nd1->NCB_CALLNAME[6]);

  printf("Path %02X opened\n", nd2->NCB_NUM);
  printf("Routing info: %c%c.%d.%d.%d.%d.%d\n",
    nd2->NCB_CALLNAME[0],
    nd2->NCB_CALLNAME[1],
    nd2->NCB_CALLNAME[2],
    nd2->NCB_CALLNAME[3],
    nd2->NCB_CALLNAME[4],
    nd2->NCB_CALLNAME[5],
    nd2->NCB_CALLNAME[6]);

    completed = 0;          /* global variable, do while zero */
    while( !completed ) {
      if( kbhit() )
            completed = 1;
        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*/
    printf("+");
    if (ncb_send(nd1, 6, mbuffer, 10) != 0) {   /*send first command*/
            /*send the command*/
      printf("Send error: %d.\n", nd1->NCB_RETCODE);
      ret_val = ncb_close(nd1); /*close the path*/
      ret_val = ncb_close(nd2); /*close the path*/
      exit(1);
      }

    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*/
    printf("<");
    if (ncb_send(nd2, 6, mbuffer, 10) != 0) {   /*send second command*/
            /*send the command*/
      printf("Send error: %d.\n", nd2->NCB_RETCODE);
      ret_val = ncb_close(nd1); /*close the path*/
      ret_val = ncb_close(nd2); /*close the path*/
      exit(1);
      }

    if (ncb_receive_wait(nd1, mbuffer, 10) != 0) {
            /*try to receive*/
      printf("\nReceive error: %d.\n", nd1->NCB_RETCODE);
      }
    printf("-");

    if (ncb_receive_wait(nd2, mbuffer, 10) != 0) {
            /*try to receive*/
      printf("\nReceive error: %d.\n", nd2->NCB_RETCODE);
      }
    printf(">");
    }
  ncb_close(nd1);   /*close the path*/
  ncb_close(nd2);   /*close the path*/
  exit(0);
    return( 0 );
  }

/*
control_c


This routine replaces the control-C handler supplied by DOS.  When you
type a control_C, the program will vector to this routine, which will
set the completed flag to non-zero.  In this test program, this will cause
the 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 */
}