/** Find all listeners. @param boardId Id/number of board for listing @return Pointer to list of Addr4882_t containing numbers of all listening devices, this list should be free by delete[] */ Addr4882_t *GPIB::findLstn(int boardId) { /** valid GIPB addresses: 0 - master 1-30 - valid device addresses 31 - all devices? */ const Addr4882_t PIDsLstnTest[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, NOADDR }; Addr4882_t *PIDsLstn; SendIFC(boardId); if ((Ibsta() & ERR)) return NULL; PIDsLstn = new Addr4882_t[ARRAY_SIZE(PIDsLstnTest)]; ::FindLstn(boardId, PIDsLstnTest, PIDsLstn, ARRAY_SIZE(PIDsLstnTest) - 1); if (Ibsta() & ERR) { delete[] PIDsLstn; return NULL; } PIDsLstn[Ibcnt()] = NOADDR; return PIDsLstn; }
/** Send command (text) to device. @param buf Buffer with text to send. @param len Number of bytes to send. @return On success returns 0, on error -1. */ int GPIB::sendCmd(const char *buf, size_t len) { ibwrt(devId, buf, len); if (Ibsta() & ERR || Ibcnt() != len) return -1; return 0; }
/*************************************************************************** * * Name: WriteCommand * Arguments: device - handle to the opened device * cmd - command string to be written * Returns: FALSE if succesful, else TRUE * * Send a command to a GPIB device and checks for error. * ***************************************************************************/ int WriteCommand (int device, const char *cmd) { size_t cmdlength; int ReturnVal; /**/ cmdlength = strlen (cmd); ibwrt (device, cmd, cmdlength); if (Ibcnt()!=cmdlength || Ibsta()&ERR) ReturnVal = TRUE; else ReturnVal = FALSE; return (ReturnVal); }
/*************************************************************************** * * Name: ReadValue * Arguments: device - handle to the opened device * buffer - buffer to read into * bufsize - size of buffer (in bytes) * Returns: FALSE if succesful, TRUE if error * * Reads a string from a GPIB device and checks for errors. If no * errors it terminates the string. * ***************************************************************************/ int ReadValue(int device, char *buffer, size_t bufsize) { int rtrnval; /**/ ibrd (device,buffer,bufsize); if (Ibcnt()==bufsize || Ibcnt()==0 || Ibsta()&ERR) rtrnval = TRUE; else { buffer[Ibcnt()-1] = '\0'; /* Terminate string */ rtrnval = FALSE; } return (rtrnval); }
/** Open device connected to GPIB. @param boardNum Id/number of board to open. @param PAD Primary device address in range 1-30 @see PIDsLstnTest @param SAD Secondary device address. */ int GPIB::open(int boardNum, int PAD, int SAD) { SendIFC(boardNum); if ((Ibsta() & ERR) != 0) { return -1; } devId = ibdev(boardNum, PAD, SAD, T1s, 1, 0); if (devId >= 0) { ibclr(devId); ibrsp(devId, &poll); ibtmo(devId, T3s); SendLLO(boardNum); } return devId; }
/** Read response from device. @param buf Pointer to char array to store text data. @param bufLen Lenght of buffer. @return Return number of bytes read, or -1 on error. */ int GPIB::readValue(char *buf, int bufLen) { int len; ibrd(devId, buf, bufLen); len = Ibcnt(); if (len == bufLen || len == 0 || Ibsta() & ERR) return -1; // strip end of string from whitespaces while (isspace(buf[--len])) { if (!len) return -1; } buf[++len] = 0; return len; }
QString clsGpib::sendCommand(QString strCommand, bool hasReturn) { if(!blInit) { init(); } if(!blInit) return ""; strCommand = strCommand.append('\n'); const char *cmd; std::string xx= strCommand.toStdString(); cmd = xx.c_str(); unsigned int cmdLength; cmdLength=strlen(cmd); //ibwrt(device,cmd,cmdLength); Send(0,intAddress,cmd,cmdLength,DABend); if(Ibcnt()!= cmdLength || Ibsta() & ERR) { return ""; } if(!hasReturn) return ""; char buffer[129]; const int bufsize=128; Receive (0,intAddress, buffer, bufsize, STOPend); while ( (ibsta & CMPL+ERR) == 0); if ((Ibcnt()==bufsize) || (Ibcnt()==0 )|| (Ibsta()&ERR)) return ""; else { buffer[Ibcnt()-1] = '\0'; /* Terminate string */ } QString str= QString(buffer); if(!str.isEmpty()) //在前一段时间发现,6500的返回值前面多了一个‘N’这个非常奇怪。 { if(str.at(0) =='N') str= str.remove(0,1); } // qDebug()<< str; return str; }
/************************************************************************ * * Name: MainMessageHandler * * Arguments: hWnd - handle to the window * Message - message that needs to be handled * hParam - message parameter * lParam - message parameter * * This is the message dispatcher that gets called whenever Windows * sends a message to this window. WinMain started up a timer that * sends a message every 1/2 sec. When the message (WM_TIMER)is received * by this routine, it reads the A/D and stores the values in ADValues[]. * It also causes a screen update which eill automatically generate a * WM_PAINT message. The WM_PAINT handler takes care of converting the * raw A/D values to voltages and printing them in the Window. * ************************************************************************/ LRESULT CALLBACK MainMessageHandler(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { HDC hDC; /* handle for the display device */ PAINTSTRUCT ps; /* holds PAINT information */ TEXTMETRIC tm; /* holds TEXT information */ HFONT hfnt, hOldFont; static HRGN hRgn; /* Rectangle region Handles */ static int CharWidth, CharHeight, device; int x, y; static char IDString[BUF_SIZE+1], VoltsString[BUF_SIZE+1]; char OutString[BUF_SIZE+10], ErrString[80]; /**/ switch (Message) { /* Windows Message Loop */ case WM_CREATE: hDC = GetDC(hWndMain); /* Get the device context for window */ hfnt = GetStockObject(ANSI_FIXED_FONT); hOldFont = SelectObject(hDC, hfnt); GetTextMetrics(hDC, &tm); /* From context, get size of font */ CharWidth = tm.tmAveCharWidth; CharHeight = tm.tmHeight + tm.tmExternalLeading; ReleaseDC(hWndMain, hDC); hRgn = CreateRectRgn(0,0, CharWidth*30, CharHeight*12); device = InitMeter(); /* Init voltmeter */ if (device >= 0) { if (!WriteCommand (device, ID_CMD)) /* get ID string */ ReadValue (device, IDString, BUF_SIZE); } else { if (Iberr() == EDVR) sprintf (ErrString,"Could not find the device at PAD %s.\n ",PAD); else if (Iberr() == ENEB) sprintf (ErrString,"GPIB board is not responding."); else sprintf (ErrString,"Error #%d while opening device at PAD %s.\n", Iberr(), PAD); MessageBox(NULL, ErrString, NULL, MB_ICONEXCLAMATION); exit (1); } break; case WM_TIMER: /* All Timer Events Processed Here */ if (!WriteCommand (device, MEASURE_CMD)) ReadValue (device, VoltsString, BUF_SIZE); InvalidateRgn(hWndMain, hRgn, FALSE); /* Force screen update */ break; case WM_PAINT: /* Repaint client area of window */ hDC = BeginPaint(hWndMain, &ps); hfnt = GetStockObject(ANSI_FIXED_FONT); hOldFont = SelectObject(hDC, hfnt); x = CharWidth * 2; /* Position cursor within window */ y = CharHeight; /* One line down and 2 chars in */ sprintf (OutString,"ID = %s ", IDString); TextOut(hDC, x, y, OutString, strlen (OutString)); y += CharHeight*2; /* Print current count */ sprintf (OutString,"Voltage= %s ", VoltsString); TextOut(hDC, x, y, OutString, strlen (OutString)); y += CharHeight*2; /* Print status */ sprintf (OutString,"ibsta= %4x (HEX)", Ibsta()); TextOut(hDC, x, y, OutString, strlen (OutString)); y += CharHeight; /* Print error */ sprintf (OutString,"iberr= %4d (DEC)", Iberr()); TextOut(hDC, x, y, OutString, strlen (OutString)); y += CharHeight; /* Print count */ sprintf (OutString,"ibcnt= %4d (DEC)", Ibcnt()); TextOut(hDC, x, y, OutString, strlen (OutString)); SetTextAlign(hDC, TA_LEFT | TA_TOP); if (hOldFont) { SelectObject(hDC, hOldFont); hOldFont = NULL; } EndPaint(hWndMain, &ps); break; case WM_CLOSE: /* Close the window */ KillTimer (hWnd, TIMER_NUM); /* Stop the timer */ DestroyWindow(hWnd); if (hWnd == hWndMain) PostQuitMessage(0); /* Send message to Quit application */ break; default: return (DefWindowProc(hWnd, Message, wParam, lParam)); } return (0l); }