//****************************************************************************
//                              Test2.c
//
// By: 		Daniel Kohn
// Date:	7/17/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/ test2.c -o test2 -lmodbus
//
// Function: test communications with all my automation direct modules by
//           sending a random on/off to all digital outputs, a fixed value
//           to the analog outputs and read back all digital outputs
//           and all digital inputs.
//****************************************************************************


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

int main()
{
   modbus_t *ctx;
   int x;
   uint8_t des[16];
   int16_t tab_reg[64];
   int rc;
   int i;

   // 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(1)
   {

      //set output bits 0-7 on T1K-08TSR and 8-24 on T1K-16TD2
      for(x=0;x<=24;x++)
      {
         if (rand()%2==1)
         {
            modbus_write_bit(ctx,x,TRUE);
         }
         else
         {
         modbus_write_bit(ctx,x,FALSE);
         }

      }

      printf("\x1B[2J\x1B\x63");	//VT100 clear screen command.
      printf("                                   1 1 1 1 1 1 1 1 1 1 2 2 2 2\n");
      printf("               0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3\n");

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

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

      //read A/D Input on T1F-8AD4DA-2

      printf("\nAnalog Inputs\n");

      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; i=i+2) 
      {
          printf("A/D[%d] = %d \n", i/2, tab_reg[i]);
      }


      // write to d/a status register (turn on output)
      // Note: (do NOT understand where theis address comes from)
      modbus_write_register(ctx,10022,1);
      sleep(1);
//      modbus_write_register(ctx,10022,0);

      // write to d/a 0
      modbus_write_register(ctx,0,1000);
//      sleep(1);
//      modbus_write_register(ctx,0,0);

      // write to d/a 1
      modbus_write_register(ctx,2,2000);
//      sleep(1);
//      modbus_write_register(ctx,2,0);

      //write to d/a 2
      modbus_write_register(ctx,4,3000);
//      sleep(1);
//      modbus_write_register(ctx,4,0);

      //write to d/a 3
      modbus_write_register(ctx,6,4000);
//      sleep(1);
//      modbus_write_register(ctx,6,0);

   }
}
