일본 Contec 사의 GPIB 카드 제어 클래스 C++

Software/C++ Builder|2023. 3. 23. 10:56
반응형

1. Device :  GP-IB(PCI)FL 

2. 제조사 : 일본 Contec (https://www.contec.com/

3. 국내 판매 : (주)엔씨텍크놀로지(http://www.da-view.com/

4. 사용용도 : GPIB 통신 카드 

5. 제어 클래스 

typedef enum {NONE, CRLF, CR, LF} Delimiters ;
typedef enum {WITHOUT_EOI, WITH_EOI} EOI ;

class TgpibPort
{
private :
protected :
	int FTimeout ;
	virtual void __fastcall setTimeout(const int Timeout) { FTimeout = Timeout ; }  ;
public :
	virtual bool __fastcall Initialize(void) = 0 ;
	virtual int __fastcall gpibPrint(int Address, AnsiString Command) = 0 ;
	virtual int __fastcall gpibInput(int Address, char* Buffer, int* Count) = 0 ;

	__property int Timeout = {read=FTimeout,  write=setTimeout} ;
};


#include "gpibac.h"   // 헤더파일은 드라이버를 설치하면 찾을 수 있습니다. 

class TContecGpib : public TgpibPort
{
private :
	void __fastcall setTimeout(const int Timeout) override final ;
public :
	TContecGpib() ;
	~TContecGpib() ;

	bool __fastcall Initialize(void) override final ;
	int __fastcall gpibSetup(Delimiters delim, EOI eoi) ;
	int __fastcall gpibPrint(int Address, AnsiString Command)  override final ;
	int __fastcall gpibInput(int Address, char* Buffer, int* Count) override final ;
};


TContecGpib::TContecGpib()
{

}
//---------------------------------------------------------------------------
TContecGpib::~TContecGpib()
{
	GpExit();
}
//---------------------------------------------------------------------------
bool __fastcall TContecGpib::Initialize(void)
{
	bool result = false ;
	int		Ifctime ;
	DWORD	Ret, mode ;

	Ret = GpExit();							// Keep off initialize repeat
	Ret = GpIni();							// GP-IB initialize

	if((Ret & 0x00FF) == 0)                 // Check of GpIni
	{
		GpBoardsts(0x0a, &mode);			// Read Master/Slave mode
		if(mode == 0)                       // mode == 0 (master), mode == 1 (slove)
		{
			Ifctime = 1;					// Default
			Ret = GpIfc(Ifctime);           // Interface Clear

			if((Ret & 0x00FF) == 0)         // Check of GpIfc
			{
				Ret = GpRen();              // Sets REN (Remote Enable) to true.
				if((Ret & 0x00FF) == 0) result = true ;          // Check of GpRen
			}
		}
	}

	return result ;
}
//---------------------------------------------------------------------------
void __fastcall TContecGpib::setTimeout(const int Timeout)
{
	DWORD Ret ;

	Ret = GpTimeout(Timeout);      	// Default(10000ms)
	if((Ret & 0x00FF) != 0)
	{
		FTimeout = Timeout ;
    }
}
//---------------------------------------------------------------------------
int __fastcall TContecGpib::gpibSetup(Delimiters Delim, EOI Eoi)
{
	DWORD Ret ;
	int result = 0 ;

	Ret = GpDelim(Delim, Eoi);      	// default delim = 1, Eoi = 1

	if((Ret & 0x00FF) != 0)   result = -1 ;

	return result ;
}
//---------------------------------------------------------------------------
int __fastcall TContecGpib::gpibPrint(int Address, AnsiString Command)
{
	int result = 0, srlen ;
	char* srbuf ;
	DWORD	MyAddr, Cmd[16], Ret;

	Ret = GpBoardsts(0x08, &MyAddr);            // Read MyAddress

	if((Ret & 0x00FF) != 0) result = -1 ;
	else
	{
		try
		{
			Cmd[0] = 2 ;          // Number of talkers and listeners ( = Number of listeners + 1)
			Cmd[1] = MyAddr ;     // Talker address
			Cmd[2] = Address ;    // Listener address

			srlen = Command.Length() ;
			srbuf = new char(srlen + 10) ;

			strcpy(srbuf, Command.c_str()) ;
			Ret = GpTalk(Cmd, srlen, (UCHAR*)srbuf) ;
		}
		__finally
		{
			delete[] srbuf ;
		}

		if(Ret != 0) result = -2 ;
	}

    return result ;
}
//---------------------------------------------------------------------------
int __fastcall TContecGpib::gpibInput(int Address, char* Buffer, int* Count)
{
	int result = 0 ;
	DWORD MyAddr, srlen, Cmd[16], Ret ;

	Ret = GpBoardsts(0x08, &MyAddr);     // Read MyAddress

	if((Ret & 0x00FF) != 0) result = -1 ;
	else
	{
		Cmd[0] = 2;
		Cmd[1] = Address;
		Cmd[2] = MyAddr;

		memset(Buffer, '\0', strlen(Buffer));

		srlen = *Count ;
		Ret = GpListen(Cmd, &srlen, (UCHAR*)Buffer);

		*Count = srlen ;

		if (Ret >= 3) result = -2 ;         // 0,1,2 : Normal completion
	}

	return result ;
}
//---------------------------------------------------------------------------
반응형

댓글()

Contec 사의 GPIB 카드 (GP-IB(PCI)F) 제어를 위한 Class (C#)

Software/C#|2023. 1. 19. 13:53
반응형

일본 Contec 사의 GPIB 통신 카드인 GP-IB(PCI)F 를 사용하기 위한 Class 입니다. C++ 로 제작되어서 제공되는 DLL 라이브러리를 사용합니다. Contec 사에서는 C#을 위한 별도의 라이브러리는 제공을 안하고 DLL을 사용한 예제 코드만 제공합니다. 

APIGPIB1.dll
0.18MB

DLL 파일은 올려둔 파일을 사용하시거나 제공받은 CD에서 드라이버를 설치하시면 찾을 수 있습니다. 

여기서는 통신에 꼭 필요한 기능만 멤버 함수로 만들었습니다. 필요한 부분은 더 추가하셔서 만들어 쓰시면 되겠습니다. 

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

namespace CGpibModule
{
    public class CContecGpib
    {
        [DllImport("apigpib1.dll")] static extern uint GpIni();
		[DllImport("apigpib1.dll")] static extern uint GpIfc(uint IfcTime);
		[DllImport("apigpib1.dll")] static extern uint GpRen();
		[DllImport("apigpib1.dll")] static extern uint GpResetren();
		[DllImport("apigpib1.dll")] static extern uint GpTalk(uint[] Cmd, uint Srlen, string Srbuf);
        [DllImport("apigpib1.dll")] static extern uint GpTalk(uint[] Cmd, uint Srlen, byte[] Srbufb);
		[DllImport("apigpib1.dll")] static extern uint GpListen(uint[] Cmd, ref uint Srlen, StringBuilder Srbuf);
        [DllImport("apigpib1.dll")] static extern uint GpListen(uint[] Cmd, ref uint Srlen, byte[] Srbufb);
        [DllImport("apigpib1.dll")] static extern uint GpPoll(uint[] Cmd, uint[] Pstb);
		[DllImport("apigpib1.dll")] static extern uint GpSrq(uint Eoi);
		[DllImport("apigpib1.dll")] static extern uint GpStb(uint Stb);
		[DllImport("apigpib1.dll")] static extern uint GpDelim(uint Delim, uint Eoi);
		[DllImport("apigpib1.dll")] static extern uint GpTimeout(uint Timeout);
		[DllImport("apigpib1.dll")] static extern uint GpChkstb(ref uint Stb, ref uint Eoi);
		[DllImport("apigpib1.dll")] static extern uint GpReadreg(uint Reg, ref uint Preg);
		[DllImport("apigpib1.dll")] static extern uint GpDma(uint Dmamode, uint Dmach);
		[DllImport("apigpib1.dll")] static extern uint GpExit();
		[DllImport("apigpib1.dll")] static extern uint GpComand(uint[] Cmd);
		[DllImport("apigpib1.dll")] static extern uint GpDmainuse();
		[DllImport("apigpib1.dll")] static extern uint GpStstop(uint Stp);
		[DllImport("apigpib1.dll")] static extern uint GpDmastop();
		[DllImport("apigpib1.dll")] static extern uint GpPpollmode(uint Pmode);
		[DllImport("apigpib1.dll")] static extern uint GpStppoll(uint[] Cmd, uint Stppu);
		[DllImport("apigpib1.dll")] static extern uint GpExppoll(ref uint Pprdata);
		[DllImport("apigpib1.dll")] static extern uint GpStwait(uint Buscode);
		[DllImport("apigpib1.dll")] static extern uint GpWaittime(uint Timeout);
		[DllImport("apigpib1.dll")] static extern uint GpReadbus(ref uint Bussta);
		[DllImport("apigpib1.dll")] static extern uint GpSfile(uint[] Cmd, uint Srlen, string Fname);
		[DllImport("apigpib1.dll")] static extern uint GpRfile(uint[] Cmd, uint Srlen, string Fname);
		[DllImport("apigpib1.dll")] static extern uint GpSdc(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpDcl();
		[DllImport("apigpib1.dll")] static extern uint GpGet(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpGtl(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpLlo();
		[DllImport("apigpib1.dll")] static extern uint GpTct(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpCrst(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpCopc(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpCwai(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpCcls(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpCtrg(uint Adr);
		[DllImport("apigpib1.dll")] static extern uint GpCpre(uint Adr, uint Stb);
		[DllImport("apigpib1.dll")] static extern uint GpCese(uint Adr, uint Stb);
		[DllImport("apigpib1.dll")] static extern uint GpCsre(uint Adr, uint Stb);
		[DllImport("apigpib1.dll")] static extern uint GpQidn(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQopt(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQpud(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQrdt(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQcal(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQlrn(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQtst(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQopc(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQemc(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQgmc(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQlmc(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQist(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQpre(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQese(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQesr(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQpsc(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQsre(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQstb(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpQddt(uint Adr, ref uint Srlen, StringBuilder Srbuf);
		[DllImport("apigpib1.dll")] static extern uint GpTaLaBit(uint TaLaSts);
		[DllImport("apigpib1.dll")] static extern uint GpBoardsts(uint Reg, ref uint Preg);
		[DllImport("apigpib1.dll")] static extern uint GpSrqEvent(int hWnd, ushort wMsg, uint SrqOn);
		[DllImport("apigpib1.dll")] static extern uint GpSrqEventEx(int hWnd, ushort wMsg, uint SrqOn);
		[DllImport("apigpib1.dll")] static extern uint GpSrqOn();
		[DllImport("apigpib1.dll")] static extern uint GpDevFind(uint[] Fstb);
		[DllImport("apigpib1.dll")] static extern byte GpInpB(short Port);
		[DllImport("apigpib1.dll")] static extern short GpInpW(short Port);
		[DllImport("apigpib1.dll")] static extern int  GpInpD(short Port);
		[DllImport("apigpib1.dll")] static extern byte GpOutB(short Port, byte Dat);
		[DllImport("apigpib1.dll")] static extern short GpOutW(short Port, short Dat);
		[DllImport("apigpib1.dll")] static extern int  GpOutD(short Port, int Dat);
		[DllImport("apigpib1.dll")] static extern uint GpSetEvent(uint EventOn);
		[DllImport("apigpib1.dll")] static extern uint GpSetEventSrq(int hWnd, ushort wMsg, uint SrqOn);
		[DllImport("apigpib1.dll")] static extern uint GpSetEventDet(int hWnd, ushort wMsg, uint DetOn);
		[DllImport("apigpib1.dll")] static extern uint GpSetEventDec(int hWnd, ushort wMsg, uint DetOn);
		[DllImport("apigpib1.dll")] static extern uint GpSetEventIfc(int hWnd, ushort wMsg, uint IfcOn);
		[DllImport("apigpib1.dll")] static extern uint GpEnableNextEvent();
		[DllImport("apigpib1.dll")] static extern uint GpSrqEx(uint Stb, uint SrqFlag, uint EoiFlag);
		[DllImport("apigpib1.dll")] static extern uint GpUpperCode(uint UpperCode);
		[DllImport("apigpib1.dll")] static extern uint GpCnvSettings(string HeaderStr, string UnitStr, string SepStr, uint SfxFlag);
		[DllImport("apigpib1.dll")] static extern uint GpCnvSettingsToStr(uint PlusFlag, uint Digit, uint CutDown);
		[DllImport("apigpib1.dll")] static extern uint GpCnvStrToDbl(string Str, ref double DblData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvStrToDblArray(string Str, double[] DblData, ref uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpCnvStrToFlt(string Str, ref float FltData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvStrToFltArray(string Str, float[] FltData, ref uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpCnvDblToStr(StringBuilder Str, ref uint StrSize, double DblData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvDblArrayToStr(StringBuilder Str, ref uint StrSize, double[] DblData, uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpCnvFltToStr(StringBuilder Str, ref uint StrSize, float FltData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvFltArrayToStr(StringBuilder Str, ref uint StrSize, float[] FltData, uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpPollEx(uint[] Cmd, uint[] Pstb, uint[] Psrq);
		[DllImport("apigpib1.dll")] static extern uint GpSlowMode(uint SlowTime);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCvSettings(uint Settings);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCvi(byte[] Str, ref short ShtData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCviArray(byte[] Str, short[] ShtData, uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCvs(byte[] Str, ref float FltData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCvsArray(byte[] Str, float[] FltData, uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCvd(byte[] Str, ref double DblData);
		[DllImport("apigpib1.dll")] static extern uint GpCnvCvdArray(byte[] Str, double[] DblData, uint ArraySize);
		[DllImport("apigpib1.dll")] static extern uint GpTalkEx(uint[] Cmd, ref uint Srlen, string Srbuf);
        [DllImport("apigpib1.dll")] static extern uint GpTalkEx(uint[] Cmd, ref uint Srlen, byte[] Srbufb);
		[DllImport("apigpib1.dll")] static extern uint GpListenEx(uint[] Cmd, ref uint Srlen, StringBuilder Srbuf);
        [DllImport("apigpib1.dll")] static extern uint GpListenEx(uint[] Cmd, ref uint Srlen, byte[] Srbufb);
		[DllImport("apigpib1.dll")] static extern uint GpTalkAsync(uint[] Cmd, ref uint Srlen, string Srbuf);
        [DllImport("apigpib1.dll")] static extern uint GpTalkAsync(uint[] Cmd, ref uint Srlen, byte[] Srbufb);
		[DllImport("apigpib1.dll")] static extern uint GpListenAsync(uint[] Cmd, ref uint Srlen, StringBuilder Srbuf);
        [DllImport("apigpib1.dll")] static extern uint GpListenAsync(uint[] Cmd, ref uint Srlen, byte[] Srbufb);
        [DllImport("apigpib1.dll")] static extern uint GpCommandAsync(uint[] Cmd);
		[DllImport("apigpib1.dll")] static extern uint GpCheckAsync(uint WaitFlag, ref uint ErrCode);
		[DllImport("apigpib1.dll")] static extern uint GpStopAsync();
		[DllImport("apigpib1.dll")] static extern uint GpDevFindEx(short Pad, short Sad, ref short Lstn);
		[DllImport("apigpib1.dll")] static extern uint GpBoardstsEx(uint SetFlag, uint Reg, ref uint Preg);
		[DllImport("apigpib1.dll")] static extern uint GpSmoothMode(uint Mode);

        public CContecGpib() { }

        public Int32 Initialize() 
        {
            uint Ret;
            uint Master = 0;

            Ret = GpExit();                                  // Keep off initialize repeat
            Ret = GpIni();                                   // GP-IB initialize
            if ((Ret & 0xFF) != 0) return 1;

            GpBoardsts(0x0a, ref Master);                    // Get Master/Slave mode

            if (Master == 0)
            {
                Ret = GpIfc(1);                           // Default Ifctime = 1 
                if ((Ret & 0xFF) != 0) return 1;

                Ret = GpRen();
                if ((Ret & 0xFF) != 0) return 1;
            }
                                 
            Ret = GpTimeout(10000);                      // 10000ms (Default)
            if ((Ret & 0xFF) != 0) return 1;

            return 0;
        }

        public void Close() 
        {
            uint Master=0;                                        // Master/Slave mode flag
            uint Ret;
            uint[] Cmd = new uint[32];

            Ret = GpBoardsts(0x0a, ref Master);              // Mode check
            if (Ret == 80) return;                              // If found error then not doing 

            if (Master == 0)
            {                                                   // When mode is Master
                Cmd[0] = 2;                                     // Command count
                Cmd[1] = 0x3f;                                  // Unlisten / UNL
                Cmd[2] = 0x5f;                                  // UnTalken / UNT
                Ret = GpComand(Cmd);                         // Send command
                Ret = GpResetren();                          // Cancel remote
            }
            Ret = GpExit();
        }
        public Int32 GpibPrint(UInt32 address, string command) 
        {
            Int32 result = 0 ;
            uint MyAddr=0;
            uint[] Cmd = new uint[16];
            uint Ret;

            Ret = GpBoardsts(0x08, ref MyAddr);
            Cmd[0] = 2;
            Cmd[1] = MyAddr;
            Cmd[2] = address;

            Ret = GpTalk(Cmd, (uint)command.Length, command);

            if (Ret >= 3) result = 1 ;

            return result ;
        }
        public Int32 GpibInput(UInt32 address, StringBuilder readData)
        {
            Int32 result = 0 ;
            uint MyAddr = 0, srlen = 10000;
            uint[] Cmd = new uint[16];
            uint Ret;

            Ret = GpBoardsts(0x08, ref MyAddr);
            Cmd[0] = 2;
            Cmd[1] = address;
            Cmd[2] = MyAddr;
            Ret = GpListen(Cmd, ref srlen, readData);

            if (Ret >= 3) result = 1 ;

            return result;
        }

        public Int32 GpibInput(UInt32 address, byte[] readData)
        {
            Int32 result = 0 ;
            uint MyAddr = 0, srlen = (uint)readData.Length ;
            uint[] Cmd = new uint[16];
            uint Ret;

            Ret = GpBoardsts(0x08, ref MyAddr);
            Cmd[0] = 2;
            Cmd[1] = address;
            Cmd[2] = MyAddr;
            Ret = GpListen(Cmd, ref srlen, readData);

            if (Ret >= 3) result = 1 ;

            return result;
        }
    }
}

 

반응형

댓글()