// FT_List
// ----------------------------------------
//
// (c) miho 2014 http://www.mlab.cz
//
// This program if free.
//
//
// History:
//
// 1.00 2014_01 Start
// 1.01 2014_03 Wait at the end of the program
//
//
// Purpose:
//
//   List all FTDI serial com port chips found in Windows system with theirs com port numbers.
//
// Environment:
//
//   This is Win32 Console Application and run in WinXP / Win7 / Win8 both 32 and 64 bit.
//
// Compilation for Windows:
//
//   MS Visual C++ 2010 Express (free, registration required)
//   Create new _empty_ project for Win32 Console Application and name project FT_List
//   Header Files   / Add / Existing Items - all .h   files (not necessary)
//   Source Files   / Add / Existing Items - all .cpp files
//   Library Files  / Add / Existing Items - all .lib .h files from lib_win32 directory (not necessary)
//   Select Release version (no debug info)
//   Set static linkage Project Properties / Configuration Release / Configuration Properties 
//      / Code Generation / Runtime Library = Multithreaded (/MT)
//

#define YEAR "2014"
#define VERSION "1.01"


// Library Definitions
// -------------------

#include <stdio.h>															// Standard IO (printf, ...)
#include <conio.h>															// Windows _getch
#include <windows.h>														// Windows Console Application
#include "lib_win32\ftd2xx.h"										// FTDI Library

// Link with library
#pragma comment (lib, "lib_win32/ftd2xx.lib")		// Add this file to Resources


void waitforkey()
{
	printf("\nPress any Key ...\n");
	_getch();
	printf("\n");
}


// Main
// ----

int main(int argc, char *argv[])
{
	// Program Info
	printf("\n");
	printf("FTDI Comport Lister\n");
	printf("===================\n");
	printf("(c) miho " YEAR " v " VERSION "\n\n");

	// Enumerate FTDI Devices
	// ----------------------

	FT_STATUS ftStatus;
	FT_HANDLE ftHandle;

	// Print Library Version
	printf("FTDI Connect\n");
	DWORD dwLibraryVer;
	ftStatus = FT_GetLibraryVersion(&dwLibraryVer);
	if (ftStatus == FT_OK)
		printf("  Library Version   0x%x\n", dwLibraryVer);
	else
		fprintf(stderr, "\nFTDI: Error Reading Library Version\n");

	// Create Device Information List
	DWORD numDevs = 0;
	ftStatus = FT_CreateDeviceInfoList(&numDevs);
	if (ftStatus == FT_OK && numDevs>0)
		printf("  Devices Found     %d\n", numDevs);
	else
		printf("  No FTDI Device Found\n");

	if (numDevs==0)
	{
		waitforkey();
		return -1;
	}

	// List All FTDI Devices
	FT_HANDLE ftHandleTemp;
	DWORD Flags;
	DWORD ID;
	DWORD Type;
	DWORD LocId;
	char SerialNumber[16];
	char Description[64];
	for (DWORD i=0; i<numDevs; i++)
	{
		ftStatus = FT_GetDeviceInfoDetail(i, &Flags, &Type, &ID, &LocId, SerialNumber, Description, &ftHandleTemp);
		if (ftStatus == FT_OK)
		{
			printf("\nDevice %d\n", i);
			if (Flags & FT_FLAGS_OPENED)
			{
				printf("  Description       Device is used by another process\n");
			}
			else
			{
				printf("  Description       \"%s\"\n", Description);
				printf("  SerialNumber      \"%s\"\n", SerialNumber);
				// printf("  Flags             0x%x\n",   Flags);
				// printf("  Type              0x%x\n",   Type);
				// printf("  ID                0x%x\n",   ID);
				printf("  Location          0x%x\n", LocId);
				{
					long comPortNumber=-1;
					ftStatus = FT_Open(i, &ftHandle);
					FT_GetComPortNumber(ftHandle, &comPortNumber);
					if (comPortNumber==-1)
					{
						printf("  Com Port          not availabe");
					}
					else
					{
						printf("  Com Port          com%d", comPortNumber);
					}
					FT_Close(ftHandle);
				}
			}
			printf("\n");
		}
	}
	waitforkey();
	return 0;
}
