﻿//==============================================================================================
//	API-ZM(WDM) Definition File																	
//==============================================================================================
using System;
using System.Runtime.InteropServices;

public enum CzmConst
{
	// Frequency
	ZM_FREQUENCY_120HZ						= 0,						// Frequency : 120Hz
	ZM_FREQUENCY_1kHZ						= 1,						// Frequency : 1kHz

	// Signal Level
	ZM_LEVEL_500mV							= 0,						// Level : 0.5V
	ZM_LEVEL_1V								= 1,						// Level : 1V

	// Measurement Speed
	ZM_SPEED_FAST							= 0,						// Measurement speed : FAST
	ZM_SPEED_NORMAL							= 1,						// Measurement speed : NORMAL
	ZM_SPEED_SLOW							= 2,						// Measurement speed : SLOW

	// Equivalent Circuit Mode
	ZM_CIRCUIT_AUTO							= 0,						// Equivalent circuit mode : AUTO
	ZM_CIRCUIT_SERIAL						= 1,						// Equivalent circuit mode : SERIAL
	ZM_CIRCUIT_PARALLEL						= 2,						// Equivalent circuit mode : PARALLEL

	// Range
	ZM_RANGE_4nF							= 3,						// Range : 4nF
	ZM_RANGE_40nF							= 4,						// Range : 40nF
	ZM_RANGE_400nF							= 5,						// Range : 400nF
	ZM_RANGE_4uF							= 6,						// Range : 4uF
	ZM_RANGE_40uF							= 7,						// Range : 40uF
	ZM_RANGE_400uF							= 8,						// Range : 400uF
	ZM_RANGE_1400uF							= 9,						// Range : 1.4mF

	// Correction
	ZM_CORRECTION_TYPE_OPEN					= 0,						// Correction type : OPEN
	ZM_CORRECTION_TYPE_SHORT				= 1,						// Correction type : SHORT
	ZM_CORRECTION_FORMAT_OPEN_GB			= 1,						// Correction format of open : GB
	ZM_CORRECTION_FORMAT_SHORT_RX			= 1,						// Correction format of short : RX

	// Contact Check
	ZM_CONTACTCHECK_DISABLE					= 0,						// Contact check : Disabled
	ZM_CONTACTCHECK_BEFORE					= 1,						// Contact check : Before measurement
	ZM_CONTACTCHECK_AFTER					= 2,						// Contact check : After measurement
	ZM_CONTACTCHECK_BOTH					= 3,						// Contact check : Before and after measurement

	// Enable/Disable
	ZM_DISABLE								= 0,						// Disable
	ZM_ENABLE								= 1,						// Enable

	// Definition Value
	CZM_MAX_PROCESS							= 16,						// Maximum number of process
	CZM_MAX_DEVICE							= 256,						// Maximum number of device


	/// <summary>
	/// Error Code
	/// </summary>
	// Initialization Error Code
	ZM_ERR_SUCCESS							= 0,						// Normality completion
	ZM_ERR_INI_RESOURCE						= 1,						// Failed in the acquisition of the resource
	ZM_ERR_INI_INTERRUPT					= 2,						// Failed in the registration of the interruption routine
	ZM_ERR_INI_MEMORY						= 3,						// Failed in the allocation of the memory. This error would hardly occur. If this error occurred, please extend the memory
	ZM_ERR_INI_REGISTRY						= 4,						// Failed in the access of registry
	ZM_ERR_SYS_RECOVERED_FROM_STANDBY		= 7,						// Execute ZmResetDevice function because the device has recovered from standby mode
	ZM_ERR_INI_NOT_FOUND_SYS_FILE			= 8,						// Initialization failed because czm.sys is not found
	ZM_ERR_INI_DLL_FILE_VERSION				= 9,						// Initialization failed because the file version of czm.dll cannot be acquired
	ZM_ERR_INI_SYS_FILE_VERSION				= 10,						// Initialization failed because the file version of czm.sys cannot be acquired
	ZM_ERR_INI_NO_MATCH_DRV_VERSION			= 11,						// Initialization failed because the file version of czm.dll is not same as czm.sys

