#include "pds700.h"

int main(int argc, char *argv[])
{
	WORD rs;
	pds_mod_t mod;
	int i, run = 10;
	char server_ip[24], can_msg[30], msg[20];

	if(argc < 4 || argc != atoi(argv[3]) + 4)
	{
		fprintf(stderr,"Usage:%s [PDS ip] [CAN ID] [CAN DLC] ",argv[0]);
		if(argc > 3)
		{
			for(i = 0; i < atoi(argv[3]); i++)
				printf("[D%d] ",i);
		}
		printf("\n[CAN DLC] maximum is 8, number of CAN data must same with [CAN DLC]\n\n");
		exit(1);
	}
	
	printf("Example: ./pds_send_can 10.1.0.1 03f 8 01 03 05 07 09 0b 0d 0f\n");
	strcpy(server_ip, argv[1]);
	strcpy(msg, argv[4]);

	for(i = 1; i < atoi(argv[3]); i++)
		strcat(msg, argv[4+i]);
		
				//   ID     DLC    DATA
	sprintf(can_msg,"t%s%s%s",argv[2],argv[3],msg);
	printf("message you send \"%s\"\n",can_msg);

	memset(&mod, 0, sizeof(mod));
	rs = Open_CAN(&mod, server_ip);

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

	while(run > 0)
	{
		Send_CAN_Msg(&mod, can_msg);
		run--;
	}

	Close_CAN(&mod);
	Close_Module(&mod);
}
