view KKFX80E.c @ 192:f105ac22da05

Fixes
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:31 +0200
parents 4f1f353e84f5
children 3427013e4f70
line wrap: on
line source

#include <ctype.h>
#include <windows.h>
#include <utility.h>
#include <ansi_c.h>


int (_stdcall *FX_OpenPort)(char **Port) = NULL;
void (_stdcall *FX_ClosePort)(void) = NULL;
int (_stdcall *FX_SendCommand)(char **Command) = NULL;
int (_stdcall *FX_GetReport)(char **Data) = NULL;
void (_stdcall *FX_Debug)(BOOL DbgFlag) = NULL;
// int (_stdcall *GetFXM)(char **Data) = NULL;
// void (_stdcall *Log)(int DbgNum) = NULL;
// int (_stdcall *ReadMonitor)(char **Data) = NULL;
// BOOL (_stdcall *EnumeratePorts)(char **PortName, char *PortNamePrefix) = NULL;

static HMODULE module;
static char *errormsg;


char * FX_Error(void)
{
	return errormsg;
}


int FX_Init(void)
{
	FARPROC funcp = NULL;
	errormsg = "";
	
	module = LoadLibrary("KK_FX80E.dll");
	if (! module)
		return 0;
	
	funcp = GetProcAddress(module, "FX_OpenPort");
  	if (! funcp)
		goto error;
	FX_OpenPort = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_ClosePort");
  	if (! funcp)
		goto error;
	FX_ClosePort = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_SendCommand");
  	if (! funcp)
		goto error;
	FX_SendCommand = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_GetReport");
  	if (! funcp)
		goto error;
	FX_GetReport = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_Debug");
  	if (! funcp)
		goto error;
	FX_Debug = (void *)funcp;
	
	return 1;
	
error:
	FreeLibrary(module);
	return 0;
}


void FX_Free(void)
{
	FreeLibrary(module);	
}


int FX_Open(char *port)
{
	int rv = FX_OpenPort(&port);
	if (! rv)
		errormsg = port;
	return rv;
}


void FX_Close(void)
{
	FX_ClosePort();
}


int FX_Send(char *cmd)
{
	int rv = FX_SendCommand(&cmd);
	if (! rv)
		errormsg = cmd;
	return rv;
}


int FX_Recv(char **ret, int timeout)
{
	int rv;
	char *data;
	double mark = Timer();
	do {
		data = ".";
		rv = FX_GetReport(&data);
		if ((Timer() - mark) > timeout) {
			rv = 0;
			data = "Function FX_Recv: Timeout";
		}
	} while ((! data) && (rv == 1));
	
	if (! rv) {
		errormsg = data;
		data = NULL;
	}
	
	if (ret)
		*ret = data;
	
	return rv;
}