	// DLL Error Code
	ZM_ERR_DLL_DEVICE_NAME					= 10000,					// The device name which wasn't registered by a device manager was specified
	ZM_ERR_DLL_INVALID_ID					= 10001,					// Invalid ID was specified. Please confirm whether the initialization function has completed normally. And confirm the scope of variable that stores ID
	ZM_ERR_DLL_CALL_DRIVER					= 10002,					// Driver can't be called
	ZM_ERR_DLL_CREATE_FILE					= 10003,					// Failed in the creation of the file
	ZM_ERR_DLL_CLOSE_FILE					= 10004,					// Failed in the closing of the file
	ZM_ERR_DLL_CREATE_THREAD				= 10005,					// Failed in the creation of the thread
	ZM_ERR_INFO_INVALID_DEVICE				= 10050,					// Specified device name is not found. Please check spelling
	ZM_ERR_INFO_NOT_FIND_DEVICE				= 10051,					// No more device
	ZM_ERR_INFO_INVALID_INFOTYPE			= 10052,					// Information type is invalid
	ZM_ERR_INFO_NOT_FOUND_DEVINFO			= 10053,					// Not found device information
	ZM_ERR_DLL_BUFF_ADDRESS					= 10100,					// Invalid data buffer address

	// Driver Error Code
	ZM_ERR_SYS_MEMORY						= 20000,					// Failed in the allocation of the memory. This error would hardly occur. If this error occurred, please extend the memory
	ZM_ERR_SYS_NOT_SUPPORTED				= 20001,					// This function can't be used by this device
	ZM_ERR_SYS_BOARD_EXECUTING				= 20002,					// Can not use while by another device works
	ZM_ERR_SYS_USING_OTHER_PROCESS			= 20003,					// Can not use because another process is using the device
	ZM_ERR_SYS_NOT_FOUND_PROCESS_DATA		= 20004,					// Process information is not found
	ZM_ERR_SYS_CHANNEL_NO					= 20100,					// Channel No is out of the settable range
	ZM_ERR_SYS_FREQUENCY					= 20101,					// Frequency is out of the settable range
	ZM_ERR_SYS_LEVEL						= 20102,					// Level is out of the settable range
	ZM_ERR_SYS_SPEED						= 20103,					// Speed is out of the settable range
	ZM_ERR_SYS_AVERAGE_COUNT				= 20104,					// AverageCount is out of the settable range
	ZM_ERR_SYS_CIRCUIT						= 20105,					// Circuit is out of the settable range
	ZM_ERR_SYS_RANGE						= 20106,					// Range is out of the settable range
	ZM_ERR_SYS_TRIGGER						= 20107,					// Trigger is out of the settable range
	ZM_ERR_SYS_TYPE							= 20108,					// Type is out of the settable range
	ZM_ERR_SYS_ENABLE						= 20109,					// Enable is out of the settable range
	ZM_ERR_SYS_MODE							= 20110,					// Mode is out of the settable range
	ZM_ERR_SYS_BIN_NO						= 20111,					// BinNo is out of the settable range
	ZM_ERR_SYS_TIME							= 20112,					// Time is out of the settable range
	ZM_ERR_SYS_FORMAT						= 20113,					// Format is out of the settable range
	ZM_ERR_SYS_TIMEOUT						= 20114,					// Timeout is out of the settable range
	ZM_ERR_SYS_CONTACTCHECK					= 20115,					// ContactCheck is out of the settable range
	ZM_ERR_SYS_MEASURE_STOP					= 20116,					// Measurement stopped
	ZM_ERR_SYS_MEASURE_TIMEOUT				= 20117,					// Measurement time out
	ZM_ERR_SYS_CONTACTCHECK_ENABLE			= 20118						// This setting is not possible because the contact check function is enabled
}

namespace CzmCs
{
	/// <summary>
	/// Summary description for Czm.
	/// </summary>
	public class Czm
	{
		/// <summary>
		/// Import unmanage DLL(CZM.DLL)
		/// </summary>
		// Common Function
		[DllImport("czm.dll")] static extern int ZmInit(string DeviceName, ref short Id);
		[DllImport("czm.dll")] static extern int ZmExit(short Id);
		[DllImport("czm.dll")] static extern int ZmResetDevice(short Id);
		[DllImport("czm.dll")] static extern int ZmGetErrorString(int ErrorCode , System.Text.StringBuilder ErrorString);

		// Information Acquisition Function
		[DllImport("czm.dll")] static extern int ZmQueryDeviceName(short Index, System.Text.StringBuilder DeviceName, System.Text.StringBuilder Device);
		[DllImport("czm.dll")] static extern int ZmGetMaxChannels(short Id, ref ushort MaxChannels);

