/* ==================================================
GPC/GPF-4301
Sample Program for C Console Application

Copyright 2001, 2004 Interface Corporation.
All rights reserved.

<Description>
Displays the received data from Kethley 195A
Coding style is C like.
=================================================== */
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <iostream>
#include "..\..\U2GPLIB.h"

#define BOARD_NO		0
#define K195A_ADRS	16
#define TR6840_ADRS	3


char recvbuffer[256];

WORD		MyGpibAdrs;		// USB-GPIBのGPIBアドレス
char		GpAdrsBuf[12];	// 使用するGPIB機器のGPIBアドレス

CHAR		szBuf[256];

BYTE		OutPutON; // 出力されていることを示すフラグ
int			ChkBtnOutVol; // チェックボタンの状態


int main()
{
	int i, j, n, t, T, nt, GpStatus;

	//int Length;
	DWORD Length;
	long AdrsMul[16];	// Address table(Keithley 195A)
	//int AdrsTR[16];	// Address table(TAKEDA RIKEN 6840)
	FILE *fp;
	char fni[64], recvdat[64], nT[16], dat[64];

	// Initialization of the specified board
	MyGpibAdrs = 0;
	GpStatus = gp_init(MyGpibAdrs, 0, 0);
	if (GpStatus != 0)
	{
		sprintf_s(szBuf, sizeof(szBuf), "gp_init() error [%d]", GpStatus);
		printf("%s", szBuf);
		return 0;
	}

	// IFCラインをTRUEにする
	GpStatus = gp_cli();
	if (GpStatus != 0)
	{
		sprintf_s(szBuf, sizeof(szBuf), "gp_cli() error [%d]", GpStatus);
		//SetDlgItemText( hwnd, IDS_STATUS, szBuf );
		printf("%s", szBuf);
		return 0;
	}

	// RENラインをTRUEにする
	GpStatus = gp_ren();
	if (GpStatus != 0)
	{
		sprintf_s(szBuf, sizeof(szBuf), "gp_ren() error [%d]", GpStatus);
		//SetDlgItemText( hwnd, IDS_STATUS, szBuf );
		printf("%s", szBuf);
		return 0;
	}




	// Specifies the address table.
	AdrsMul[0] = K195A_ADRS;
	AdrsMul[1] = -1;
	//AdrsTR[0] = TR6840_ADRS;
	//AdrsTR[1] = -1;





	//File Output
	printf("Input the file name for output. > ");
	scanf("%s", fni);
	strcat(fni, ".csv");
	fp = fopen(fni, "w");
	printf("Input the number of times to count. > ");
	scanf("%d", &n);
	printf("Input the interval of waiting time.[s] > ");
	scanf("%d", &t);
	T = t * 1000;

	//GpibSend(BOARD_NO, AdrsMul, 4, "*RST");
	//GpibSend(BOARD_NO, AdrsMul, 4, "*CLS");
	//GpibSend(BOARD_NO, AdrsMul, 3, "F6X");
	gp_wrt("15", "*RST");
	gp_wrt("15", "*CLS");
	gp_wrt("15", ":FUNC 'RES'");
	// Configures the device.


	for (i = 0; i < n; i++) {
		dat[0] = '\0';
		// Sends the device command.
		//GpibSend(BOARD_NO, AdrsMul, 3, "B1X");
		gp_wrt("15", "READ?");

		// Receives data.
		Length = sizeof(recvbuffer);
		int ierror;
		memset(recvbuffer, 0x00, sizeof(recvbuffer));
		GpStatus = gp_red("15", recvbuffer, sizeof(recvbuffer));

		if (GpStatus != 0)
		{
			printf("Data receive error %d \n", GpStatus);
			Sleep(10000);//mili second
			printf("Data receive error\n");
			exit(0);
		}
		for (j = 4; recvbuffer[j] != NULL; j++) {
			recvdat[j - 4] = recvbuffer[j];
		}
		recvdat[j] = '\0';
		nt = i * t;
		sprintf(nT, "%d", nt);


		strcat(dat, nT);
		strcat(dat, ",");
		strcat(dat, recvdat);
		strcat(dat, "\n\0");
		fprintf(fp, "%s", dat);


		// Displays the data.
		//recvbuffer[Length] = 0;
		printf("Receive Data = %s\n", recvbuffer);

		Sleep(T);//mili second
	}
	fclose(fp);

	//PciGpibExMastSendData(BOARD_NO, AdrsTR, 10, "OUTPut OFF", 0);

	// REN line assertion
	//PciGpibExResetRen(BOARD_NO);
	//GpibClose(BOARD_NO);
	//exit(0);
	return 0;
}
