Search
 Documentation
Online Manual (PDF)
Code Samples
Performance Chart
Installation Guide
Building/Platform Compatability
Release Notes
Warranty and Licensing
testplc.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <plc.h>
 
int main(int argc, char *argv[])
{
  PLC *plc_ptr;
  unsigned short ai_data[4096], i_last;
  int i, j_bytes, j_ret=0;
 
  /* Verify we have enough command-line arguments and print usage otherwise */
  if(argc < 5 || (*argv[1] != 'r' && *argv[1] != 'w')) {
    printf("Usage: %s [r|w] \"module & hostname/port\" \"address\" "
           "#bytes [values...]\n", *argv);
    printf("Examples:\n"
           "  Read 10 bytes from N7:0\n"
           "    %s r \"abeth 192.168.0.2\" \"N7:0\" 10\n\n"
           "  Read 1 boolean bit from I3.5\n"
           "    %s r \"step7 10.1.0.14\" \"I3.5\" 1\n\n"
           "  Write the word \"200\" to N7:10\n"
           "    %s w \"cip 192.168.1.250\" \"N7:10\" 2 200\n\n"
           "  Write 5 words (200,201,202,203,204) starting at DB12.DW20\n"
           "    %s w \"step5 /dev/ttyS0\" \"DB12.DW20\" 10 200\n\n"
           "Use * in front of the module name to turn on debugging output.\n",
           *argv, *argv, *argv, *argv);
    return 1;
  }
 
  j_bytes=strtol(argv[4], NULL, 0);
  if(j_bytes < 1 || j_bytes > PLC_CHAR_MAX) {
    printf("Error: Data length '%s' is beyond the range 1-%d.\n", argv[4],
           PLC_CHAR_MAX);
    return 1;
  }
 
  /* Send all logging to the current terminal */
  plc_log_init(PLC_LOG_TTY);
 
  /* Open the PLC */
  plc_ptr=plc_open(argv[2]);
  if(!plc_ptr) {
    plc_print_error(plc_ptr, "plc_open");
    return 1;
  }
 
  /* Read data from the PLC */
  if(*argv[1] == 'r') {
    j_ret=plc_read(plc_ptr, PLC_RREG, argv[3], ai_data, j_bytes, 5000,
                   (j_bytes & 1)?PLC_CVT_NONE:PLC_CVT_WORD);
    if(j_ret < 0) {
      plc_print_error(plc_ptr, "plc_read");
      return 1;
    }
  }
 
  /* Write data to the PLC */
  else if(*argv[1] == 'w') {
    /* Build a list of values to send */
    if(j_bytes % 2) {
      if(argc < 6) {
        printf("Missing a value to write to '%s'.\n", argv[3]);
        return 1;
      }
      memset(ai_data, strtol(argv[5], NULL, 0), j_bytes);
    } else {
      i_last=-1;
      for(i=0;i < j_bytes/2;i++) {
        if(argc > i+5)
          ai_data[i]=i_last=strtol(argv[i+5], NULL, 0);
        else
          ai_data[i]=++i_last;
      }
    }
 
    j_ret=plc_write(plc_ptr, PLC_WREG, argv[3], ai_data, j_bytes, 5000,
                    (j_bytes & 1)?PLC_CVT_NONE:PLC_CVT_WORD);
    if(j_ret < 0) {
      plc_print_error(plc_ptr, "plc_write");
      return 1;
    }
    j_ret=j_bytes;
  }
 
  /* Display contents of the data buffer */
  printf("%s %d byte%s:", (*argv[1] == 'w')?"Wrote":"Read", j_ret,
         j_ret==1?"":"s");
 
  if(j_ret & 1) {
    /* If odd number of bytes, display using bytes */
    for(i=0;i < j_ret;i++)
      printf(" %02x", ((unsigned char *)ai_data)[i]);
  } else {
    /* If even number of bytes, display using words */
    for(i=0;i < j_ret/2;i++)
      printf(" %04x", ai_data[i]);
  }
  printf("\n");
 
  /* Close the PLC */
  if(plc_close(plc_ptr) == -1) {
    plc_print_error(plc_ptr, "plc_close");
    return 1;
  }
   
  return 0;  /* Exit successfully */
}
 
                              
© 2018 Commercial Timesharing, Inc. (CTI) All Rights Reserved      |     PLCIO is product of CTI      |     www.comtime.com     |     ph: 330.644.3059