		// Measurement Condition Function
		[DllImport("czm.dll")] static extern int ZmSetFrequency(short Id, ushort Channel, ushort Frequency);
		[DllImport("czm.dll")] static extern int ZmGetFrequency(short Id, ushort Channel, ref ushort Frequency);
		[DllImport("czm.dll")] static extern int ZmSetLevel(short Id, ushort Channel, ushort Level);
		[DllImport("czm.dll")] static extern int ZmGetLevel(short Id, ushort Channel, ref ushort Level);
		[DllImport("czm.dll")] static extern int ZmSetSpeed(short Id, ushort Channel, ushort Speed, uint AverageCount);
		[DllImport("czm.dll")] static extern int ZmGetSpeed(short Id, ushort Channel, ref ushort Speed, ref uint AverageCount);
		[DllImport("czm.dll")] static extern int ZmSetCircuit(short Id, ushort Channel, ushort Circuit);
		[DllImport("czm.dll")] static extern int ZmGetCircuit(short Id, ushort Channel, ref ushort Circuit);
		[DllImport("czm.dll")] static extern int ZmSetRange(short Id, ushort Channel, ushort Range);
		[DllImport("czm.dll")] static extern int ZmGetRange(short Id, ushort Channel, ref ushort Range);
		[DllImport("czm.dll")] static extern int ZmCorrection(short Id, ushort Channel, ushort Type);
		[DllImport("czm.dll")] static extern int ZmSetCorrectionEnable(short Id, ushort Channel, ushort Type, ushort Enable);
		[DllImport("czm.dll")] static extern int ZmGetCorrectionEnable(short Id, ushort Channel, ushort Type, ref ushort Enable);
		[DllImport("czm.dll")] static extern int ZmSetCorrectionValue(short Id, ushort Channel, ushort Type, float correction_value_1, float correction_value_2, ushort format);
		[DllImport("czm.dll")] static extern int ZmGetCorrectionValue(short Id, ushort Channel, ushort Type, ref float correction_value_1, ref float correction_value_2, ushort format);
		[DllImport("czm.dll")] static extern int ZmSetStartDelay(short Id, ushort Channel, float Time);
		[DllImport("czm.dll")] static extern int ZmGetStartDelay(short Id, ushort Channel, ref float Time);
		[DllImport("czm.dll")] static extern int ZmSetSignalOutputDelay(short Id, ushort Channel, float Time);
		[DllImport("czm.dll")] static extern int ZmGetSignalOutputDelay(short Id, ushort Channel, ref float Time);
		[DllImport("czm.dll")] static extern int ZmSetTriggerSyncEnable(short Id, ushort Channel, ushort Enable);
		[DllImport("czm.dll")] static extern int ZmGetTriggerSyncEnable(short Id, ushort Channel, ref ushort Enable);
		[DllImport("czm.dll")] static extern int ZmSetContactCheck(short Id, ushort Channel, ushort ContactCheck);
		[DllImport("czm.dll")] static extern int ZmGetContactCheck(short Id, ushort Channel, ref ushort ContactCheck);

		// Measurement Operation Function
		[DllImport("czm.dll")] static extern int ZmSingleCMeasure(short Id, ushort Channel, ref float CValue, ref float DValue, ref uint Status);

		// Constructor
		public Czm()
		{
		}

		// Common Function
		public int Init(string DeviceName, out short Id)
		{
			Id = 0;
			int ret = ZmInit(DeviceName, ref Id);
			return ret;
		}
		public int Exit(short Id)
		{
			int ret = ZmExit(Id);
			return ret;
		}
		public int ResetDevice(short Id)
		{
			int ret = ZmResetDevice(Id);
			return ret;
		}
		public int GetErrorString(int ErrorCode, out string ErrorString)
		{
			ErrorString = new String('0',1);
			System.Text.StringBuilder errorstring = new System.Text.StringBuilder(256);
			int ret = ZmGetErrorString(ErrorCode, errorstring);
			if(ret == 0)
			{
				ErrorString = errorstring.ToString();
			}
			return ret;
		}

		// Information Acquisition Function
		public int QueryDeviceName(short Index, out string DeviceName, out string Device)
		{
			DeviceName = new String('0',1);
			Device = new String('0',1);
			System.Text.StringBuilder devicename = new System.Text.StringBuilder(256);
			System.Text.StringBuilder device = new System.Text.StringBuilder(256);
			int ret = ZmQueryDeviceName(Index, devicename, device);
			if(ret == 0)
			{
				DeviceName = devicename.ToString();
				Device = device.ToString();
			}
			return ret;
		}
		public int GetMaxChannels(short Id, out ushort MaxChannels)
		{
			MaxChannels = 0;
			int ret = ZmGetMaxChannels(Id, ref MaxChannels);
			return ret;
		}

