#include "pds700.h"

int main(int argc, char *argv[])
{
	WORD rs;
	char in_str[BUFSIZ], tmp[BUFSIZ], out_str[BUFSIZ];
	pds_mod_t mod;
	int index = 0, i;
	char slen = 8, rlen = 9;  //slen = send byte length, rlen = receive byte length

	if (argc!= 4)
	{
		fprintf(stderr,"Usage:%s [PDS ip] [comport number] [baudrate]\a\n",argv[0]);
		exit(1);
	}
	
	memset(&mod, 0, sizeof(pds_mod_t));
	//BaudRate = 115200, DataFormat = 8n1
	rs = Open_Com(&mod, argv[1], 10000, atoi(argv[2]), atoi(argv[3]), 8, 0, 1);

	if(rs)
	{
		printf("Error Code %d : Open PDS-700 COM%d Error\n", rs, atoi(argv[2]));
		exit(1);
	}

	printf("Send modbus to PDS COM%d : \n",atoi(argv[2]));
	//Read M-7017 module name
	out_str[0] = 0x01;  //NETID
        out_str[1] = 0x03;  //Fuction code
        out_str[2] = 0x01;  //Modbus address high byte
        out_str[3] = 0xe2;  //Modbus address low byte
        out_str[4] = 0x00;  //channel number high byte
        out_str[5] = 0x02;  //channel number low byte
        out_str[6] = 0x65;  //CRC
        out_str[7] = 0xc1;  //CRC

	for(i = 0; i < slen; i++)
                printf("send_cmd[%d] 0x%x\n",i, out_str[i] & 0xff);

	rs = Send_Modbus(&mod, atoi(argv[2]), out_str, slen);

	if(rs)
		printf("Error Code %d : Send String Error\n", rs);

	rs = Read_Modbus(&mod, atoi(argv[2]), in_str, rlen);
	if(rs)
               	printf("Error Code %d : Read String Error\n", rs);

	printf("Read modbus from PDS COM%d : \n", atoi(argv[2]));

	for(i = 0; i < rlen; i++)
		printf("receive_cmd[%d] 0x%x\n",i, in_str[i] & 0xff);

	Close_Module(&mod);
}
