22 #ifdef HAVE_LINUX_GPIB
23 #define __inline__ __inline
28 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
29 #define DIRECT_ENTRY_NI488
30 static int load_ni4882dll();
31 static int free_ni4882dll();
32 #endif // WINDOWS || __WIN32__ || defined _WIN32
33 inline int strerror_r(
int err,
char *buf,
size_t len) {
return strerror_s(buf,len,err); }
36 static void (__stdcall *pEnableRemote)(int,
const Addr4882_t*);
37 #define EnableRemote (*pEnableRemote)
38 static void (__stdcall *pSendIFC)(int);
39 #define SendIFC (*pSendIFC)
40 static unsigned long(__stdcall *pThreadIbsta)(void);
41 #define ThreadIbsta() (pThreadIbsta())
42 static unsigned long(__stdcall *pThreadIberr)(void);
43 #define ThreadIberr() (pThreadIberr())
44 static unsigned long(__stdcall *pThreadIbcnt)(void);
45 #define ThreadIbcnt() (pThreadIbcnt())
46 unsigned long (__stdcall *pibclr)(int);
47 #define ibclr (*pibclr)
48 unsigned long (__stdcall *pibconfig)(int, int, int );
49 #define ibconfig (*pibconfig)
50 static int(__stdcall *pibdev)(int, int, int, int, int, int);
51 #define ibdev (*pibdev)
52 static int(__stdcall *pibonl)(int, int);
53 #define ibonl (*pibonl)
54 unsigned long (__stdcall *pibrd)(int,
void *, size_t);
56 unsigned long (__stdcall *pibrsp)(int,
char *);
57 #define ibrsp (*pibrsp)
58 unsigned long (__stdcall *pibwrt)(int,
const void *, size_t);
59 #define ibwrt (*pibwrt)
63 #define MIN_BUF_SIZE 1024
68 XNIGPIBPort::s_cntOpened = 0;
73 XNIGPIBPort::gpibStatus(
const XString &msg) {
75 if(ThreadIbsta() & DCAS) sta +=
"DCAS ";
76 if(ThreadIbsta() & DTAS) sta +=
"DTAS ";
77 if(ThreadIbsta() & LACS) sta +=
"LACS ";
78 if(ThreadIbsta() & TACS) sta +=
"TACS ";
79 if(ThreadIbsta() & ATN) sta +=
"ATN ";
80 if(ThreadIbsta() & CIC) sta +=
"CIC ";
81 if(ThreadIbsta() & REM) sta +=
"REM ";
82 if(ThreadIbsta() & LOK) sta +=
"LOK ";
83 if(ThreadIbsta() & CMPL) sta +=
"CMPL ";
84 #ifdef HAVE_LINUX_GPIB
85 if(ThreadIbsta() & EVENT) sta +=
"EVENT ";
86 if(ThreadIbsta() & SPOLL) sta +=
"SPOLL ";
87 #endif //HAVE_LINUX_GPIB
88 if(ThreadIbsta() & RQS) sta +=
"RQSE ";
89 if(ThreadIbsta() & SRQI) sta +=
"SRQI ";
90 if(ThreadIbsta() & END) sta +=
"END ";
91 if(ThreadIbsta() & TIMO) sta +=
"TIMO ";
92 if(ThreadIbsta() & ERR) sta +=
"ERR ";
93 switch(ThreadIberr()) {
94 case EDVR: err =
"EDVR";
break;
95 case ECIC: err =
"ECIC";
break;
96 case ENOL: err =
"ENOL";
break;
97 case EADR: err =
"EADR";
break;
98 case EARG: err =
"EARG";
break;
99 case ESAC: err =
"ESAC";
break;
100 case EABO: err =
"EABO";
break;
101 case ENEB: err =
"ENEB";
break;
102 case EDMA: err =
"EDMA";
break;
103 case EOIP: err =
"EOIP";
break;
104 case ECAP: err =
"ECAP";
break;
105 case EFSO: err =
"EFSO";
break;
106 case EBUS: err =
"EBUS";
break;
107 case ESTB: err =
"ESTB";
break;
108 case ESRQ: err =
"ESRQ";
break;
109 case ETAB: err =
"ETAB";
break;
110 default: err = formatString(
"%u",(
unsigned int)ThreadIberr());
break;
112 if((ThreadIberr() == EDVR) || (ThreadIberr() == EFSO)) {
115 char *s = strerror_r(ThreadIbcntl(), buf,
sizeof(buf));
116 cntl = formatString(
"%d",(
int)ThreadIbcntl()) +
" " + s;
118 if(strerror_r(ThreadIbcntl(), buf,
sizeof(buf))) {
119 cntl = formatString(
"%d",(
int)ThreadIbcntl());
122 cntl = formatString(
"%d",(
int)ThreadIbcntl()) +
" " + buf;
128 cntl = formatString(
"%d",(
int)ThreadIbcntl());
130 return QString(
"GPIB %1: addr %2, sta %3, err %4, cntl %5")
139 :
XPort(interface), m_ud(-1) {
142 XNIGPIBPort::~XNIGPIBPort() {
151 XNIGPIBPort::gpib_reset() throw (
XInterface::XCommError &) {
160 m_address = shot[ *pInterface->address()];
162 m_gpibWaitBeforeWrite = pInterface->gpibWaitBeforeWrite();
163 m_gpibWaitBeforeRead = pInterface->gpibWaitBeforeRead();
164 m_gpibWaitBeforeSPoll = pInterface->gpibWaitBeforeSPoll();
165 m_bGPIBUseSerialPollOnWrite = pInterface->gpibUseSerialPollOnWrite();
166 m_bGPIBUseSerialPollOnRead = pInterface->gpibUseSerialPollOnRead();
167 m_gpibMAVbit = pInterface->gpibMAVbit();
171 XNIGPIBPort::gpib_open() throw (
XInterface::XCommError &) {
172 int port = atoi(portString().c_str());
175 if(s_cntOpened == 0) {
176 #ifdef DIRECT_ENTRY_NI488
180 dbgPrint(i18n(
"GPIB: Sending IFC"));
187 Addr4882_t addrtbl[2];
190 ceos = 0x1400 + eos()[eos().length() - 1];
196 gpibStatus(i18n(
"opening gpib device faild")), __FILE__, __LINE__);
202 EnableRemote(port, addrtbl);
206 XNIGPIBPort::gpib_close() throw (
XInterface::XCommError &) {
207 if(m_ud >= 0) ibonl(m_ud, 0);
212 #ifdef DIRECT_ENTRY_NI488
223 assert(buf.length() == strlen(str) + eos().length());
224 this->write(buf.c_str(), buf.length());
228 gpib_spoll_before_write();
230 for(
int i = 0; ; i++) {
231 msecsleep(m_gpibWaitBeforeWrite);
232 int ret = ibwrt(m_ud, const_cast<char*>(sendbuf), size);
235 switch(ThreadIberr()) {
239 dbgPrint(
"EDVR/EFSO, try to continue");
240 msecsleep(10 * i + 10);
244 gpibStatus(i18n(
"too many EDVR/EFSO")), __FILE__, __LINE__);
246 gErrPrint(gpibStatus(i18n(
"ibwrt err")));
249 gErrPrint(i18n(
"try to continue"));
254 size -= ThreadIbcntl();
255 if((size == 0) && (ret & CMPL)) {
259 sendbuf += ThreadIbcntl();
261 dbgPrint(
"ibwrt interrupted.");
264 gErrPrint(gpibStatus(i18n(
"ibwrt terminated without CMPL")));
268 XNIGPIBPort::receive() throw (
XInterface::XCommError &) {
269 unsigned int len = gpib_receive(MIN_BUF_SIZE, 1000000uL);
275 unsigned int len = gpib_receive(length, length);
276 buffer().resize(len);
280 XNIGPIBPort::gpib_receive(
unsigned int est_length,
unsigned int max_length)
283 gpib_spoll_before_read();
285 for(
int i = 0; ; i++) {
286 unsigned int buf_size = std::min(max_length, len + est_length);
287 if(buffer().size() < buf_size)
288 buffer().resize(buf_size);
289 msecsleep(m_gpibWaitBeforeRead);
290 int ret = ibrd(m_ud, &buffer()[len], buf_size - len);
292 switch(ThreadIberr()) {
296 dbgPrint(
"EDVR/EFSO, try to continue");
297 msecsleep(10 * i + 10);
301 gpibStatus(i18n(
"too many EDVR/EFSO")), __FILE__, __LINE__);
303 gErrPrint(gpibStatus(i18n(
"ibrd err")));
306 gErrPrint(i18n(
"try to continue"));
311 if(ThreadIbcntl() > buf_size - len)
313 len += ThreadIbcntl();
314 if((ret & END) && (ret & CMPL)) {
318 if(len == max_length)
320 dbgPrint(
"ibrd terminated without END");
323 gErrPrint(gpibStatus(i18n(
"ibrd terminated without CMPL")));
328 XNIGPIBPort::gpib_spoll_before_read() throw (
XInterface::XCommError &) {
329 if(m_bGPIBUseSerialPollOnRead) {
330 for(
int i = 0; ; i++) {
333 gpibStatus(i18n(
"too many spoll timeouts")), __FILE__, __LINE__);
335 msecsleep(m_gpibWaitBeforeSPoll);
337 int ret = ibrsp(m_ud,(
char*)&spr);
339 switch(ThreadIberr()) {
342 dbgPrint(
"EDVR/EFSO, try to continue");
343 msecsleep(10 * i + 10);
346 gErrPrint(gpibStatus(i18n(
"ibrsp err")));
350 if(((spr & m_gpibMAVbit) == 0)) {
352 msecsleep(10 * i + 10);
361 XNIGPIBPort::gpib_spoll_before_write() throw (
XInterface::XCommError &) {
362 if(m_bGPIBUseSerialPollOnWrite) {
363 for(
int i = 0; ; i++) {
366 gpibStatus(i18n(
"too many spoll timeouts")), __FILE__, __LINE__);
368 msecsleep(m_gpibWaitBeforeSPoll);
370 int ret = ibrsp(m_ud,(
char*)&spr);
372 switch(ThreadIberr()) {
375 dbgPrint(
"EDVR/EFSO, try to continue");
376 msecsleep(10 * i + 10);
379 gErrPrint(gpibStatus(i18n(
"ibrsp err")));
382 if((spr & m_gpibMAVbit)) {
388 gErrPrint(gpibStatus(i18n(
"ibrd before ibwrt asserted")));
391 gpib_receive(MIN_BUF_SIZE, 1000000uL);
400 #if defined DIRECT_ENTRY_NI488
403 static HINSTANCE ni4882dll = NULL;
405 static int load_ni4882dll() {
406 ni4882dll=LoadLibrary(L
"NI4882.DLL");
407 if(ni4882dll == NULL) {
411 pEnableRemote = (void (__stdcall *)
412 (int,
const Addr4882_t*))GetProcAddress(ni4882dll,
"EnableRemote");
413 pSendIFC = (void (__stdcall *)(int))GetProcAddress(ni4882dll,
"SendIFC");
414 pThreadIbsta = (
unsigned long (__stdcall *)(void))GetProcAddress(ni4882dll,
"ThreadIbsta");
415 pThreadIberr = (
unsigned long (__stdcall *)(void))GetProcAddress(ni4882dll,
"ThreadIberr");
416 pThreadIbcnt = (
unsigned long (__stdcall *)(void))GetProcAddress(ni4882dll,
"ThreadIbcnt");
417 pibclr = (
unsigned long (__stdcall *)(int)) GetProcAddress(ni4882dll,
"ibclr");
418 pibconfig = (
unsigned long (__stdcall *)
419 (int, int, int)) GetProcAddress(ni4882dll,
"ibconfig");
420 pibdev = (int (__stdcall *)
421 (int, int, int, int, int, int)) GetProcAddress(ni4882dll,
"ibdev");
422 pibonl = (int (__stdcall *)(int, int)) GetProcAddress(ni4882dll,
"ibonl");
423 pibrd = (
unsigned long (__stdcall *)
424 (int,
void *, size_t)) GetProcAddress(ni4882dll,
"ibrd");
425 pibrsp = (
unsigned long (__stdcall *)
426 (int,
char*)) GetProcAddress(ni4882dll,
"ibrsp");
427 pibwrt = (
unsigned long (__stdcall *)
428 (int,
const void *, size_t)) GetProcAddress(ni4882dll,
"ibwrt");
430 if((pEnableRemote == NULL) || (pSendIFC == NULL) ||
431 (pThreadIbsta == NULL) || (pThreadIberr == NULL) || (pThreadIbcnt == NULL) ||
432 (pibclr == NULL) || (pibconfig == NULL) || (pibdev == NULL) || (pibonl == NULL) ||
433 (pibrd == NULL) || (pibrsp == NULL) || (pibwrt == NULL)) {
441 static int free_ni4882dll() {
442 if(ni4882dll != NULL)
443 FreeLibrary(ni4882dll);
446 #endif // DIRECT_ENTRY_NI488