/* Send and Receive ASCII command from a i7k/i8k/i87k modules via serial port.

   Author: Moki Matsushima

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* File level history (record changes for this file here.)

   v 0.0.0  1 April 2005 by Moki Matsushima
     create, blah blah... */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <ctype.h>
#include "msw.h"
#include "modbus.h"

#define iTalkVersion	15
void	usage(void);

int main(int argc, char **argv)
{
	int RetValue,slot,comport,timeout, addr, count, function, netid;
	char szBuf[80], serial[10];
	DWORD baudrate;
	WORD wT, wValue;
	if (argc == 9) {
		comport = atoi(argv[1]);
		baudrate = atol(argv[2]);
		netid = atoi(argv[3]);
		function = atoi(argv[4]);
		addr = atoi(argv[5]);
		count = atoi(argv[6]);
		wValue = atol(argv[7]);
		timeout=atoi(argv[8]);
	}
	else {
		usage();
		return FAILURE;
	}

	switch(function) {
		case FC05ForceSingleCoil:
		case FC15ForceMultipleCoil:
			szBuf[0] = wValue & 0xff;
			break;
		case FC06PresetSingleRegister:
		case FC16PresetMultipleRegister:
			szBuf[0] = (wValue >> 8) & 0xff;
			szBuf[1] = wValue & 0xff;
			break;
		default:
			usage();
			return FAILURE;
	}

	if(!strcmp(argv[1],"ttySA0"))
	{
		comport = ttySA0;
		sprintf(serial, "ttySA0");
	}
	else if(!strcmp(argv[1],"ttySA1"))
	{
		comport = ttySA1;
		sprintf(serial, "ttySA1");
	}
	else
	{
		while(!isdigit(*argv[1]) && *argv[1])
			argv[1]++;

		comport = atoi(argv[1]) + 2;
		sprintf(serial, "ttyS%d", comport-2);
	}

	/* open device file */
	RetValue = Open_Com(comport, baudrate, Data8Bit, NonParity, OneStopBit);
	if (RetValue >0) {
		printf("open %s failed!\n",serial);
		return FAILURE;
	}

	RetValue = modbusRequest(comport, netid, function, addr, count, szBuf, sizeof(szBuf), timeout, &wT);
	Close_Com(comport);

	if (RetValue == 0) {
		return NoError;
	} else if (RetValue == CmdError) {
		printf("Module at %s return error code %d !!!\n",serial, szBuf[0]);
		return FAILURE;
	} else {
		printf("Module at %s error %d !!!\n",serial, RetValue);
		return FAILURE;
	}
}

void usage(void)
{
	printf("ICPDAS iTalk utility v%d\n",iTalkVersion);
	printf("function : setmodbus\n");
	printf("Send ASCII command and wait resopnse from a serial module\n");
	printf("Usage: setmodbus comport baudrate netid function addr count value timeout\n");
	printf("Example 1:setmodbus ttySA0 115200 1 15 3 1 1 100\n");
	printf("Write 1 with FC15 to addr 3 of netid 1 at ttySA0 and wait response\n");
	printf("Example 2:setmodbus ttyS1 115200 1 16 3 1 1234 100\n");
	printf("Write 1234 with FC16 to addr 3 of netid 1 at ttyS1 and wait response\n");
}
