/*  Copyright (C) MOXA Inc. All rights reserved.

    This software is distributed under the terms of the
    MOXA License.  See the file COPYING-MOXA for details.
*/
/*
    mxcan_read.cpp

    This is for the read of canbus data.

    2010-10-31	Epson Chiang
		new release
*/


#include "stdafx.h"
#include <windows.h>
#include "..\SDK\mxcanbus.h"

#define MAX_MSG_SIZE			10

static unsigned int g_count = 0;

static void
print_can_message(CANMSG *msg)
{
	unsigned int i;

	printf("[0x%X 0x%X %d] ",msg->identifier,msg->flags, msg->length);
	for (i=0; i < msg->length; i++) printf("0x%X ", msg->data[i]);
	printf("\n");
}


int _tmain(int argc, _TCHAR* argv[])
{
	 int fd;
	int port = 0;
	ULONG ulPortIndex=0;
	DWORD dwBytesReturn=0;
	int max, packets=0;
	char buf[2048];
	unsigned int r_count = 0;
	int i=0;
	DWORD dwReadTotal=0;

	printf("Enter can port number:");
	scanf("%d",&port);

	
	/*Open can port*/
	fd = mxcan_open(port);

	while(1)
	{
		memset(buf,0,sizeof(buf));
		
		max = mxcan_read(fd, buf, MAX_MSG_SIZE*sizeof(CANMSG),NULL);
		
		if (max <= 0)
		{
			printf("No data\n");
			return -1;
		}
		dwReadTotal+=max;
		printf("-mxcan_read = %d dwReadTotal = %d\n", max, dwReadTotal);

	}
		
	/*Close can port*/
	mxcan_close(fd);
	
	return 0;
}

