// ****************************************************************************
//                              Test3.c
//
// By: 		Daniel Kohn
// Date:	7/18/2013
//
// 2nd test using the libmodbus library and the Automation Direct
// Terminator I/O moduals.
//
// Code written using reference and  code snipits from:
//          http://libmodbus.org/site_media/html/libmodbus.html
//
// to compile: gcc -I /usr/include/modbus/ test3.c -o test3 -lmodbus
//
// Function: test communications with all my automation direct modules. 
//           Can be used as a command line or via a php script. 
//
//          usage:
//
//          test3 [options]
//
//          where options are:
//
//          -o # x    -> change output # (x=0 off, x=1 on)
//          -a # x    -> change analog # (x is value to output)
//          -c x      -> change analog control register (x is value to send)
//
// ****************************************************************************


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <modbus.h>

int main(int argc, char *argv[])
{
   modbus_t *ctx;
   int x;
   uint8_t des[16];
   int16_t tab_reg[64];
   int16_t reg[1];
   int16_t da[8];
   int rc;
   int i;
   int c;
   long value;
   int y;

   // Note: Default port number in libmodbus libary is INCORRECT 
   //       at least for my Automation Direct device. Default is 1502
   //       my devices uses 502.
   ctx = modbus_new_tcp_pi("192.168.0.53", "502");
   if (ctx == NULL) 
   {
      fprintf(stderr, "Unable to allocate libmodbus context\n");
      return -1;
   }

   if (modbus_connect(ctx) == -1) 
   {
      fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
      modbus_free(ctx);
      return -1;
   }

   while((c = getopt(argc, argv, "a:o:c:")) != -1)
   {
      switch(c)
      {
         // set analog output
         case'a':
            y=atoi(argv[2])*2;
            value=atol(argv[3]);
            modbus_write_register(ctx,y,value);
            usleep(500000);
            break;
         // set relay or DC output
         case'o':
            y=atoi(argv[2]);
            value=atol(argv[3]);
            if (value==1)
            {
               modbus_write_bit(ctx,y,TRUE);
            }
            else
            {
               modbus_write_bit(ctx,y,FALSE);
            }
            break;

         // set Analog Output Control
         case'c':
            // write to d/a status register (turn on output)
            // Note: (do NOT understand where theis address comes from)
            value=atoi(argv[2]);
            modbus_write_register(ctx,10022,value);
            break;
         default:
            printf("usage:\n\ntest3 [options]\n");
            printf("   -o # x    -> change output # (x=0 off, x=1 on)\n");
            printf("   -a # x    -> change analog # (x is value to output\n");
            printf("   -c x      -> change analog control register (x is value to send)\n");
            exit(EXIT_FAILURE);
            break;
      }
   }


   //Read output bits 0-24
   modbus_read_bits(ctx,0,24,des);
   for(x=0;x<=22;x++)
   {
      printf("%i,",des[x]);
   }
   printf("%i\n",des[x]);

   //Read inputs 0-7 on T1K-16ND3
   modbus_read_input_bits(ctx,0,16,des);
   for(x=0;x<=14;x++)
   {
      printf("%i,",des[x]);
   }
   printf("%i\n",des[x]);

   //read A/D Input on T1F-8AD4DA-2
   rc = modbus_read_input_registers(ctx, 0, 16, tab_reg);
   if (rc == -1) 
   {
       fprintf(stderr, "%s\n", modbus_strerror(errno));
       return -1;
   }

   for (i=0; i < rc-2; i=i+2) 
   {
       printf("%d,", tab_reg[i]);
   }
   printf("%d\n", tab_reg[i]);

   //get Analog Output Control
   modbus_read_registers(ctx,10022,1,reg);
   printf("%d\n",reg[0]);

  //get D/A Values
  modbus_read_registers(ctx,0,8,da);
  for(i=0;i<6;i=i+2)
   {
       printf("%d,", da[i]);
   }
   printf("%d\n", da[i]);

   modbus_close(ctx);

}
