29typedef long Device_Link;
30typedef long Device_Flags;
31typedef long Device_ErrorCode;
34 Device_ErrorCode error;
47 Device_ErrorCode error;
67 Device_ErrorCode error;
83 Device_ErrorCode error;
93#define device_write 11
95#define destroy_link 23
97#define VXI11_DEFAULT_TIMEOUT 10000
98#define VXI11_READ_TIMEOUT 2000
99#define VXI11_CLIENT CLIENT
100#define VXI11_LINK Create_LinkResp
101#define VXI11_MAX_CLIENTS 256
102#define VXI11_NULL_READ_RESP 50
105#define VXI11_NULL_WRITE_RESP 51
109 VXI11_CLIENT *client;
114#define DEVICE_CORE 0x0607AF
115#define DEVICE_CORE_VERSION 1
116#define RCV_END_BIT 0x04
117#define RCV_CHR_BIT 0x02
120char VXI11_IP_ADDRESS[VXI11_MAX_CLIENTS][20];
121CLIENT *VXI11_CLIENT_ADDRESS[VXI11_MAX_CLIENTS];
122int VXI11_DEVICE_NO = 0;
123int VXI11_LINK_COUNT[VXI11_MAX_CLIENTS];
125static struct timeval TIMEOUT = { 25, 0 };
138BOOL sc(
char *con,
char *var);
140int vxi11_open_device(
const char *ip,
142int vxi11_open_device(
const char *ip,
145int vxi11_open_device(
const char *ip,
149int vxi11_open_link(
const char *ip,
153int vxi11_send(
CLINK *clink,
155int vxi11_send(
CLINK *clink,
158int vxi11_send(CLIENT *client,
161int vxi11_send(CLIENT *client,
165int vxi11_close_device(
const char *ip,
167int vxi11_close_device(
const char *ip,
170int vxi11_close_link(
const char *ip,
173double vxi11_obtain_double_value(
CLINK *clink,
175double vxi11_obtain_double_value(
CLINK *clink,
177 unsigned long timeout);
178long vxi11_send_and_receive(
CLINK *clink,
181 unsigned long buf_len,
182 unsigned long timeout);
183long vxi11_receive(
CLINK *clink,
186long vxi11_receive(CLIENT *client,
190 unsigned long timeout);
191long vxi11_receive(
CLINK *clink,
194 unsigned long timeout);
195long vxi11_obtain_long_value(
CLINK *clink,
197 unsigned long timeout);
198long vxi11_obtain_long_value(
CLINK *clink,
200long vxi11_receive_data_block(
CLINK *clink,
203 unsigned long timeout);
211enum clnt_stat destroy_link_1(Device_Link *argp,
218bool_t xdr_Create_LinkParms (XDR *xdrs,
220bool_t xdr_Create_LinkResp (XDR *xdrs,
222bool_t xdr_Device_ErrorCode (XDR *xdrs,
223 Device_ErrorCode *objp);
224bool_t xdr_Device_Link (XDR *xdrs,
226bool_t xdr_Device_WriteParms (XDR *xdrs,
228bool_t xdr_Device_WriteResp (XDR *xdrs,
230bool_t xdr_Device_Flags (XDR *xdrs,
232bool_t xdr_Device_Error (XDR *xdrs,
234bool_t xdr_Device_ReadParms (XDR *xdrs,
236bool_t xdr_Device_ReadResp (XDR *xdrs,
239#define BUF_LEN 1000000
241int main(
int argc,
char **argv)
243 static char *progname;
244 static char *serverIP;
254 char outputbuf[BUF_LEN];
262 if(sc(argv[index],(
char*)
"-ip")||sc(argv[index],(
char*)
"-ip_address")||sc(argv[index],(
char*)
"-IP")){
263 serverIP = argv[++index];
267 if(sc(argv[index],(
char*)
"-command")||sc(argv[index],(
char*)
"-c")||sc(argv[index],(
char*)
"-comm")){
268 snprintf(comm,256,
"%s",argv[++index]);
275 if(got_ip==FALSE||got_comm==FALSE){
276 printf(
"%s: sends commands to an Agilent scope via ethernet\n",progname);
277 printf(
"Run using %s [arguments]\n\n",progname);
278 printf(
"REQUIRED ARGUMENTS:\n");
279 printf(
"-ip -ip_address -IP : IP address of scope (eg 128.243.74.232)\n");
280 printf(
"-c -command -comm : command or query to send\n\n");
281 printf(
"DOCUMENTATION:\n");
282 printf(
"http://cp.literature.agilent.com/litweb/pdf/54855-97017.pdf\n");
286 if (vxi11_open_device(serverIP,clink) != 0) {
287 printf(
"Quitting...\n");
291 vxi11_send(clink, comm);
292 if (strstr(comm,
"?") != 0) {
293 bytes_returned = vxi11_receive(clink, outputbuf, BUF_LEN);
294 vxi11_close_device(serverIP,clink);
296 if (bytes_returned > 0) {
297 printf(
"%s",outputbuf);
298 }
else if (bytes_returned == -15) {
299 printf(
"Error: Nothing received after sending scope command %s\n", comm);
303 vxi11_close_device(serverIP,clink);
308BOOL sc(
char *con,
char *var) {
309 if(strcmp(con,var)==0){
315int vxi11_open_device(
const char *ip,
CLINK *clink) {
317 strncpy(device,
"inst0",6);
318 return vxi11_open_device(ip, clink, device);
321int vxi11_open_device(
const char *ip, CLIENT **client, VXI11_LINK **link,
char *device) {
324 *client = clnt_create((
char*)ip, DEVICE_CORE, DEVICE_CORE_VERSION,
"tcp");
326 *client = clnt_create(ip, DEVICE_CORE, DEVICE_CORE_VERSION,
"tcp");
329 if (*client == NULL) {
331 clnt_pcreateerror((
char*)ip);
333 clnt_pcreateerror(ip);
338 return vxi11_open_link(ip, client, link, device);
341int vxi11_open_device(
const char *ip,
CLINK *clink,
char *device) {
346 for (l=0; l<VXI11_MAX_CLIENTS; l++){
347 if (strcmp(ip,VXI11_IP_ADDRESS[l]) == 0 ) {
353 if (VXI11_DEVICE_NO >= VXI11_MAX_CLIENTS) {
354 printf(
"Error: maximum of %d clients allowed\n",VXI11_MAX_CLIENTS);
355 ret = -VXI11_MAX_CLIENTS;
357 ret = vxi11_open_device(ip, &(clink->client), &(clink->link), device);
358 strncpy(VXI11_IP_ADDRESS[VXI11_DEVICE_NO],ip,20);
359 VXI11_CLIENT_ADDRESS[VXI11_DEVICE_NO] = clink->client;
360 VXI11_LINK_COUNT[VXI11_DEVICE_NO]=1;
364 clink->client = VXI11_CLIENT_ADDRESS[device_no];
365 ret = vxi11_open_link(ip, &(clink->client), &(clink->link), device);
366 VXI11_LINK_COUNT[device_no]++;
371int vxi11_open_link(
const char *ip, CLIENT **client, VXI11_LINK **link,
char *device) {
376 link_parms.clientId = (long) *client;
377 link_parms.lockDevice = 0;
378 link_parms.lock_timeout = VXI11_DEFAULT_TIMEOUT;
379 link_parms.device = device;
383 if (create_link_1(&link_parms, *link, *client) != RPC_SUCCESS) {
385 clnt_perror(*client, (
char*)ip);
387 clnt_perror(*client, ip);
396 return (clnt_call(clnt, create_link,
397 (xdrproc_t) xdr_Create_LinkParms, (caddr_t) argp,
398 (xdrproc_t) xdr_Create_LinkResp, (caddr_t) clnt_res,
404#if defined(SOLARIS) && !defined(_LP64)
407 register int32_t *buf;
410 if (xdrs->x_op == XDR_ENCODE) {
411 buf = XDR_INLINE (xdrs, 3 * BYTES_PER_XDR_UNIT);
413 if (!xdr_long (xdrs, &objp->clientId))
415 if (!xdr_bool (xdrs, &objp->lockDevice))
417 if (!xdr_u_long (xdrs, &objp->lock_timeout))
421 IXDR_PUT_INT32(buf, objp->clientId);
422 IXDR_PUT_BOOL(buf, objp->lockDevice);
423 IXDR_PUT_U_INT32(buf, objp->lock_timeout);
425 if (!xdr_string (xdrs, &objp->device, ~0))
428 }
else if (xdrs->x_op == XDR_DECODE) {
429 buf = XDR_INLINE (xdrs, 3 * BYTES_PER_XDR_UNIT);
431 if (!xdr_long (xdrs, &objp->clientId))
433 if (!xdr_bool (xdrs, &objp->lockDevice))
435 if (!xdr_u_long (xdrs, &objp->lock_timeout))
439 objp->clientId = IXDR_GET_INT32(buf);
440 objp->lockDevice = IXDR_GET_BOOL(buf);
441 objp->lock_timeout = IXDR_GET_U_INT32(buf);
443 if (!xdr_string (xdrs, &objp->device, ~0))
448 if (!xdr_long (xdrs, &objp->clientId))
450 if (!xdr_bool (xdrs, &objp->lockDevice))
452 if (!xdr_u_long (xdrs, &objp->lock_timeout))
454 if (!xdr_string (xdrs, &objp->device, ~0))
461 if (!xdr_Device_ErrorCode (xdrs, &objp->error))
463 if (!xdr_Device_Link (xdrs, &objp->lid))
465 if (!xdr_u_short (xdrs, &objp->abortPort))
467 if (!xdr_u_long (xdrs, &objp->maxRecvSize))
472bool_t xdr_Device_ErrorCode (XDR *xdrs, Device_ErrorCode *objp)
474 if (!xdr_long (xdrs, objp))
479bool_t xdr_Device_Link (XDR *xdrs, Device_Link *objp)
481 if (!xdr_long (xdrs, objp))
486int vxi11_send(
CLINK *clink,
const char *cmd) {
487 return vxi11_send(clink, cmd, strlen(cmd));
490int vxi11_send(
CLINK *clink,
const char *cmd,
unsigned long len) {
491 return vxi11_send(clink->client, clink->link, cmd, len);
494int vxi11_send(CLIENT *client, VXI11_LINK *link,
const char *cmd) {
495 return vxi11_send(client, link, cmd, strlen(cmd));
498int vxi11_send(CLIENT *client, VXI11_LINK *link,
const char *cmd,
unsigned long len) {
500 int bytes_left = (int)len;
503 send_cmd =
new char[len];
504 memcpy(send_cmd, cmd, len);
506 write_parms.lid = link->lid;
507 write_parms.io_timeout = VXI11_DEFAULT_TIMEOUT;
508 write_parms.lock_timeout = VXI11_DEFAULT_TIMEOUT;
515 memset(&write_resp, 0,
sizeof(write_resp));
517 if ((
unsigned int)bytes_left <= link->maxRecvSize) {
518 write_parms.flags = 8;
519 write_parms.data.data_len = bytes_left;
522 write_parms.flags = 0;
527 if (link->maxRecvSize > 0) {
528 write_parms.data.data_len = link->maxRecvSize;
531 write_parms.data.data_len = 4096;
534 write_parms.data.data_val = send_cmd + (len - bytes_left);
536 if(device_write_1(&write_parms, &write_resp, client) != RPC_SUCCESS) {
538 return -VXI11_NULL_WRITE_RESP;
544 if (write_resp . error != 0) {
545 printf(
"vxi11_user: write error: %d\n",(
int)(write_resp . error));
547 return -(write_resp . error);
549 bytes_left -= write_resp . size;
550 }
while (bytes_left > 0);
558 return (clnt_call(clnt, device_write,
559 (xdrproc_t) xdr_Device_WriteParms, (caddr_t) argp,
560 (xdrproc_t) xdr_Device_WriteResp, (caddr_t) clnt_res,
566 if (!xdr_Device_Link (xdrs, &objp->lid))
568 if (!xdr_u_long (xdrs, &objp->io_timeout))
570 if (!xdr_u_long (xdrs, &objp->lock_timeout))
572 if (!xdr_Device_Flags (xdrs, &objp->flags))
574 if (!xdr_bytes (xdrs, (
char **)&objp->data.data_val, (u_int *) &objp->data.data_len, ~0))
581 if (!xdr_Device_ErrorCode (xdrs, &objp->error))
583 if (!xdr_u_long (xdrs, &objp->size))
588bool_t xdr_Device_Flags (XDR *xdrs, Device_Flags *objp)
590 if (!xdr_long (xdrs, objp))
595int vxi11_close_device(
const char *ip,
CLINK *clink) {
600 for (l=0; l<VXI11_MAX_CLIENTS; l++){
601 if (strcmp(ip,VXI11_IP_ADDRESS[l]) == 0 ) {
606 if (device_no == -1) {
607 printf(
"vxi11_close_device: error: I have no record of you ever opening device\n");
608 printf(
" with IP address %s\n",ip);
613 if (VXI11_LINK_COUNT[device_no] > 1 ) {
614 ret = vxi11_close_link(ip,clink->client, clink->link);
615 VXI11_LINK_COUNT[device_no]--;
620 ret = vxi11_close_device(ip, clink->client, clink->link);
626int vxi11_close_device(
const char *ip, CLIENT *client, VXI11_LINK *link) {
629 ret = vxi11_close_link(ip, client, link);
631 clnt_destroy(client);
636int vxi11_close_link(
const char *ip, CLIENT *client, VXI11_LINK *link) {
638 memset(&dev_error, 0,
sizeof(dev_error));
640 if (destroy_link_1(&link->lid, &dev_error, client) != RPC_SUCCESS) {
642 clnt_perror(client,(
char*)ip);
644 clnt_perror(client,ip);
652enum clnt_stat destroy_link_1(Device_Link *argp,
Device_Error *clnt_res, CLIENT *clnt)
654 return (clnt_call(clnt, destroy_link,
655 (xdrproc_t) xdr_Device_Link, (caddr_t) argp,
656 (xdrproc_t) xdr_Device_Error, (caddr_t) clnt_res,
662 if (!xdr_Device_ErrorCode (xdrs, &objp->error))
667double vxi11_obtain_double_value(
CLINK *clink,
const char *cmd) {
668 return vxi11_obtain_double_value(clink, cmd, VXI11_READ_TIMEOUT);
671double vxi11_obtain_double_value(
CLINK *clink,
const char *cmd,
unsigned long timeout) {
675 if (vxi11_send_and_receive(clink, cmd, buf, 50, timeout) != 0) {
676 printf(
"Returning 0.0\n");
679 val = strtod(buf, (
char **)NULL);
683long vxi11_send_and_receive(
CLINK *clink,
const char *cmd,
char *buf,
unsigned long buf_len,
unsigned long timeout) {
687 ret = vxi11_send(clink, cmd);
689 if (ret != -VXI11_NULL_WRITE_RESP) {
690 printf(
"Error: vxi11_send_and_receive: could not send cmd.\n");
691 printf(
" The function vxi11_send returned %d. ",ret);
694 else printf(
"(Info: VXI11_NULL_WRITE_RESP in vxi11_send_and_receive, resending query)\n");
697 bytes_returned = vxi11_receive(clink, buf, buf_len, timeout);
698 if (bytes_returned <= 0) {
699 if (bytes_returned >-VXI11_NULL_READ_RESP) {
700 printf(
"Error: vxi11_send_and_receive: problem reading reply.\n");
701 printf(
" The function vxi11_receive returned %ld. ",bytes_returned);
704 else printf(
"(Info: VXI11_NULL_READ_RESP in vxi11_send_and_receive, resending query)\n");
706 }
while (bytes_returned == -VXI11_NULL_READ_RESP || ret == -VXI11_NULL_WRITE_RESP);
710long vxi11_receive(CLIENT *client, VXI11_LINK *link,
char *buffer,
unsigned long len,
unsigned long timeout) {
715 read_parms.lid = link->lid;
716 read_parms.requestSize = len;
717 read_parms.io_timeout = timeout;
718 read_parms.lock_timeout = timeout;
719 read_parms.flags = 0;
720 read_parms.termChar = 0;
723 memset(&read_resp, 0,
sizeof(read_resp));
725 read_resp.data.data_val = buffer + curr_pos;
726 read_parms.requestSize = len - curr_pos;
728 if(device_read_1(&read_parms, &read_resp, client) != RPC_SUCCESS) {
729 return -VXI11_NULL_READ_RESP;
733 if (read_resp . error != 0) {
743 printf(
"vxi11_user: read error: %d\n",(
int)(read_resp . error));
744 return -(read_resp . error);
747 if((
unsigned long)(curr_pos + read_resp . data.data_len) <= len) {
748 curr_pos += read_resp . data.data_len;
750 if( (read_resp.reason & RCV_END_BIT) || (read_resp.reason & RCV_CHR_BIT) ) {
753 else if( (
unsigned long)curr_pos == len ) {
754 printf(
"xvi11_user: read error: buffer too small. Read %ld bytes without hitting terminator.\n", curr_pos );
762long vxi11_receive(
CLINK *clink,
char *buffer,
unsigned long len) {
763 return vxi11_receive(clink, buffer, len, VXI11_READ_TIMEOUT);
766long vxi11_receive(
CLINK *clink,
char *buffer,
unsigned long len,
unsigned long timeout) {
767 return vxi11_receive(clink->client, clink->link, buffer, len, timeout);
772 return (clnt_call(clnt, device_read,
773 (xdrproc_t) xdr_Device_ReadParms, (caddr_t) argp,
774 (xdrproc_t) xdr_Device_ReadResp, (caddr_t) clnt_res,
780#if defined(SOLARIS) && !defined(_LP64)
783 register int32_t *buf;
786 if (xdrs->x_op == XDR_ENCODE) {
787 if (!xdr_Device_Link (xdrs, &objp->lid))
789 buf = XDR_INLINE (xdrs, 3 * BYTES_PER_XDR_UNIT);
791 if (!xdr_u_long (xdrs, &objp->requestSize))
793 if (!xdr_u_long (xdrs, &objp->io_timeout))
795 if (!xdr_u_long (xdrs, &objp->lock_timeout))
799 IXDR_PUT_U_INT32(buf, objp->requestSize);
800 IXDR_PUT_U_INT32(buf, objp->io_timeout);
801 IXDR_PUT_U_INT32(buf, objp->lock_timeout);
803 if (!xdr_Device_Flags (xdrs, &objp->flags))
805 if (!xdr_char (xdrs, &objp->termChar))
808 }
else if (xdrs->x_op == XDR_DECODE) {
809 if (!xdr_Device_Link (xdrs, &objp->lid))
811 buf = XDR_INLINE (xdrs, 3 * BYTES_PER_XDR_UNIT);
813 if (!xdr_u_long (xdrs, &objp->requestSize))
815 if (!xdr_u_long (xdrs, &objp->io_timeout))
817 if (!xdr_u_long (xdrs, &objp->lock_timeout))
821 objp->requestSize = IXDR_GET_U_INT32(buf);
822 objp->io_timeout = IXDR_GET_U_INT32(buf);
823 objp->lock_timeout = IXDR_GET_U_INT32(buf);
825 if (!xdr_Device_Flags (xdrs, &objp->flags))
827 if (!xdr_char (xdrs, &objp->termChar))
832 if (!xdr_Device_Link (xdrs, &objp->lid))
834 if (!xdr_u_long (xdrs, &objp->requestSize))
836 if (!xdr_u_long (xdrs, &objp->io_timeout))
838 if (!xdr_u_long (xdrs, &objp->lock_timeout))
840 if (!xdr_Device_Flags (xdrs, &objp->flags))
842 if (!xdr_char (xdrs, &objp->termChar))
849 if (!xdr_Device_ErrorCode (xdrs, &objp->error))
851 if (!xdr_long (xdrs, &objp->reason))
853 if (!xdr_bytes (xdrs, (
char **)&objp->data.data_val, (u_int *) &objp->data.data_len, ~0))
858long vxi11_obtain_long_value(
CLINK *clink,
const char *cmd,
unsigned long timeout) {
861 if (vxi11_send_and_receive(clink, cmd, buf, 50, timeout) != 0) {
862 printf(
"Returning 0\n");
865 return strtol(buf, (
char **)NULL, 10);
869long vxi11_obtain_long_value(
CLINK *clink,
const char *cmd) {
870 return vxi11_obtain_long_value(clink, cmd, VXI11_READ_TIMEOUT);
873long vxi11_receive_data_block(
CLINK *clink,
char *buffer,
unsigned long len,
unsigned long timeout) {
876 unsigned long necessary_buffer_size;
880 unsigned long returned_bytes;
883 necessary_buffer_size=len+12;
884 in_buffer=
new char[necessary_buffer_size];
885 ret=vxi11_receive(clink, in_buffer, necessary_buffer_size, timeout);
886 if (ret < 0)
return ret;
887 if (in_buffer[0] !=
'#') {
888 printf(
"vxi11_user: data block error: data block does not begin with '#'\n");
889 printf(
"First 20 characters received were: '");
891 printf(
"%c",in_buffer[l]);
898 sscanf(in_buffer,
"#%1d",&ndigits);
902 sprintf(scan_cmd,
"#%%1d%%%dlu",ndigits);
903 sscanf(in_buffer,scan_cmd,&ndigits,&returned_bytes);
904 memcpy(buffer, in_buffer+(ndigits+2), returned_bytes);
906 return (
long) returned_bytes;