/*--------------------------------------------------------------------------*/
/*  F&eIT RP-COM(FIT)H, RP-COM(FIT)H-AF, RP-COM(FIT)GY, FX-DS540-COM        */
/*  zCOMӰ ۸ݸ      Ver2.00 (2005-09-08)            */
/*                                                                          */
/*  @\:               F&IT@ւ̎M                                    */
/*                                                                          */
/*  쐬@:           Windows ̏ꍇ                                      */
/*                      Microsoft Visual Studio 6.0 gp                  */
/*                          cl -c -DWIN32 rx.c                              */
/*                          link tx.obj wsock32.lib                         */
/*                      Linux ̏ꍇ                                        */
/*                          cc rx.c -o rx                                   */
/*                                                                          */
/*                                                      CONTEC Co.,Ltd.     */
/*--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fitport.c"

#define IP_ADDRESS  "192.168.1.1"


//================================================================
// MTvC
//================================================================
int main(int argc, char *argv[])
{
	char		ip_addr[80];
	BYTE		recv_data[1400];
	PFIT_COMM	pfc;
	WORD		port_no;
	DWORD		baudrate;
	WORD		data_bit;
	WORD		parity_bit;
	WORD		stop_bit;
	WORD		flow_ctrl;
	WORD		ret;
	WORD		len;
	//-----------------------------------------
	// gp@̐
	//-----------------------------------------
	if (argc == 2 &&
		(strcmp(argv[1], "-help") == 0 ||
		strcmp(argv[1], "-h") == 0)) {
		printf("usage:%s <ip-address>\n", argv[0]);
		return 0;
	}
	//-----------------------------------------
	// IPAhX̃Rs[
	//-----------------------------------------
	if (argc == 2) {
		strcpy_s(ip_addr, 80, argv[1]);
	} else {
		strcpy_s(ip_addr, 80, IP_ADDRESS);
	}
	//-----------------------------------------
	// 
	//-----------------------------------------
	port_no = 1;
	baudrate = 9600;
	data_bit = 0;
	parity_bit = 0;
	stop_bit = 0;
	flow_ctrl = 0;
	pfc = FitCommOpen(ip_addr, port_no, baudrate, data_bit, parity_bit, stop_bit, flow_ctrl);
	if (pfc == NULL) {
		printf("Open error!\n");
		return 0;
	}
	//-----------------------------------------
	// [v
	//-----------------------------------------
	while(1) {
		//-----------------------------------------
		// M
		//-----------------------------------------
		memset(recv_data, 0x00, sizeof(recv_data));
		len = sizeof(recv_data);
		ret = FitCommReceive(pfc, recv_data, &len, 10);
		if (ret == 0) {
			printf("Receive:%s\n", recv_data);
		} else {
			printf("Receive timeout(%d).\n", ret);
		}
	}
	//-----------------------------------------
	// I
	//-----------------------------------------
	FitCommClose(pfc);
#ifdef  WIN32   /* Windows */
    WSACleanup();
#endif
	return 0;
}

