view file-data-provider.c @ 265:da38cbbc7ec8

Add DDS clients test code To verify the portabiolity of the clients code and to make testing easier the test code is independent of the CVI environment and runtime.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Sun, 21 Jun 2015 14:44:33 +0200
parents ec81395bf08d
children
line wrap: on
line source

/* FXAnalise data provider reading data from KK data file on disc */

#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <formatio.h>

#include "data-provider.h"

//#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
#define LOGFILEPATH "C:\\temp"


int CVICALLBACK FileDataProvider (void *functionData)
{
	int main_thread_id;
	char datafile[MAX_PATHNAME_LEN];
	int fd;
	int read;
	int year, month, day, hour, min, sec, msec;
	struct tm t;
	char line[1024];
	struct event event;
	double now;
	
	/* get main thread id to post messages to it */
	main_thread_id = CmtGetMainThreadID();
	
	/* guess current data log file name */
	GetCurrentDateTime(&now);
	GetDateTimeElements(now, NULL, NULL, NULL, &month, &day, &year);
	snprintf(datafile, sizeof(datafile) - 1, 
		"%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year % 100, month, day);
	
	/* open file */
	fd = OpenFile(datafile, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
	if (fd < 0) {
		/* notify error to the main thread */
		send_message(main_thread_id, "error opening data file '%s'", datafile);
	}
	
	/* seek to file end */
	SetFilePtr(fd, 0, 2);
	
	while (acquiring) {

		/* read one line from data file */
		read = ReadLine(fd, line, sizeof(line) - 1);
		if (read < 1) {
			Delay(0.05);
			continue;
		}
		
		/* parse data */
		read = Scan(line, "%d[w2]%d[w2]%d[w2] %d[w2]%d[w2]%d[w2].%d %f %f %f %f", 
			&year, &month, &day, &hour, &min, &sec, &msec,
			&event.data[0], &event.data[1], &event.data[2], &event.data[3]);
		if (read != 11) {
			/* notify error to the main thread */
			send_message(main_thread_id, "error parsing data log line");
			/* skip line */
			continue;
		}
		
		t.tm_hour = hour;
		t.tm_min = min;
		t.tm_sec = sec;
		t.tm_mday = day;
		/* correct month count */
		t.tm_mon = month - 1;
		/* years since 1900 */
		t.tm_year = year + 2000 - 1900;
		/* deduce if daylight saving is in place from the date and time itself */
		t.tm_isdst = -1;
		
		/* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */
		event.time.tv_sec = mktime(&t);
		event.time.tv_usec = msec * 1000;
		
		/* convert from kHz to Hz */
		event.data[0] *= 1000.0;
		event.data[1] *= 1000.0;
		event.data[2] *= 1000.0;
		event.data[3] *= 1000.0;
		
		/* push data into the data queue */
		CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
			
		/* handle switch to next data file */
		if ((hour == 23) && (min == 59) && (sec == 59)) {
				
			/* next data file name */
			snprintf(datafile, sizeof(datafile) - 1, 
				"%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year, month, day + 1);
				
			/* close data file */
			CloseFile(fd);
				
			/* wait for new data file to appear */
			int retry = 20;
			while (retry--) {
				fd = OpenFile(datafile, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
				if (fd != -1)
					break;
				Delay(0.01);
			}
		}
	}
	return 0;
}