/*
 *  send_read2.c
 *
 *  v 0.0 1999.12.20 by Reed Lai
 *
 *  open port, then quest the name of module 01
 *
 *  History
 *
 *  v 0.0 1999.12.20 by Reed Lai
 *    create, ... ok, i keep this as simple as possible
 */

// #define I7K_DEBUG
#include "msw.h"
#include "i7k.h"
#include "i7000.h"

#define RCVBUF_SIZE 16

int main(int argc,char **argv)
{
	int fd,rs;						/* file descriptor */
	char cmd[16];					/* point to command string */
	char rbuf[RCVBUF_SIZE];		/* receive buffer */
	__tod_t texp;				/* to receive the expended time */
	int s_s_comport, s_r_comport;
	char rx_str[128], tx_str[128];
	int comport, s_s_idx = 0, s_r_idx, u_idx = 0;
	unsigned long baud = 115200;
	int timeout = 5;
	WORD wT;
	int num = 1, tmp_num = 1;
	unsigned long error1 = 0, error2 = 0;
	unsigned long test_num = 1, total = 0;

	int c;

        while((c=getopt(argc, argv, "a:b:c:n:r:s:u:")) != -1)
        {
                switch(c)
                {
                        case 'b':
				baud = atol(optarg);
                                break;

                        case 'n':
				num = atoi(optarg);
				tmp_num = num;
                                break;

                        case 'r':
				s_r_idx = atoi(optarg);
                                s_r_comport = s_r_idx;
                                break;

                        case 's':
				s_s_idx = atoi(optarg);
                                s_s_comport = s_s_idx;
                                break;
                        case '?':
                                printf("wrong command\n");
                                break;
                }
        }

	if(s_s_comport == s_r_comport)
	{
		comport = s_s_comport;
		rs = Open_Com(comport, baud, Data8Bit, NonParity, OneStopBit);

                if (rs > 0) {
                        printf("open port failed!\n");
                        return (-1);
                }
		
	}
	else
	{
		rs = Open_Com(s_s_comport, baud, Data8Bit, NonParity, OneStopBit);

		if (rs > 0) {
			printf("open port failed!\n");
			return (-1);
		}

		s_s_idx = s_s_comport - 2;

		rs = Open_Com(s_r_comport, baud, Data8Bit, NonParity, OneStopBit);

		if (rs > 0) {
			printf("open port failed!\n");
			return (-1);
		}
	
		s_r_idx = s_r_comport - 2;
	}

	//printf("cmd len : %d\n",strlen(cmd));
	//while (getchar() != 27)
	printf("---------------------------------------------\n");
	while(num > 0)
	{
		sprintf(tx_str, "ttyS%d_test%d\r", s_s_idx, tmp_num);

#if 1
		Send_Cmd(s_s_comport, tx_str, 0);
#else
		Send_String(s_s_comport, tx_str, 0);
#endif
		printf("/dev/ttyS%d Send    : %s\n", s_s_idx, tx_str);

		memset(rx_str,0,sizeof(rx_str));
#if 1
		Receive_Cmd(s_r_comport, rx_str, 1, 0);
#else
		Receive_String(s_r_comport, rx_str, 100, 0);
#endif
		printf("/dev/ttyS%d Receive : %s\n", s_r_idx, rx_str);
				
		usleep(1000);
		printf("\n");

		sprintf(tx_str, "ttyS%d_test%d\r", s_r_idx, tmp_num);
#if 1
		Send_Cmd(s_s_comport, tx_str, 0);
#else
		Send_String(s_r_comport, tx_str, 0);
#endif
		printf("/dev/ttyS%d Send    : %s\n", s_r_idx, tx_str);

		memset(rx_str,0,sizeof(rx_str));
#if 1
		Receive_Cmd(s_r_comport, rx_str, 1, 0);
#else
		Receive_String(s_s_comport, rx_str, 100, 0);
#endif
		printf("/dev/ttyS%d Receive : %s\n", s_s_idx, rx_str);

		tmp_num--;
		num--;
		usleep(1000);
		printf("---------------------------------------------\n");
	}

	printf("finish ...\n");
	if(s_s_comport == s_r_comport)
	{
		Close_Com(comport);
	}
	else
	{
		Close_Com(s_s_comport);
		Close_Com(s_r_comport);
	}

	return (0);
}
