using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace DebugToolInstrumentInterface;

public class TE_Handle
{
	private TEDriverType driver_Type;

	private string m_addressDesc;

	public int m_gpibHandle;

	public uint m_VisaDefaultRM;

	public uint m_VisaHandle;

	private uint m_visaWriteTerminator;

	private uint m_visaReadTerminator;

	private byte[] m_visaReadBuf;

	public TE_Handle()
	{
		driver_Type = TEDriverType.visa32;
		m_gpibHandle = 0;
		m_VisaDefaultRM = 0u;
		m_VisaHandle = 0u;
		m_visaWriteTerminator = 0u;
		m_visaReadTerminator = 0u;
		m_visaReadBuf = new byte[4096];
	}

	public void SetupDriverType(TEDriverType type)
	{
		if (type == TEDriverType.gpib_32 && driver_Type == TEDriverType.visa32)
		{
			if (m_VisaHandle != 0)
			{
				viClose(m_VisaHandle);
			}
			if (m_VisaDefaultRM != 0)
			{
				viClose(m_VisaDefaultRM);
			}
		}
		driver_Type = type;
	}

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viOpenDefaultRM(out uint m_defaultRM);

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viOpen(uint m_defaultRM, string rsrcName, uint mode, uint timeout, out uint vi);

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viSetAttribute(uint vi, uint attrName, uint attrValue);

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viGetAttribute(uint vi, uint attrName, byte[] attrValue);

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viWrite(uint vi, byte[] buffer, uint cnt, out uint retCnt);

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viRead(uint vi, byte[] buffer, uint cnt, out uint retCnt);

	[DllImport("visa32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
	public static extern int viClose(uint vi);

	private bool gpibInit(string descString)
	{
		return false;
	}

	private bool visaInit(string descString)
	{
		int num = 0;
		num |= viOpenDefaultRM(out m_VisaDefaultRM);
		if (num != 0 || m_VisaDefaultRM == 0)
		{
			return false;
		}
		num |= viOpen(m_VisaDefaultRM, descString, 0u, 10000u, out m_VisaHandle);
		if (num != 0 || m_VisaHandle == 0)
		{
			return false;
		}
		num |= viSetAttribute(m_VisaHandle, 1073676314u, 10000u);
		uint writeTerminator;
		switch (getInterfaceType())
		{
		case 1:
		case 2:
		case 3:
			writeTerminator = 0u;
			break;
		case 4:
		case 6:
			writeTerminator = ((!("INSTR" == getResourceName())) ? 10u : 0u);
			break;
		default:
			writeTerminator = 10u;
			break;
		}
		if (!SetWriteTerminator(writeTerminator))
		{
			return false;
		}
		if (!SetReadTerminator(10u))
		{
			return false;
		}
		if (num != 0)
		{
			return false;
		}
		return true;
	}

	private bool SetReadTerminator(uint terminator)
	{
		if (terminator == 10 || terminator == 13)
		{
			m_visaReadTerminator = terminator;
		}
		else
		{
			m_visaReadTerminator = 0u;
		}
		int num = 0;
		if (m_visaReadTerminator != 0)
		{
			if (viSetAttribute(m_VisaHandle, 1073676312u, m_visaReadTerminator) != 0)
			{
				return false;
			}
			if (viSetAttribute(m_VisaHandle, 1073676344u, 1u) != 0)
			{
				return false;
			}
		}
		else if (viSetAttribute(m_VisaHandle, 1073676344u, 0u) != 0)
		{
			return false;
		}
		return true;
	}

	private bool SetWriteTerminator(uint terminator)
	{
		uint attrValue;
		if (terminator == 10 || terminator == 13)
		{
			m_visaWriteTerminator = terminator;
			attrValue = 0u;
		}
		else
		{
			m_visaWriteTerminator = 0u;
			attrValue = 1u;
		}
		if (viSetAttribute(m_VisaHandle, 1073676310u, attrValue) != 0)
		{
			return false;
		}
		return true;
	}

	private string getResourceName()
	{
		string result = "";
		byte[] array = new byte[32];
		if (viGetAttribute(m_VisaHandle, 3221159937u, array) == 0)
		{
			result = Encoding.Default.GetString(GeneralHandle.TrimWithFirstNonZero(array));
		}
		return result;
	}

	private short getInterfaceType()
	{
		short result = 0;
		byte[] array = new byte[2];
		if (viGetAttribute(m_VisaHandle, 1073676657u, array) == 0)
		{
			result = BitConverter.ToInt16(array, 0);
		}
		return result;
	}

	private int gpibWrite(string cmd)
	{
		return -1;
	}

	private int visaWrite(string cmd)
	{
		int num = 0;
		switch (m_visaWriteTerminator)
		{
		case 13u:
			cmd += "\r";
			break;
		case 10u:
			cmd += "\n";
			break;
		}
		uint length = (uint)cmd.Length;
		byte[] bytes = Encoding.Default.GetBytes(cmd);
		uint retCnt = 0u;
		try
		{
			num = viWrite(m_VisaHandle, bytes, length, out retCnt);
			Thread.Sleep(100);
		}
		catch (Exception ex)
		{
			GeneralHandle.writeDebugLog(ex.Message);
			Close();
			Thread.Sleep(1000);
			visaInit(m_addressDesc);
			length = (uint)cmd.Length;
			bytes = Encoding.Default.GetBytes(cmd);
			retCnt = 0u;
			num = viWrite(m_VisaHandle, bytes, length, out retCnt);
		}
		return num;
	}

	private int gpibRead(out string retStr)
	{
		retStr = GeneralHandle.NO_NI_GPIB_SUPPORT_STATEMENT;
		return -1;
	}

	private int visaReadAction(out string retStr)
	{
		retStr = "";
		int num = 1073676294;
		uint retCnt = 0u;
		while (num == 1073676294)
		{
			GeneralHandle.BatchAssignValue(m_visaReadBuf, 0);
			num = viRead(m_VisaHandle, m_visaReadBuf, 4096u, out retCnt);
			string @string = Encoding.Default.GetString(m_visaReadBuf, 0, (int)retCnt);
			retStr += @string;
			Thread.Sleep(100);
		}
		return num;
	}

	private int visaRead(out string retStr)
	{
		int num = 0;
		try
		{
			return visaReadAction(out retStr);
		}
		catch (Exception ex)
		{
			GeneralHandle.writeDebugLog(ex.Message);
			Close();
			Thread.Sleep(1000);
			visaInit(m_addressDesc);
			return visaReadAction(out retStr);
		}
	}

	public bool Init(string descString)
	{
		m_addressDesc = descString;
		if (driver_Type != 0)
		{
			return visaInit(descString);
		}
		return gpibInit(descString);
	}

	public int write(string cmd)
	{
		if (driver_Type != 0)
		{
			return visaWrite(cmd);
		}
		return gpibWrite(cmd);
	}

	public int read(out string retStr)
	{
		if (driver_Type != 0)
		{
			return visaRead(out retStr);
		}
		return gpibRead(out retStr);
	}

	public void Close()
	{
		if (driver_Type == TEDriverType.visa32)
		{
			if (m_VisaHandle != 0)
			{
				viClose(m_VisaHandle);
			}
			if (m_VisaDefaultRM != 0)
			{
				viClose(m_VisaDefaultRM);
			}
		}
	}
}
