#include <stdio.h>
#include <windows.h>

#include "USB-RFID.h"

int main()
	{
	// check the DLL version:
	if ((USB_RFID_Version() & 0xFF00) != 0x010A)
		{
		printf ("Wrong DLL Version: expected 1.10, found %d.%02d\n",
			USB_RFID_Version() / 0x100, USB_RFID_Version() & 0xFF);
		return -1;
		}

	// define the port number:
	const unsigned short PortNum = 0;

	// open the port, attach it to COM1:
	if (USB_RFID_Open (PortNum, 1))
		{
		// handle a possible error:
		printf ("%s\n", USB_RFID_ErrorMessage (PortNum));
		return 1;
		}

	// setup the device: enable the RFID
	if (USB_RFID_EnableRFID (PortNum, TRUE))
		{
		// handle a possible error:
		printf ("%s\n", USB_RFID_ErrorMessage (PortNum));
		return 2;
		}

	// main program loop:
	while (true)
		{
		// check for automatic data:
		WORD CommandID;
		const int Result =
			USB_RFID_CheckAutoInput (PortNum, CommandID);

		if (Result != 0) // handle possible errors
			{
			if (Result < 0)
				printf ("%s\n", USB_RFID_ErrorMessage (PortNum));
			else
				printf ("Unexpected command received\n");
			continue; // do not proceed on errors
			}

		// check if RFID data is ready
		if (CommandID & USB_RFID_AUTO)
			{
			BYTE Data [8];
			if (USB_RFID_GetLastRFID (PortNum, Data))
				printf ("%s\n", USB_RFID_ErrorMessage (PortNum));
			else
				{
				// handle RFID data here
				}
			}

		// insert further cases here

		// insert service of other commands here

		}

	//finish the program:
	return 0;
	}
