📄 gpibrw.c
字号:
/*---------------------------------------------------------------------------*/
/* */
/* FILE: gpibrw.c */
/* */
/* PURPOSE: This example illustrates how to use the GPIB Library to */
/* communicate with a GPIB device. */
/* */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Include files */
/*---------------------------------------------------------------------------*/
#include <cvirte.h>
#include <utility.h>
#include <ansi_c.h>
#include <gpib.h>
#include <formatio.h>
#include <userint.h>
#include "gpibrw.h"
/*---------------------------------------------------------------------------*/
/* Defines */
/*---------------------------------------------------------------------------*/
#define WRITE 0
#define READ 1
#define TRUE 1
#define FALSE 0
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
int pnl_handle, device;
int address;
/*---------------------------------------------------------------------------*/
/* Internal function prototypes */
/*---------------------------------------------------------------------------*/
void setup_gpib (void);
int send_board_commands (void);
/*---------------------------------------------------------------------------*/
/* This is the application's entry-point. */
/*---------------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
DisableBreakOnLibraryErrors ();
address = 0;
setup_gpib (); /* get the gpib address of the device */
if (address == 0)
/* no device is connected so we'll send some board commands */
send_board_commands();
else
{
device = ibdev (0, address, NO_SAD, T10s, 1, 0);
if ((pnl_handle = LoadPanel (0, "gpibrw.uir", PNL)) < 0)
return -1;
SetCtrlAttribute (pnl_handle, PNL_STATUS_MSG, ATTR_VISIBLE, 0);
DisplayPanel (pnl_handle);
RunUserInterface ();
DiscardPanel (pnl_handle);
}
CloseCVIRTE ();
return 0;
}
/*---------------------------------------------------------------------------*/
/* Do the action... We'll either write or read depending on the user's */
/* currently specified option. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK Go (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
static int func, numToRead;
static char write_buffer[100], read_buffer[2000];
if (event == EVENT_COMMIT)
{
GetCtrlVal (pnl_handle, PNL_GPIB_FUNC, &func);
switch (func)
{
case WRITE:
GetCtrlVal (pnl_handle, PNL_MESSAGE, write_buffer);
SetCtrlAttribute (pnl_handle, PNL_STATUS_MSG, ATTR_VISIBLE, 1);
ProcessDrawEvents();
ibwrt (device, write_buffer, strlen(write_buffer));
SetCtrlAttribute (pnl_handle, PNL_STATUS_MSG, ATTR_VISIBLE, 0);
ProcessDrawEvents();
if (ibsta & 0x8000)
{
SetCtrlVal (pnl_handle, PNL_STATUS, 1);
SetCtrlVal (pnl_handle, PNL_ERR, iberr);
}
else
{
SetCtrlVal (pnl_handle, PNL_STATUS, 0);
SetCtrlVal (pnl_handle, PNL_ERR, 0);
}
break;
case READ:
GetCtrlVal (pnl_handle, PNL_READ_BYTES, &numToRead);
SetCtrlAttribute (pnl_handle, PNL_STATUS_MSG, ATTR_VISIBLE, 1);
ProcessDrawEvents();
ibrd (device, read_buffer, numToRead);
SetCtrlAttribute (pnl_handle, PNL_STATUS_MSG, ATTR_VISIBLE, 0);
ProcessDrawEvents();
if (ibsta & 0x8000)
{
SetCtrlVal (pnl_handle, PNL_STATUS, 1);
SetCtrlVal (pnl_handle, PNL_ERR, iberr);
}
else
{
SetCtrlVal (pnl_handle, PNL_STATUS, 0);
SetCtrlVal (pnl_handle, PNL_ERR, 0);
}
ResetTextBox (pnl_handle, PNL_DATA, "");
InsertTextBoxLine (pnl_handle, PNL_DATA, 0, read_buffer);
FillBytes (read_buffer, 0, numToRead, 0x0);
break;
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Quit the application. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK Quit (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
if (event == EVENT_COMMIT)
{
QuitUserInterface (0);
}
return(0);
}
/*---------------------------------------------------------------------------*/
/* This function will pop a new panel which asks the user for a GPIB address */
/* to communicate with. Note the use of GetUserEvent -- it can be helpful */
/* in this type of context to implement a modal dialog. */
/*---------------------------------------------------------------------------*/
void setup_gpib (void)
{
int setup_panel;
int end_loop;
int which_panel, which_control;
if ((setup_panel = LoadPanel (0, "gpibrw.uir", SETUP)) >= 0)
{
DisplayPanel (setup_panel);
end_loop = FALSE;
while (end_loop == FALSE)
{
GetUserEvent (0, &which_panel, &which_control);
if (which_panel == setup_panel && which_control == SETUP_CONTINUE)
{
GetCtrlVal (setup_panel, SETUP_ADDRESS, &address);
end_loop = TRUE;
}
}
DiscardPanel (setup_panel);
}
}
/*---------------------------------------------------------------------------*/
/* Pop up a panel to allow the user to send commands to the GPIB board. */
/*---------------------------------------------------------------------------*/
int send_board_commands(void)
{
int board;
int board_panel;
int which_panel, which_control;
int end_loop;
if ((board_panel = LoadPanel (0, "gpibrw.uir", BOARD)) < 0)
return -1;
DisplayPanel (board_panel);
end_loop = FALSE;
while (end_loop == FALSE)
{
GetUserEvent (0, &which_panel, &which_control);
if (which_panel == board_panel && which_control == BOARD_SEND_1)
{
ResetTextBox (board_panel, BOARD_COMMAND, "");
SetCtrlVal (board_panel, BOARD_COMMAND, "ibfind (\"gpib0\")");
board = ibfind ("gpib0");
if (ibsta & 0x8000)
{
SetCtrlVal (board_panel, BOARD_STATUS, 1);
SetCtrlVal (board_panel, BOARD_ERR, iberr);
}
else
{
SetCtrlVal (board_panel, BOARD_STATUS, 0);
SetCtrlVal (board_panel, BOARD_ERR, 0);
}
SetCtrlAttribute (board_panel, BOARD_SEND_2, ATTR_DIMMED, 0);
}
if (which_panel == board_panel && which_control == BOARD_SEND_2)
{
ResetTextBox (board_panel, BOARD_COMMAND, "");
SetCtrlVal (board_panel, BOARD_COMMAND, "ibsic");
ibsic(board);
if (ibsta & 0x8000)
{
SetCtrlVal (board_panel, BOARD_STATUS, 1);
SetCtrlVal (board_panel, BOARD_ERR, iberr);
}
else
{
SetCtrlVal (board_panel, BOARD_STATUS, 0);
SetCtrlVal (board_panel, BOARD_ERR, 0);
}
}
if (which_panel == board_panel && which_control == BOARD_QUIT)
{
DiscardPanel(board_panel);
end_loop = TRUE;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -