#include "pds700.h"
#include <math.h>

#define SLEN  20
#define RLEN  10

int main(int argc, char *argv[])
{
	WORD rs, i, j, times;
	unsigned char in_str[BUFSIZ], out_str[BUFSIZ];
	pds_mod_t mod;
	
	if (argc!= 3)
	{
		fprintf(stderr,"Usage:%s [PDS ip] [COM num] \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]), 115200, 8, 0, 1);

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

	for(i = 0; i < SLEN; i++)
		out_str[i] = i;

	rs = Send_Binary(&mod, atoi(argv[2]), out_str, SLEN);

	if(rs)
                printf("Error Code %d : Send Binary Error\n", rs);
        else
        {
		printf("Send Binary to PDS COM%d :   ",atoi(argv[2]));
		for(i = 0; i < SLEN; i++)
                	printf("%d ",out_str[i]);
		printf("\n");
        }

	times = SLEN/RLEN;

	if(SLEN%RLEN != 0)
		times++;

	for(i = 0; i < times; i++)
	{
		rs = Read_Binary(&mod, atoi(argv[2]), in_str, RLEN);
		if(rs < 0)
			printf("Error Code %d : Read Binary Error\n", rs);
		else
		{
			printf("Read Binary from PDS COM%d : ", atoi(argv[2]));
			for(j = 0; j < rs; j++)
			{
				printf("%d ",in_str[j]);
			}
			printf("\n");
		}
	}

	Close_Module(&mod);
}