		// Measurement Condition Function
		public int SetFrequency(short Id, ushort Channel, ushort Frequency)
		{
			int ret = ZmSetFrequency(Id, Channel, Frequency);
			return ret;
		}
		public int GetFrequency(short Id, ushort Channel, out ushort Frequency)
		{
			Frequency = 0;
			int ret = ZmGetFrequency(Id, Channel, ref Frequency);
			return ret;
		}
		public int SetLevel(short Id, ushort Channel, ushort Level)
		{
			int ret = ZmSetLevel(Id, Channel, Level);
			return ret;
		}
		public int GetLevel(short Id, ushort Channel, out ushort Level)
		{
			Level = 0;
			int ret = ZmGetLevel(Id, Channel, ref Level);
			return ret;
		}
		public int SetSpeed(short Id, ushort Channel, ushort Speed, uint AverageCount)
		{
			int ret = ZmSetSpeed(Id, Channel, Speed, AverageCount);
			return ret;
		}
		public int GetSpeed(short Id, ushort Channel, out ushort Speed, out uint AverageCount)
		{
			Speed = 0;
			AverageCount = 0;
			int ret = ZmGetSpeed(Id, Channel, ref Speed, ref AverageCount);
			return ret;
		}
		public int SetCircuit(short Id, ushort Channel, ushort Circuit)
		{
			int ret = ZmSetCircuit(Id, Channel, Circuit);
			return ret;
		}
		public int GetCircuit(short Id, ushort Channel, out ushort Circuit)
		{
			Circuit = 0;
			int ret = ZmGetCircuit(Id, Channel, ref Circuit);
			return ret;
		}
		public int SetRange(short Id, ushort Channel, ushort Range)
		{
			int ret = ZmSetRange(Id, Channel, Range);
			return ret;
		}
		public int GetRange(short Id, ushort Channel, out ushort Range)
		{
			Range = 0;
			int ret = ZmGetRange(Id, Channel, ref Range);
			return ret;
		}
		public int Correction(short Id, ushort Channel, ushort Type)
		{
			int ret = ZmCorrection(Id, Channel, Type);
			return ret;
		}
		public int SetCorrectionEnable(short Id, ushort Channel, ushort Type, ushort Enable)
		{
			int ret = ZmSetCorrectionEnable(Id, Channel, Type, Enable);
			return ret;
		}
		public int GetCorrectionEnable(short Id, ushort Channel, ushort Type, out ushort Enable)
		{
			Enable = 0;
			int ret = ZmGetCorrectionEnable(Id, Channel, Type, ref Enable);
			return ret;
		}
		public int SetCorrectionValue(short Id, ushort Channel, ushort Type, float correction_value_1, float correction_value_2, ushort format)
		{
			int ret = ZmSetCorrectionValue(Id, Channel, Type, correction_value_1, correction_value_2, format);
			return ret;
		}
		public int GetCorrectionValue(short Id, ushort Channel, ushort Type, out float correction_value_1, out float correction_value_2, ushort format)
		{
			correction_value_1 = 0;
			correction_value_2 = 0;
			int ret = ZmGetCorrectionValue(Id, Channel, Type, ref correction_value_1, ref correction_value_2, format);
			return ret;
		}
		public int SetStartDelay(short Id, ushort Channel, float Time)
		{
			int ret = ZmSetStartDelay(Id, Channel, Time);
			return ret;
		}
		public int GetStartDelay(short Id, ushort Channel, out float Time)
		{
			Time = 0;
			int ret = ZmGetStartDelay(Id, Channel, ref Time);
			return ret;
		}
		public int SetSignalOutputDelay(short Id, ushort Channel, float Time)
		{
			int ret = ZmSetSignalOutputDelay(Id, Channel, Time);
			return ret;
		}
		public int GetSignalOutputDelay(short Id, ushort Channel, out float Time)
		{
			Time = 0;
			int ret = ZmGetSignalOutputDelay(Id, Channel, ref Time);
			return ret;
		}
		public int SetTriggerSyncEnable(short Id, ushort Channel, ushort Enable)
		{
			int ret = ZmSetTriggerSyncEnable(Id, Channel, Enable);
			return ret;
		}
		public int GetTriggerSyncEnable(short Id, ushort Channel, out ushort Enable)
		{
			Enable = 0;
			int ret = ZmGetTriggerSyncEnable(Id, Channel, ref Enable);
			return ret;
		}
		public int SetContactCheck(short Id, ushort Channel, ushort ContactCheck)
		{
			int ret = ZmSetContactCheck(Id, Channel, ContactCheck);
			return ret;
		}
		public int GetContactCheck(short Id, ushort Channel, out ushort ContactCheck)
		{
			ContactCheck = 0;
			int ret = ZmGetContactCheck(Id, Channel, ref ContactCheck);
			return ret;
		}

		// Measurement Operation Function
		public int SingleCMeasure(short Id, ushort Channel, out float CValue, out float DValue, out uint Status)
		{
			CValue = 0;
			DValue = 0;
			Status = 0;
			int ret = ZmSingleCMeasure(Id, Channel, ref CValue, ref DValue, ref Status);
			return ret;
		}
	}
}