-
Notifications
You must be signed in to change notification settings - Fork 0
/
vxi11.cpp
2372 lines (2102 loc) · 85.3 KB
/
vxi11.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ***************************************************************************
// vxi11.cpp - Implementation of the Vxi11 class for libvxi11.so library
// VXI-11 protocol for instrument communication via TCP/IP
//
// Written by Eddie Lew, Lew Engineering
// Copyright (C) 2020 Eddie Lew
//
// Edit history:
//
// 01-21-24 - Added support for Linux in addition to MacOS.
// read(): Added more information in error messages.
// 12-19-23 - timeout(): Prevent crash if called before open() or after close()
// is called.
// Added device address to most error messages.
// 10-14-23 - Vxi11 constructor, open(): Added comment for RS-232 use.
// timeout(), open(): Set RPC timeout to the user specified timeout
// time+10s. Before it was always set to 120 s, which is not
// enough for slow devices.
// 01-17-23 - read(): Fix issue where read was terminating on line feed when
// user set the read termination character to non-line feed on
// E5810A RS-232 port, causing read to end prematurely.
// Fixed various typos in comments.
// 10-02-22 - Added log_err_ena() to control whether error messages are logged
// to stderr or not. Defaults to enable logging.
// 09-23-22 - Added support for the abort channel, added abort() function.
// 09-17-22 - Added support for SRQ (service request) interrupt callback with
// enable_srq() and srq_callback().
// Support multi-threaded operation by adding mutex around RPC use.
// 08-22-22 - read(): Use read termination method specified by
// read_terminator(). Default is END (EOI for GPIB).
// 08-21-22 - Added error description to error messages.
// Added docmd_*() functions for low level control of
// GPIB/LAN gateways.
// 11-27-21 - read(): Add number of bytes read in error message when END
// indicator is not found.
// 06-10-20 - First released version.
// 05-18-20 - Started file.
// ***************************************************************************
#include "libvxi11.h"
#include "vxi11_rpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <rpc/pmap_clnt.h>
#include <netdb.h>
// Macro to conveniently access __p_client and __p_link members of the class.
// They are defined as void * in the class so that the .h file does not need
// to include the RPC interface.
#define _p_client ((CLIENT *)__p_client)
#define _p_client_abort ((CLIENT *)__p_client_abort)
#define _p_link ((Create_LinkResp *)__p_link)
// Static members to support SRQ callback function
void *Vxi11::_p_pthread_svc_run = 0; // Thread pointer for _fn_svc_run()
void *Vxi11::_p_svcXprt_srq_tcp = 0; // TCP RPC service transport for SRQ
void *Vxi11::_p_svcXprt_srq_udp = 0; // UDP RPC service transport for SRQ
void (*Vxi11::_pfn_srq_callback)(Vxi11 *) = 0; // User callback for SRQ intr
// Error description for each error code from the VXI-11 RPC calls
const char *Vxi11::_as_err_desc[Vxi11::CNT_ERR_DESC_MAX] =
{"", // 0 (no error)
"syntax error", // 1
"", // 2
"device not accessible", // 3
"invalid link identifier", // 4
"parameter error", // 5
"channel not established", // 6
"", // 7
"operation not supported", // 8
"out of resources", // 9
"", // 10
"device locked by another link", // 11
"no lock held by this link", // 12
"", // 13
"", // 14
"I/O timeout", // 15
"", // 16
"I/O error", // 17
"", // 18
"", // 19
"", // 20
"invalid address", // 21
"", // 22
"abort", // 23
"", // 24
"", // 25
"", // 26
"", // 27
"", // 28
"channel already established", // 29
"", // 30
"", // 31
};
bool Vxi11::_b_log_err = true; // Enable error logging to stderr
// ***************************************************************************
// Vxi11Mutex - Class to serialize access VXI-11 RPCs via pthread mutexes
//
// This is needed to properly handle SRQ interrupts and Vxi11 objects
// used in multiple threads.
//
// Create a local instance of this class in each function where a lock on the
// mutex is required. The mutex will be unlocked when that instance goes out
// of scope, such as when the function returns.
// ***************************************************************************
class Vxi11Mutex
{
private:
static pthread_mutex_t mutex; // Mutex to lock & unlock
public:
// Constructor to lock mutex
Vxi11Mutex () {
int err = pthread_mutex_lock (&mutex);
if (err)
Vxi11::log_err ("Vxi11 error: could not lock mutex, error %d", err);
}
// Destructor to unlock mutex
~Vxi11Mutex () {
int err = pthread_mutex_unlock (&mutex);
if (err)
Vxi11::log_err ("Vxi11 error: could not unlock mutex, error %d", err);
}
};
pthread_mutex_t Vxi11Mutex::mutex = PTHREAD_MUTEX_INITIALIZER;
// ***************************************************************************
// Vxi11::log_err - Log errors to stderr if enabled
//
// Parameters:
// 1. s_format - printf style format string to log
// 2. ... - Variable number of parameters, depending on s_format
//
// Returns: None
// ***************************************************************************
void Vxi11::
log_err (const char *s_format, ...)
{
if (!_b_log_err) // Do nothing if logging is disabled
return;
if (!s_format) // Do nothing if null pointer
return;
const int LEN_MSG_MAX = 65535; // Max string length to log
static char s_msg[LEN_MSG_MAX+1];
va_list va; // Process input like printf() does
va_start (va, s_format);
int cnt = vsnprintf (s_msg, LEN_MSG_MAX, s_format, va);
va_end (va);
s_msg[LEN_MSG_MAX] = 0; // Make sure it is terminated
fputs (s_msg, stderr); // Print error message to stderr
}
// ***************************************************************************
// Vxi11 default constructor - Do not connect to device yet
//
// Parameters: None
// Returns: N/A
//
// Notes: Use this constructor if you want to connect to the device at
// a later time using the open() call.
// ***************************************************************************
Vxi11::
Vxi11 (void)
{
_b_valid = 0; // No connection to device
__p_client = 0; // No RPC client yet
__p_link = 0; // No link to device yet
__p_client_abort = 0; // No RPC client for abort channel yet
_s_device_addr[0] = 0; // No device address/name yet
_ui_device_ip_addr = 0; // No device IP address yet
_b_srq_ena = false; // SRQ interrupt not enabled
_b_srq_udp = false; // SRQ interrupt will use TCP, not UDP
_d_timeout = 10.0; // Default timeout in seconds
_timeout_ms = 10000; // Default timeout in milliseconds
read_terminator (-1); // Terminate read with END (EOI line
// for GPIB)
}
// ***************************************************************************
// Vxi11 constructor - Open connection to device
//
// Parameters:
// 1. s_address - Device network address, either a host name or IP
// address in dot notation
//
// 2. s_device - Device name at s_address
//
// May be set to null pointer if device is directly
// connected to the network (an Ethernet or Wifi device); in
// that case "inst0" is used by default.
//
// For GPIB devices connected to a GPIB/LAN gateways, this is
// usually "gpib0,n", where 'n' is the GPIB address number of
// the GPIB device.
//
// For GPIB interfaces (the GPIB/LAN gateway itself), this is
// usually "gpib0".
//
// For RS-232 devices connected to an Agilent/Keysight
// E5810A/B, this is usually "COM1,488".
//
// 3. p_err - Returns error code if given non-zero pointer
// Stores to pointer location 0 = no error
// 1 = error
//
// Notes: If there was an error in the RPC call, that error message will
// be printed to stderr if log_err_ena() is true.
// ***************************************************************************
Vxi11::
Vxi11 (const char *s_address, const char *s_device, int *p_err)
{
_b_valid = 0; // No connection to device
__p_client = 0; // No RPC client yet
__p_link = 0; // No link to device yet
__p_client_abort = 0; // No RPC client for abort channel yet
_s_device_addr[0] = 0; // No device address/name yet
_ui_device_ip_addr = 0; // No device IP address yet
_b_srq_ena = false; // SRQ interrupt not enabled
_b_srq_udp = false; // SRQ interrupt will use TCP, not UDP
_d_timeout = 10.0; // Default timeout in seconds
_timeout_ms = 10000; // Default timeout in milliseconds
read_terminator (-1); // Terminate read with END (EOI line
// for GPIB)
int err = open (s_address, s_device); // Connect to device
if (p_err) // Return error to caller
*p_err = err;
}
// ***************************************************************************
// Vxi11 destructor - Close connection to device if it was open
// ***************************************************************************
Vxi11::
~Vxi11 ()
{
if (_b_valid) // Close connection to device if
close (); // it currently open
}
// ***************************************************************************
// Vxi11::open - Open connection to device
// VXI-11 RPC is "create_link"
//
// Parameters:
// 1. s_address - Device network address, either a host name or IP
// address in dot notation
// 2. s_device - Device name at s_address
//
// May be set to null pointer if device is directly
// connected to the network (an Ethernet or WiFi device); in
// that case "inst0" is used by default.
//
// For GPIB devices connected to a GPIB/LAN gateways, this is
// usually "gpib0,n", where 'n' is the GPIB address number of
// the GPIB device.
//
// For GPIB interfaces (the GPIB/LAN gateway itself), this is
// usually "gpib0".
//
// For RS-232 devices connected to an Agilent/Keysight
// E5810A/B, this is usually "COM1,488".
//
// Returns: 0 = no error
// 1 = error
//
// Notes: If there was an error in the RPC call, that error message will
// be printed to stderr if log_err_ena() is true.
//
// Use this function if the default constructor was used, or if
// re-opening the device after closing it.
// ***************************************************************************
int Vxi11::
open (const char *s_address, const char *s_device)
{
// Cannot open a new connection if one is already open in this instance
if (_b_valid) {
log_err ("Vxi11::open error: connection already open to %s.\n",
_s_device_addr);
return (1);
}
// Check if host name or IP address is not null
if (!s_address) {
log_err ("Vxi11::open error: null address.\n");
return (1);
}
// Use default device name if it is not specified
if (!s_device)
s_device = "inst0"; // According to VXI-11.3 Rule B.1.2
// Store the address and device name so that the user can distinguish
// the Vxi11 object used.
_s_device_addr[255] = 0; // Terminate in case input is too large
strncpy (_s_device_addr, s_address, 255);
strncat (_s_device_addr, ":", 255);
strncat (_s_device_addr, s_device, 255);
// *************************************************************************
// Set up core RPC channel
// *************************************************************************
// Create a client of the RPC functions for device at given address
const char *s_tcp = "tcp";
__p_client = clnt_create ((char *)s_address, DEVICE_CORE,
DEVICE_CORE_VERSION, (char *)s_tcp);
if (!_p_client) { // Exit early if error
const char *s_err = "Vxi11 open error: client creation";
clnt_pcreateerror ((char *)s_err); // Print error message
return (1);
}
// Change underlying RPC timeout from 25s that was set in vxi11_rpc_clnt.c
// to 10 seconds more than the user specified timeout
timeout (_d_timeout);
// Create a link to the device
Create_LinkParms linkParms;
linkParms.clientId = (long)_p_client; // RPC client ID
linkParms.lockDevice = 0; // Do not lock device
linkParms.lock_timeout = _timeout_ms; // Timeout in ms
linkParms.device = (char *)s_device; // Device name
Create_LinkResp *p_link = create_link_1 (&linkParms, _p_client);
if (!p_link) { // Exit early if error
const char *s_err = "Vxi11::open error: link creation";
clnt_perror (_p_client, (char *)s_err); // Print error message
clnt_destroy (_p_client);
return (1);
}
// Copy it to the object, since RPC will lose it on next call
__p_link = (Create_LinkResp *)malloc (sizeof (Create_LinkResp));
if (!_p_link) { // Exit early if error
log_err ("Vxi11::open error: could not allocate memory for %s.\n",
_s_device_addr);
destroy_link_1 (&(p_link->lid), _p_client);
clnt_destroy (_p_client);
return (1);
}
*_p_link = *p_link;
// Get IP address of the device
// This is used later if the abort channel is used
hostent *p_hostent = gethostbyname (s_address);
if (!p_hostent) {
log_err ("Vxi11::open error: could not get device IP address for %s.\n",
_s_device_addr);
destroy_link_1 (&(p_link->lid), _p_client);
clnt_destroy (_p_client);
return (1);
}
_ui_device_ip_addr = *(unsigned int *)(p_hostent->h_addr_list[0]);
_b_valid = 1; // Now have valid connection
return (0);
}
// ***************************************************************************
// Vxi11::close - Close connection to the device
// VXI-11 RPC is "destroy_link"
//
// Parameters: None
//
// Returns: 0 = no error
// ***************************************************************************
int Vxi11::
close (void)
{
if (!_b_valid) // Early return if no connection to
return (0); // the device
// Close SRQ interrupt channel
// Leave RPC service running for SRQ since it is global to all Vxi11 objects
int err = enable_srq (false);
_b_valid = 0; // No connection to device
// Close link to device
Device_Error *p_error = destroy_link_1 (&(_p_link->lid), _p_client);
if (!p_error) {
log_err ("Vxi11::close error: no RPC response for %s.\n", _s_device_addr);
err = 1;
}
// Possible errors
// 0 = no error
// 4 = invalid link identifier
else {
int err_code = int (p_error->error);
if (err_code) {
int idx_err_desc = ((err_code >= 0) && (err_code < CNT_ERR_DESC_MAX)) ?
err_code : 0;
log_err ("Vxi11::close error: destroy_link error %d %s for %s.\n",
err_code, _as_err_desc[idx_err_desc], _s_device_addr);
err = 1;
}
}
free (_p_link);
__p_link = 0;
if (_p_client_abort) {
clnt_destroy (_p_client_abort); // Abort channel
__p_client_abort = 0;
}
// Close RPC client
clnt_destroy (_p_client); // Core (normal) channel
__p_client = 0;
return (err);
}
// ***************************************************************************
// Vxi11::timeout - Set timeout time
//
// Parameters:
// 1. d_timeout - Timeout time, in seconds, for subsequent VXI-11 operations
//
// Returns: None
//
// Notes: Default timeout if this function is not called is 10 seconds.
// ***************************************************************************
void Vxi11::
timeout (double d_timeout)
{
if (d_timeout < 0)
d_timeout = 0;
_d_timeout = d_timeout; // Store timeout in s
_timeout_ms = int (d_timeout * 1000 + 0.5); // Store timeout in ms
// Change underlying RPC timeout 10 seconds longer
if (_p_client) {
struct timeval timeval_timeout = {int (_d_timeout + 10.5), 0};
clnt_control (_p_client, CLSET_TIMEOUT, (char *)(&timeval_timeout));
}
}
// ***************************************************************************
// Vxi11::timeout - Get timeout time
//
// Parameters: None
//
// Returns: Timeout time, in seconds
// ***************************************************************************
double Vxi11::
timeout (void)
{
return (_d_timeout);
}
// ***************************************************************************
// Vxi11::write - Write data to the device
// VXI-11 RPC is "device_write"
//
// Parameters:
// 1. ac_data - Data to send to device
// 2. cnt_data - Number of bytes in ac_data to send
//
// Returns: 0 = no error
// 1 = error
//
// Notes: If Vxi11 object is associated with a GPIB device or GPIB interface
// via a GPIB/LAN gateway, GPIB uses SEND command sequence, with EOI
// on last byte.
// ***************************************************************************
int Vxi11::
write (const char *ac_data, int cnt_data)
{
// Early return if object did not make connection to instrument
if (!_b_valid) {
log_err ("Vxi11::write error: no connection to device.\n");
return (1);
}
// Check input parameters
if ((!ac_data) || (cnt_data < 0)) {
log_err ("Vxi11::write error: invalid parameters for %s.\n",
_s_device_addr);
return (1);
}
if (cnt_data == 0) // No error for sending no data
return (0);
Device_WriteParms writeParms; // To send to device_write RPC call
writeParms.lid = _p_link->lid; // Link ID from create_link RPC call
writeParms.io_timeout = _timeout_ms; // Timeout for I/O in ms
writeParms.lock_timeout = _timeout_ms;// Timeout for lock in ms
// Get max # of bytes to send at a time
int cnt_max = _p_link->maxRecvSize;
if (cnt_max <= 0) // Check for valid max value
cnt_max = 1024; // Default to 1K if not valid
int cnt_left = cnt_data; // Number of bytes left to send
Vxi11Mutex vxi11Mutex; // Lock access until function returns
// Loop sending data to the device, limited to the max allowed at a time
do {
// If remaining bytes to send is less than max
if (cnt_left <= cnt_max) {
writeParms.flags = 8; // Indicate this is the end of the data
writeParms.data.data_len = cnt_left; // Send remaining # of bytes
}
// Remaining bytes more than max, so send max
else {
writeParms.flags = 0; // Not last block of data yet
writeParms.data.data_len=cnt_max; // Send max allowed # of bytes
}
// Send data to device
writeParms.data.data_val = (char *)(&ac_data[cnt_data - cnt_left]);
Device_WriteResp *p_writeResp = device_write_1 (&writeParms, _p_client);
if (p_writeResp == 0) { // Error if device does not respond
log_err ("Vxi11::write error: no RPC response for %s.\n",_s_device_addr);
return (1);
}
// Possible errors
// 0 = no error
// 4 = invalid link identifier
// 5 = parameter error
// 11 = device locked by another link
// 15 = I/O timeout
// 17 = I/O error
// 23 = abort
int err_code = int (p_writeResp->error);
if (err_code) {
int idx_err_desc = ((err_code >= 0) && (err_code < CNT_ERR_DESC_MAX)) ?
err_code : 0;
log_err ("Vxi11::write error: %d %s for %s.\n",
err_code, _as_err_desc[idx_err_desc], _s_device_addr);
return (1);
}
cnt_left -= p_writeResp->size; // Number of bytes left
} while (cnt_left > 0); // End when all sent
return (0);
}
// ***************************************************************************
// Vxi11::printf - Write data to the device using printf format
// VXI-11 RPC is "device_write"
//
// Parameters:
// 1. s_format - printf style format data to send to device
// 2. ... - Variable number of parameters, depending on s_format
//
// Returns: 0 = no error
// 1 = error
//
// Notes: If Vxi11 object is associated with a GPIB device or GPIB interface
// via a GPIB/LAN gateway, GPIB uses SEND command sequence, with EOI
// on last byte.
// ***************************************************************************
int Vxi11::
printf (const char *s_format, ...)
{
if (!s_format) { // Check input parameters
log_err ("Vxi11::printf error: invalid parameters for %s.\n",
_s_device_addr);
return (1);
}
const int CNT_DATA_MAX = 65536; // Max string to send
static char s_data[CNT_DATA_MAX];
va_list va; // Process input like printf() does
va_start (va, s_format);
int cnt = vsnprintf (s_data, CNT_DATA_MAX, s_format, va);
va_end (va);
s_data[CNT_DATA_MAX-1] = 0; // Make sure it is terminated
if ((cnt < 0) || (cnt>CNT_DATA_MAX)) {// Check for error
log_err ("Vxi11::printf error: vsnprintf error, count = %d for %s.\n",
cnt, _s_device_addr);
return (1);
}
// Send string to the device
int err = write (s_data, strlen (s_data));
return (err);
}
// ***************************************************************************
// Vxi11::read - Read data from the device
// VXI-11 RPC is "device_read"
//
// Parameters:
// 1. ac_data - Store read data here
// The result will be null-terminated if cnt_data_max is 1
// or more greater than the number of bytes read from the
// device.
// 2. cnt_data_max - Max length allocated in ac_data
// 3. pcnt_read - Returns actual number of bytes returned in ac_data
// This does not include the null terminator
//
// Returns: 0 = no error
// 1 = error
//
// Notes: If Vxi11 object is associated with a GPIB device or GPIB interface
// via a GPIB/LAN gateway, GPIB uses RECEIVE command sequence
// ***************************************************************************
int Vxi11::
read (char *ac_data, int cnt_data_max, int *pcnt_read)
{
int cnt_read_default; // Use local variable if user does not
if (!pcnt_read) // specify pcnt_read parameter
pcnt_read = &cnt_read_default;
*pcnt_read = 0; // No bytes read yet
// Early return if object did not make connection to instrument
if (!_b_valid) {
log_err ("Vxi11::read error: no connection to device.\n");
return (1);
}
if (!ac_data || (cnt_data_max < 1)) { // Check input parameters
log_err ("Vxi11::read error: invalid parameters for %s.\n",
_s_device_addr);
return (1);
}
ac_data[0] = 0; // Null string for early return
Device_ReadParms readParms; // To send to device_read RPC
readParms.lid = _p_link->lid; // Link ID from create_link RPC call
readParms.requestSize = cnt_data_max; // Maximum # of bytes to read
readParms.io_timeout = _timeout_ms; // Timeout for I/O in ms
readParms.lock_timeout = _timeout_ms; // Timeout for lock in ms
// Use END signal to terminate the read
// EOI line on GPIB, line feed (ASCII 10) data character on RS-232
if (_c_read_terminator == -1) {
readParms.flags = 0; // No special flags
readParms.termChar = 0; // Termination character not used,
// since flags = 0
}
// Use data character to terminate the read
else {
readParms.flags = 128; // Use termination character
readParms.termChar = (char)_c_read_terminator;
}
Vxi11Mutex vxi11Mutex; // Lock access until function returns
// Iterate reads, since internal buffer in device_read RPC call may be less
// than the maximum number of bytes requested
do {
// Number of bytes to read
readParms.requestSize = cnt_data_max - *pcnt_read;
// Read from the device
Device_ReadResp *p_readResp = device_read_1 (&readParms, _p_client);
if (p_readResp == 0) { // Check for error
log_err ("Vxi11::read error: no RPC response for %s.\n", _s_device_addr);
return (1);
}
// Copy read data to user buffer
const int cnt_read = p_readResp->data.data_len; // # of bytes actually read
if (cnt_read > 0) {
// Make sure we don't read more than the user buffer can hold.
// This error should never occur; it would only happen if the device
// sends more bytes than requested.
if ((*pcnt_read + cnt_read) > cnt_data_max) {
log_err ("Vxi11::read error: Read more bytes than expected for %s.\n",
_s_device_addr);
return (1);
}
// Copy to user buffer
memcpy (ac_data + *pcnt_read, p_readResp->data.data_val, cnt_read);
*pcnt_read += cnt_read;; // Update total # of bytes read
if (*pcnt_read < cnt_data_max) // Null terminate string if there is
ac_data[*pcnt_read] = 0; // enough room in the buffer
}
// Possible errors
// 0 = no error
// 4 = invalid link identifier
// 11 = device locked by another link
// 15 = I/O timeout
// 17 = I/O error
// 23 = abort
int err_code = int (p_readResp->error);
if (err_code) {
int idx_err_desc = ((err_code >= 0) && (err_code < CNT_ERR_DESC_MAX)) ?
err_code : 0;
log_err ("Vxi11::read error: %d %s, %d bytes read, "
"termination reason = 0x%x for %s.\n",
err_code, _as_err_desc[idx_err_desc], cnt_read,
p_readResp->reason, _s_device_addr);
return (1);
}
// If "END" indicator or termination character is read, then done reading
// from device.
//
// reason bit 2 = END (EOI on GPIB, line feed on RS-232 on E5810A)
// bit 1 = specified termination character found
//
// Test each bit separately because the E5810A when using the RS-232 port
// will turn on bit 2 whenever the line feed character is read, even if a
// different termination character is specified.
if (((_c_read_terminator == -1) && (p_readResp->reason & 4)) ||
((_c_read_terminator != -1) && (p_readResp->reason & 2)))
break;
// If user buffer is full, return with error
else if (*pcnt_read == cnt_data_max) {
log_err ("Vxi11::read error: read buffer full with %d bytes "
"before reaching END indicator, %d last bytes read, "
"termination reason 0x%x for %s.\n",
cnt_data_max, cnt_read, p_readResp->reason, _s_device_addr);
ac_data[cnt_data_max-1] = 0;
return (1);
}
} while (1);
return (0);
}
// ***************************************************************************
// Vxi11::query - Send query for a double from the device
// Convenience function combines write() and read()
//
// Parameters:
// 1. s_query - Query string to send to device
// 2. pd_val - Stores double read from device here
//
// Returns: 0 = no error
// 1 = error
// ***************************************************************************
int Vxi11::
query (const char *s_query, double *pd_val)
{
int CNT_READ_MAX = 256; // Assume 256 characters is enough
char s_read[CNT_READ_MAX+1]; // to read back a value
// Query to get string
int err = query (s_query, s_read, CNT_READ_MAX);
if (err) { // Return early if query failed
*pd_val = 0.0;
return (1);
}
// Convert string to double
int cnt = sscanf (s_read, "%le", pd_val);
if (cnt != 1) {
*pd_val = 0.0;
return (1);
}
return (0);
}
// ***************************************************************************
// Vxi11::query - Send query for an integer from the device
// Convenience function combines write() and read()
//
// Parameters:
// 1. s_query - Query string to send to device
// 2. pi_val - Stores integer read from device here
//
// Returns: 0 = no error
// 1 = error
// ***************************************************************************
int Vxi11::
query (const char *s_query, int *pi_val)
{
int CNT_READ_MAX = 256; // Assume 256 characters is enough
char s_read[CNT_READ_MAX+1]; // to read back a value
// Query to get string
int err = query (s_query, s_read, CNT_READ_MAX);
if (err) { // Return early if query failed
*pi_val = 0;
return (1);
}
// Convert string to double
int cnt = sscanf (s_read, "%d", pi_val);
if (cnt != 1) {
*pi_val = 0;
return (1);
}
return (0);
}
// ***************************************************************************
// Vxi11::query - Send query for a null-terminated string from the device
// Convenience function combines write() and read()
//
// Parameters:
// 1. s_query - Query string to send to device
// 2. s_val - Stores null-terminated string read from device here
// 3. len_val_max - Max length allocated in s_val, including null termination
//
// Returns: 0 = no error
// 1 = error
// ***************************************************************************
int Vxi11::
query (const char *s_query, char *s_val, int len_val_max)
{
// Send query
int err = write (s_query, strlen (s_query));
if (err) {
s_val[0] = 0;
return (1);
}
// Read response
err = read (s_val, len_val_max);
return (err);
}
// ***************************************************************************
// Vxi11::readstb - Read status byte from the device (serial poll)
// VXI-11 RPC is "device_readstb"
//
// Parameters: None
//
// Returns: 8-bit status byte if no error
// -1 if error
//
// Notes: If Vxi11 object is associated with a GPIB device (a GPIB instrument
// connected to a GPIB/LAN gateway), the READ STATUS BYTE control
// sequence is executed for the addressed device, with GPIB commands
// SPE (serial poll enable, ATN code 24) and SPD (serial poll disable,
// ATN code 25) (see notes)
//
// If Vxi11 object is associated with a GPIB interface (the GPIB/LAN
// gateway itself), this function will return an error.
// ***************************************************************************
int Vxi11::
readstb (void)
{
// Early return if object did not make connection to instrument
if (!_b_valid) {
log_err ("Vxi11::readstb error: no connection to device.\n");
return (1);
}
Device_GenericParms genericParms; // To send to device_readstb RPC
genericParms.lid = _p_link->lid; // Link ID from create_link RPC call
genericParms.io_timeout = _timeout_ms; // Timeout for I/O in ms
genericParms.lock_timeout = _timeout_ms; // Timeout for lock in ms
genericParms.flags = 0; // Not used
Vxi11Mutex vxi11Mutex; // Lock access until function returns
// Read status byte
Device_ReadStbResp *p_readStbResp=device_readstb_1 (&genericParms,_p_client);
if (!p_readStbResp) {
log_err ("Vxi11::readstb error: no RPC response for %s.\n",_s_device_addr);
return (-1);
}
// Possible errors
// 0 = no error
// 4 = invalid link identifier
// 8 = operation not supported
// 11 = device locked by another link
// 15 = I/O timeout
// 17 = I/O error
// 23 = abort
int err_code = int (p_readStbResp->error);
if (err_code) {
int idx_err_desc = ((err_code >= 0) && (err_code < CNT_ERR_DESC_MAX)) ?
err_code : 0;
log_err ("Vxi11::readstb error: %d %s for %s.\n",
err_code, _as_err_desc[idx_err_desc], _s_device_addr);
return (-1);
}
// Return status byte
int stb = p_readStbResp->stb;
return (stb);
}
// ***************************************************************************
// Vxi11::trigger - Send group execute trigger
// VXI-11 RPC is "device_trigger"
//
// Parameters: None
//
// Returns: 0 = no error
// 1 = error
//
// Notes: If Vxi11 object is associated with a GPIB device (a GPIB instrument
// connected to a GPIB/LAN gateway), only that device will receive the
// GPIB command GET (group execute trigger, ATN code 8).
//
// If Vxi11 object is associated with a GPIB interface (the GPIB/LAN
// gateway itself), then all addressed devices will receive the
// GPIB command GET (group execute trigger, ATN code 8).
// Use the doccmd_send_command() function to send the raw GPIB commands
// to address the set of devices prior to calling trigger().
// ***************************************************************************
int Vxi11::
trigger (void)
{
// Early return if object did not make connection to instrument
if (!_b_valid) {
log_err ("Vxi11::trigger error: no connection to device.\n");
return (1);
}
Device_GenericParms genericParms; // To send to device_trigger RPC
genericParms.lid = _p_link->lid; // Link ID from create_link RPC call
genericParms.io_timeout = _timeout_ms; // Timeout for I/O in ms
genericParms.lock_timeout = _timeout_ms; // Timeout for lock in ms
genericParms.flags = 0; // Not used
Vxi11Mutex vxi11Mutex; // Lock access until function returns
// Send trigger command
Device_Error *p_error = device_trigger_1 (&genericParms, _p_client);
if (!p_error) {
log_err ("Vxi11::trigger error: no RPC response for %s.\n",_s_device_addr);
return (1);
}
// Possible errors
// 0 = no error
// 4 = invalid link identifier
// 8 = operation not supported
// 11 = device locked by another link
// 15 = I/O timeout
// 17 = I/O error
// 23 = abort
int err_code = int (p_error->error);
if (err_code) {
int idx_err_desc = ((err_code >= 0) && (err_code < CNT_ERR_DESC_MAX)) ?
err_code : 0;
log_err ("Vxi11::trigger error: %d %s for %s.\n",
err_code, _as_err_desc[idx_err_desc], _s_device_addr);
return (1);
}
return (0);
}
// ***************************************************************************
// Vxi11::clear - Clear (reset) a device
// VXI-11 RPC is "device_clear"
//
// Parameters: None
//
// Returns: 0 = no error
// 1 = error
//
// Notes: If Vxi11 object is associated with a GPIB device (a GPIB instrument
// connected to a GPIB/LAN gateway) the GPIB command is SDC (selective
// device clear, ATN code 4), and resets only that device
//
// If Vxi11 object is associated with a GPIB interface (a GPIB/LAN
// gateway itself), GPIB command is DCL (device clear, ATN code 20),
// and resets all devices
// ***************************************************************************
int Vxi11::
clear (void)
{
// Early return if object did not make connection to instrument