sicl 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,49 +1,61 @@
1
- #!/usr/bin/ruby -W0
2
-
3
- # temporary solution for cygwin
4
- case RUBY_PLATFORM
5
- when "i386-cygwin"
6
- # default file location settings
7
- SICL_DIR = "/cygdrive/c/Program Files/Agilent/IO Libraries Suite"
8
- SICL_LIB_DIR = "#{SICL_DIR}/lib"
9
- SICL_INC_DIR = "#{SICL_DIR}/include"
10
- SICL32LIB = "sicl32.lib"
11
- # target options
12
- SICL_LIB_OPT = "--with-sicl-lib"
13
- SICL_INC_OPT = "--with-sicl-include"
14
-
15
- ARGV.delete_if do |arg|
16
- case arg
17
- when /^(#{SICL_INC_OPT}=)(.*)/
18
- SICL_INC_DIR = $2
19
- true
20
- when /^(#{SICL_LIB_OPT}=)(.*)/
21
- SICL_LIB_DIR = $2
22
- true
23
- else
24
- false
25
- end
26
- end
27
-
28
- ARGV << "#{SICL_INC_OPT}=#{SICL_INC_DIR}"
29
- ARGV << "#{SICL_LIB_OPT}=."
30
- SICL32LIB = "#{SICL_LIB_DIR}/#{SICL32LIB}"
31
-
32
- raise IOError.new("'#{SICL32LIB}': No such file") unless File.exist?(SICL32LIB)
33
-
34
- # convert dynamic link library to static link library
35
- `printf 'NAME sicl32.dll\nEXPORTS\n' >x.def`
36
- `nm -g --defined '#{SICL32LIB}' | sed -n '/00000000 I __imp__/s///p' >>x.def`
37
- `dlltool --def=x.def --output-lib=libsicl.a`
38
- `rm x.def`
39
- else
40
- end
41
-
42
- require 'mkmf'
43
-
44
- dir_config('sicl')
45
-
46
- if have_header('sicl.h') and have_library('sicl')
47
- create_makefile('sicl')
48
- end
49
-
1
+ #!/usr/bin/ruby -W0
2
+ ################################################################################
3
+ #
4
+ # Copyright (c) 2007 Naoki Kobayashi
5
+ #
6
+ # This library is free software. ( except for SICL Library )
7
+ # You can distribute/modify this file under the same terms of ruby.
8
+ #
9
+ # $LastChangedDate$
10
+ # $Revision$
11
+ # $Author$
12
+ #
13
+ ################################################################################
14
+
15
+ # temporary solution for cygwin
16
+ case RUBY_PLATFORM
17
+ when "i386-cygwin"
18
+ # default file location settings
19
+ SICL_DIR = "/cygdrive/c/Program Files/Agilent/IO Libraries Suite"
20
+ SICL_LIB_DIR = "#{SICL_DIR}/lib"
21
+ SICL_INC_DIR = "#{SICL_DIR}/include"
22
+ SICL32LIB = "sicl32.lib"
23
+ # target options
24
+ SICL_LIB_OPT = "--with-sicl-lib"
25
+ SICL_INC_OPT = "--with-sicl-include"
26
+
27
+ ARGV.delete_if do |arg|
28
+ case arg
29
+ when /^(#{SICL_INC_OPT}=)(.*)/
30
+ SICL_INC_DIR = $2
31
+ true
32
+ when /^(#{SICL_LIB_OPT}=)(.*)/
33
+ SICL_LIB_DIR = $2
34
+ true
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ ARGV << "#{SICL_INC_OPT}=#{SICL_INC_DIR}"
41
+ ARGV << "#{SICL_LIB_OPT}=."
42
+ SICL32LIB = "#{SICL_LIB_DIR}/#{SICL32LIB}"
43
+
44
+ raise IOError.new("'#{SICL32LIB}': No such file") unless File.exist?(SICL32LIB)
45
+
46
+ # convert dynamic link library to static link library
47
+ `printf 'NAME sicl32.dll\nEXPORTS\n' >x.def`
48
+ `nm -g --defined '#{SICL32LIB}' | sed -n '/00000000 I __imp__/s///p' >>x.def`
49
+ `dlltool --def=x.def --output-lib=libsicl.a`
50
+ `rm x.def`
51
+ else
52
+ end
53
+
54
+ require 'mkmf'
55
+
56
+ dir_config('sicl')
57
+
58
+ if have_header('sicl.h') and have_library('sicl')
59
+ create_makefile('sicl')
60
+ end
61
+
data/ext/sicl.c CHANGED
@@ -1,749 +1,752 @@
1
- /*
2
- * Copyright (c) 2005-2006 Naoki Kobayashi
3
- *
4
- * This library is free software. ( except for SICL Library )
5
- * You can distribute/modify this file under the same terms of ruby.
6
- *
7
- * $Header: $
8
- *
9
- */
10
-
11
- #if defined(__BORLANDC__)
12
- # define _MSC_VER 1200
13
- # include <windows.h>
14
- #endif /* __BORLANDC__ */
15
-
16
- #if defined(__CYGWIN__)
17
- # define _REENT_ONLY
18
- #endif /* __CYGWIN__ */
19
-
20
- #if defined(__BORLANDC__)
21
- __declspec(dllexport) BOOL __stdcall DllMain(HANDLE h, DWORD w, LPVOID p) { return TRUE; }
22
- #endif /* __BORLANDC__ */
23
-
24
- #include <ruby.h>
25
- #include "sicl.h"
26
-
27
- /* -----------------------------------------------------------------------
28
- File Local Variables
29
- ----------------------------------------------------------------------- */
30
-
31
- /* prototype of class and module */
32
- static VALUE mSicl;
33
- static VALUE cInstrument;
34
- static VALUE eSicl;
35
-
36
- /* hash table of internal configurations */
37
- static VALUE gConfig;
38
-
39
- /* id of event handlers */
40
- static ID s_errhdlr;
41
- static ID s_srqhdlr;
42
- static ID s_intrhdlr;
43
-
44
- /* id of 'call' method of Proc object */
45
- static ID s_call;
46
-
47
- /* id of 'buf_mode' method of SICL::Instrument object */
48
- static ID s_buf_mode;
49
-
50
- static const VALUE READ_ALL_DATA = INT2FIX(-1);
51
- static const char EOL[] = "\r\n";
52
-
53
- /* -----------------------------------------------------------------------
54
- struct cparse_params
55
- ----------------------------------------------------------------------- */
56
-
57
- struct instrument {
58
- INST id;
59
- int errno;
60
- };
61
-
62
- /* -----------------------------------------------------------------------
63
- Utils
64
- ----------------------------------------------------------------------- */
65
-
66
- #define CLEAR_ERROR(inst) do { inst->errno=0; } while(0)
67
-
68
- VALUE sicl_return ( VALUE self, struct instrument * inst, int error )
69
- {
70
- if ( error ) {
71
- inst->errno = error;
72
- } else {
73
- inst->errno = 0;
74
- }
75
-
76
- return error ? Qnil : self;
77
- }
78
-
79
- /* -----------------------------------------------------------------------
80
- SICL wrapper Main Routines
81
- ----------------------------------------------------------------------- */
82
-
83
- static VALUE sicl_close ( VALUE self )
84
- {
85
- rb_funcall ( self, rb_intern("close"), 0, 0 );
86
- }
87
-
88
- /* error handler ( dummy ) for sicl_enter () */
89
- static void SICLCALLBACK dmy_handler ( INST id, int error )
90
- {
91
- ; /* do nothing. */
92
- }
93
-
94
- /* error handler */
95
- static void SICLCALLBACK err_handler ( INST id, int error )
96
- {
97
- VALUE self = rb_hash_aref ( gConfig, INT2FIX(id) );
98
- VALUE hdlr = rb_ivar_get ( self, s_errhdlr );
99
-
100
- if ( ! NIL_P(self) ) {
101
- /* save error code */
102
- struct instrument * inst = NULL;
103
- Data_Get_Struct ( self, struct instrument, inst );
104
- inst->errno = error;
105
- }
106
-
107
- if ( ! NIL_P(hdlr) ) {
108
- VALUE err = rb_ary_new ();
109
- rb_ary_push ( err, INT2FIX(error) );
110
- rb_ary_push ( err, rb_str_new2(igeterrstr(error)) );
111
-
112
- rb_funcall ( hdlr, s_call, 1, err );
113
- } else {
114
- rb_raise ( eSicl, igeterrstr(error) );
115
- }
116
- }
117
-
118
- /* srq handler */
119
- static void SICLCALLBACK srq_handler ( INST id )
120
- {
121
- VALUE self = rb_hash_aref ( gConfig, INT2FIX(id) );
122
- VALUE hdlr = rb_ivar_get ( self, s_srqhdlr );
123
-
124
- if ( ! NIL_P(hdlr) )
125
- rb_funcall ( hdlr, s_call, 0 );
126
- }
127
-
128
- /* interrupt handler */
129
- static void SICLCALLBACK intr_handler ( INST id, long reason, long secval )
130
- {
131
- VALUE self = rb_hash_aref ( gConfig, INT2FIX(id) );
132
- VALUE hdlr = rb_ivar_get ( self, s_srqhdlr );
133
-
134
- if ( ! NIL_P(hdlr) )
135
- rb_funcall ( hdlr, s_call, 2, INT2FIX(reason), INT2FIX(secval) );
136
- }
137
-
138
- VALUE sicl_s_igeterrstr ( VALUE self, VALUE code )
139
- {
140
- Check_Type ( code, T_FIXNUM );
141
-
142
- return rb_str_new2 ( igeterrstr ( FIX2INT ( code ) ) );
143
- }
144
-
145
- VALUE sicl_igeterrstr ( VALUE self )
146
- {
147
- struct instrument * inst = NULL;
148
-
149
- Data_Get_Struct ( self, struct instrument, inst );
150
-
151
- return rb_str_new2 ( igeterrstr ( inst->errno ) );
152
- }
153
-
154
- VALUE sicl_igeterrno ( VALUE self )
155
- {
156
- struct instrument * inst = NULL;
157
-
158
- Data_Get_Struct ( self, struct instrument, inst );
159
-
160
- return INT2FIX(inst->errno);
161
- }
162
-
163
- VALUE sicl_ionerror ( VALUE self, VALUE proc )
164
- {
165
- if ( NIL_P(proc) || CLASS_OF(proc)==rb_cProc ) {
166
-
167
- rb_ivar_set ( self, s_errhdlr, proc );
168
-
169
- } else {
170
-
171
- rb_raise ( rb_eTypeError, "Need a block or nil" );
172
-
173
- }
174
-
175
- return self;
176
- }
177
-
178
- VALUE sicl_igetonerror ( VALUE self )
179
- {
180
- if ( rb_block_given_p() ) {
181
-
182
- rb_ivar_set ( self, s_errhdlr, rb_block_proc() );
183
-
184
- return self;
185
-
186
- } else {
187
-
188
- return rb_ivar_get ( self, s_errhdlr );
189
-
190
- }
191
- }
192
-
193
- VALUE sicl_ionsrq ( VALUE self, VALUE proc )
194
- {
195
- struct instrument * inst = NULL;
196
-
197
- Data_Get_Struct ( self, struct instrument, inst );
198
-
199
- if ( NIL_P(proc) ) {
200
-
201
- rb_ivar_set ( self, s_srqhdlr, proc );
202
- ionsrq ( inst->id, 0 );
203
-
204
- } else if ( CLASS_OF(proc)==rb_cProc ) {
205
-
206
- rb_ivar_set ( self, s_srqhdlr, proc );
207
- ionsrq ( inst->id, srq_handler );
208
-
209
- } else {
210
-
211
- rb_raise ( rb_eTypeError, "Need a block or nil" );
212
-
213
- }
214
-
215
- return self;
216
- }
217
-
218
- VALUE sicl_igetonsrq ( VALUE self )
219
- {
220
- struct instrument * inst = NULL;
221
-
222
- Data_Get_Struct ( self, struct instrument, inst );
223
-
224
- if ( rb_block_given_p() ) {
225
-
226
- rb_ivar_set ( self, s_srqhdlr, rb_block_proc() );
227
- ionsrq ( inst->id, srq_handler );
228
-
229
- return self;
230
-
231
- } else {
232
-
233
- return rb_ivar_get ( self, s_srqhdlr );
234
-
235
- }
236
- }
237
-
238
- VALUE sicl_ionintr ( VALUE self, VALUE proc )
239
- {
240
- struct instrument * inst = NULL;
241
-
242
- Data_Get_Struct ( self, struct instrument, inst );
243
-
244
- if ( NIL_P(proc) ) {
245
-
246
- rb_ivar_set ( self, s_intrhdlr, proc );
247
- ionintr ( inst->id, 0 );
248
-
249
- } else if ( CLASS_OF(proc)==rb_cProc ) {
250
-
251
- rb_ivar_set ( self, s_intrhdlr, proc );
252
- ionintr ( inst->id, intr_handler );
253
-
254
- } else {
255
-
256
- rb_raise ( rb_eTypeError, "Need a block or nil" );
257
-
258
- }
259
-
260
- return self;
261
- }
262
-
263
- VALUE sicl_igetonintr ( VALUE self )
264
- {
265
- struct instrument * inst = NULL;
266
-
267
- Data_Get_Struct ( self, struct instrument, inst );
268
-
269
- if ( rb_block_given_p() ) {
270
-
271
- rb_ivar_set ( self, s_intrhdlr, rb_block_proc() );
272
- ionintr ( inst->id, intr_handler );
273
-
274
- return self;
275
-
276
- } else {
277
-
278
- return rb_ivar_get ( self, s_intrhdlr );
279
-
280
- }
281
- }
282
-
283
- VALUE sicl_s_allocate ( VALUE klass )
284
- {
285
- VALUE self = 0;
286
- struct instrument * inst = NULL;
287
-
288
- self = Data_Make_Struct ( klass, struct instrument, 0, 0, inst );
289
-
290
- rb_ivar_set ( self, s_errhdlr, Qnil );
291
- rb_ivar_set ( self, s_srqhdlr, Qnil );
292
- rb_ivar_set ( self, s_intrhdlr, Qnil );
293
- rb_ivar_set ( self, s_buf_mode, Qnil );
294
-
295
- return self;
296
- }
297
-
298
- VALUE sicl_iopen ( VALUE self, VALUE addr )
299
- {
300
- struct instrument * inst = NULL;
301
-
302
- Data_Get_Struct ( self, struct instrument, inst );
303
-
304
- Check_Type ( addr, T_STRING );
305
-
306
- inst->id = iopen ( RSTRING(addr)->ptr );
307
- itimeout ( inst->id, 5000 );
308
-
309
- rb_hash_aset ( gConfig, INT2FIX(inst->id), self );
310
-
311
- CLEAR_ERROR ( inst );
312
-
313
- if ( rb_block_given_p() ) {
314
- return rb_ensure(rb_yield, self, sicl_close, self);
315
- }
316
-
317
- return self;
318
- }
319
-
320
- VALUE sicl_s_iopen ( VALUE klass, VALUE addr )
321
- {
322
- VALUE self = sicl_s_allocate (cInstrument);
323
-
324
- return sicl_iopen ( self, addr );
325
- }
326
-
327
- VALUE sicl_initialize ( VALUE self, VALUE addr )
328
- {
329
- return sicl_iopen ( self, addr );
330
- }
331
-
332
- VALUE sicl_itimeout ( VALUE self, VALUE ms )
333
- {
334
- int err = 0;
335
- struct instrument * inst = NULL;
336
-
337
- Check_Type ( ms, T_FIXNUM );
338
-
339
- Data_Get_Struct ( self, struct instrument, inst );
340
-
341
- err = itimeout ( inst->id, FIX2INT(ms) );
342
-
343
- return sicl_return ( self, inst, err );
344
- }
345
-
346
- VALUE sicl_igettimeout ( VALUE self )
347
- {
348
- int err = 0;
349
- long tval = 0;
350
- struct instrument * inst = NULL;
351
-
352
- Data_Get_Struct ( self, struct instrument, inst );
353
-
354
- err = igettimeout ( inst->id, & tval );
355
-
356
- return sicl_return ( INT2FIX(tval), inst, err );
357
- }
358
-
359
- VALUE sicl_set_buf_mode ( VALUE self, VALUE enabled )
360
- {
361
- if ( NIL_P(enabled) )
362
- rb_ivar_set ( self, s_buf_mode, Qnil );
363
- else if ( TYPE(enabled)==T_FALSE )
364
- rb_ivar_set ( self, s_buf_mode, Qfalse );
365
- else
366
- rb_ivar_set ( self, s_buf_mode, Qtrue );
367
-
368
- return self;
369
- }
370
-
371
- VALUE sicl_get_buf_mode ( VALUE self )
372
- {
373
- return rb_ivar_get ( self, s_buf_mode );
374
- }
375
-
376
- VALUE sicl_iwrite ( VALUE self, VALUE cmd, VALUE eoi )
377
- {
378
- int err = 0;
379
- struct instrument * inst = NULL;
380
- int endi = 0; /* send END indicator, or not */
381
- unsigned long actualcnt = 0;
382
- char * ptr = NULL;
383
- long len = 0;
384
-
385
- int ( * write_func )( INST, char *, unsigned long, int, unsigned long * ) = iwrite;
386
-
387
- Check_Type ( cmd, T_STRING );
388
-
389
- if ( TYPE(eoi)==T_TRUE ) endi = 1;
390
-
391
- Data_Get_Struct ( self, struct instrument, inst );
392
-
393
- ptr = RSTRING(cmd)->ptr;
394
- len = RSTRING(cmd)->len;
395
-
396
- if ( rb_ivar_get( self, s_buf_mode )==Qtrue ) write_func = ifwrite;
397
-
398
- while ( len>0 ) {
399
- err = write_func ( inst->id, ptr, len, endi, & actualcnt );
400
-
401
- if ( err ) break;
402
-
403
- ptr += actualcnt;
404
- len -= actualcnt;
405
- }
406
-
407
- return sicl_return ( self, inst, err );
408
- }
409
-
410
- VALUE sicl_iwrite_op ( VALUE self, VALUE cmd )
411
- {
412
- VALUE eoi = Qfalse;
413
- char ch = 0;
414
-
415
- Check_Type ( cmd, T_STRING );
416
-
417
- ch = RSTRING(cmd)->ptr[RSTRING(cmd)->len-1];
418
-
419
- if ( ch == '\r' || ch == '\n' ) eoi = Qtrue;
420
-
421
- return sicl_iwrite ( self, cmd, eoi );
422
- }
423
-
424
- VALUE sicl_iwrite_arg ( int argc, VALUE * argv, VALUE self )
425
- {
426
- VALUE cmd = 0;
427
- VALUE eoi = 0;
428
-
429
- rb_scan_args ( argc, argv, "11", & cmd, & eoi );
430
-
431
- return sicl_iwrite ( self, cmd, eoi );
432
- }
433
-
434
- VALUE sicl_iread ( VALUE self, VALUE max )
435
- {
436
- int err = 0;
437
- struct instrument * inst = NULL;
438
- char buf[0x100];
439
- int size = sizeof(buf)-1;
440
- int len = NUM2INT(READ_ALL_DATA);
441
- int reason = 0;
442
- unsigned long actualcnt = 0;
443
-
444
- int ( * read_func )( INST, char *, unsigned long, int *, unsigned long *) = iread;
445
-
446
- VALUE str = rb_str_new2 ("");
447
-
448
- buf[0] = '\0';
449
-
450
- if ( ! NIL_P(max) ) len = NUM2INT(max);
451
-
452
- Data_Get_Struct ( self, struct instrument, inst );
453
-
454
- if ( rb_ivar_get( self, s_buf_mode )==Qtrue ) read_func = ifread;
455
-
456
- for (;;) {
457
- if ( len==0 ) break;
458
-
459
- if ( 0<len && len<size ) size = len;
460
-
461
- err = read_func( inst->id, buf, size, & reason, & actualcnt );
462
-
463
- if ( err && err!=I_ERR_TIMEOUT ) break;
464
-
465
- len -= actualcnt;
466
- rb_str_cat ( str, buf, actualcnt );
467
-
468
- if ( err==I_ERR_TIMEOUT ) {
469
- if ( RSTRING(str)->len>0 ) err = I_ERR_NOERROR;
470
- break;
471
- }
472
- if ( reason==I_TERM_END ) break;
473
- if ( reason==I_TERM_CHR ) break;
474
- }
475
-
476
- return sicl_return ( str, inst, err );
477
- }
478
-
479
- VALUE sicl_iread_arg ( int argc, VALUE * argv, VALUE self )
480
- {
481
- VALUE max = 0;
482
-
483
- rb_scan_args ( argc, argv, "01", & max );
484
-
485
- return sicl_iread ( self, max );
486
- }
487
-
488
- VALUE sicl_output ( VALUE self, VALUE cmd )
489
- {
490
- VALUE tmp = 0;
491
- VALUE eoi = Qtrue;
492
- VALUE reg1 = 0;
493
- VALUE reg2 = 0;
494
- VALUE blnk = 0;
495
- ID s_gsub = 0;
496
-
497
- Check_Type ( cmd, T_STRING );
498
-
499
- reg1 = rb_reg_new ( "[\\s;]*$", 7, 0 );
500
- reg2 = rb_reg_new ( "^[\\s;]*", 7, 0 );
501
- blnk = rb_str_new2 ( "" );
502
- s_gsub = rb_intern ( "gsub" );
503
-
504
- tmp = rb_funcall ( cmd, s_gsub, 2, reg1, blnk );
505
- cmd = rb_funcall ( tmp, s_gsub, 2, reg2, blnk );
506
-
507
- rb_str_cat2 ( cmd, EOL );
508
-
509
- self = sicl_iwrite ( self, cmd, eoi );
510
-
511
- return self;
512
- }
513
-
514
- VALUE sicl_enter ( VALUE self )
515
- {
516
- VALUE str = 0;
517
- const ID s_chomp = rb_intern ( "chomp" );
518
-
519
- ionerror ( dmy_handler );
520
-
521
- str = sicl_iread ( self, READ_ALL_DATA );
522
-
523
- ionerror ( err_handler );
524
-
525
- if ( NIL_P(str) ) return Qnil;
526
-
527
- str = rb_funcall ( str, s_chomp, 0 );
528
-
529
- return str;
530
- }
531
-
532
- VALUE sicl_enter2 ( VALUE self, VALUE out )
533
- {
534
- VALUE str = 0;
535
- const VALUE reg = rb_reg_new ( ".*", 2, 0 );
536
- const ID s_gsub = rb_intern ( "gsub!" );
537
-
538
- Check_Type ( out, T_STRING );
539
-
540
- str = sicl_enter ( self );
541
-
542
- if ( NIL_P(str) ) return Qnil;
543
-
544
- rb_funcall ( out, s_gsub, 2, reg, str );
545
-
546
- return self;
547
- }
548
-
549
- VALUE sicl_query ( VALUE self, VALUE cmd )
550
- {
551
- self = sicl_output ( self, cmd );
552
-
553
- return NIL_P(self) ? Qnil : sicl_enter ( self );
554
- }
555
-
556
- VALUE sicl_ireadstb ( VALUE self )
557
- {
558
- int err = 0;
559
- struct instrument * inst = NULL;
560
- unsigned char stb = 0;
561
-
562
- Data_Get_Struct ( self, struct instrument, inst );
563
-
564
- err = ireadstb ( inst->id, & stb );
565
-
566
- return sicl_return ( INT2FIX( stb ), inst, err );
567
- }
568
-
569
- int sicl_func ( VALUE self, int (*func)( INST ) )
570
- {
571
- int err = 0;
572
- struct instrument * inst = NULL;
573
-
574
- Data_Get_Struct ( self, struct instrument, inst );
575
-
576
- err = func ( inst->id );
577
-
578
- return sicl_return ( self, inst, err );
579
- }
580
-
581
- VALUE sicl_iclose ( VALUE self )
582
- {
583
- int err = 0;
584
- struct instrument * inst = NULL;
585
- INST id = 0;
586
-
587
- Data_Get_Struct ( self, struct instrument, inst );
588
-
589
- id = inst->id;
590
-
591
- err = iclose ( inst->id );
592
-
593
- if ( !err )
594
- rb_hash_delete ( gConfig, INT2FIX(id) );
595
-
596
- return !err ? Qtrue : Qfalse;
597
- }
598
-
599
- VALUE sicl_iflush ( int argc, VALUE * argv, VALUE self )
600
- {
601
- int err = 0;
602
- struct instrument * inst = NULL;
603
- VALUE mask = 0;
604
-
605
- rb_scan_args ( argc, argv, "01", & mask );
606
-
607
- if ( NIL_P(mask) ) mask = INT2FIX( I_BUF_READ | I_BUF_WRITE_END );
608
-
609
- Check_Type ( mask, T_FIXNUM );
610
-
611
- Data_Get_Struct ( self, struct instrument, inst );
612
-
613
- err = iflush ( inst->id, FIX2INT(mask) );
614
-
615
- return sicl_return ( self, inst, err );
616
- }
617
-
618
- VALUE sicl_igetaddr ( VALUE self )
619
- {
620
- int err = 0;
621
- struct instrument * inst = NULL;
622
- char * addr = NULL;
623
-
624
- Data_Get_Struct ( self, struct instrument, inst );
625
-
626
- err = igetaddr ( inst->id, & addr );
627
-
628
- return sicl_return ( rb_str_new2 ( addr ), inst, err );
629
- }
630
-
631
- VALUE sicl_itermchr ( VALUE self, VALUE tchr )
632
- {
633
- int err = 0;
634
- struct instrument * inst = NULL;
635
-
636
- Data_Get_Struct ( self, struct instrument, inst );
637
-
638
- Check_Type ( tchr, T_FIXNUM );
639
-
640
- err = itermchr ( inst->id, FIX2INT(tchr) );
641
-
642
- return sicl_return ( self, inst, err );
643
- }
644
-
645
- VALUE sicl_igettermchr ( VALUE self )
646
- {
647
- int err = 0;
648
- struct instrument * inst = NULL;
649
- int tchr = 0;
650
-
651
- Data_Get_Struct ( self, struct instrument, inst );
652
-
653
- err = igettermchr ( inst->id, & tchr );
654
-
655
- return sicl_return ( INT2FIX ( tchr ), inst, err );
656
- }
657
-
658
- VALUE sicl_iabort ( VALUE self ) { sicl_func ( self, iabort ); }
659
- VALUE sicl_iclear ( VALUE self ) { sicl_func ( self, iclear ); }
660
- VALUE sicl_ilocal ( VALUE self ) { sicl_func ( self, ilocal ); }
661
- VALUE sicl_iremote ( VALUE self ) { sicl_func ( self, iremote ); }
662
- VALUE sicl_ilock ( VALUE self ) { sicl_func ( self, ilock ); }
663
- VALUE sicl_iunlock ( VALUE self ) { sicl_func ( self, iunlock ); }
664
- VALUE sicl_itrigger ( VALUE self ) { sicl_func ( self, itrigger ); }
665
-
666
- /* -----------------------------------------------------------------------
667
- Ruby Interface
668
- ----------------------------------------------------------------------- */
669
- #if defined(__BORLANDC__)
670
- __declspec(dllexport) void __stdcall Init_sicl()
671
- #else /* __BORLANDC__ */
672
- void Init_sicl()
673
- #endif /* __BORLANDC__ */
674
- {
675
- mSicl = rb_define_module ( "SICL" );
676
-
677
- rb_define_module_function( mSicl, "open", sicl_s_iopen, 1 );
678
- rb_define_module_function( mSicl, "errstr", sicl_s_igeterrstr, 1 );
679
-
680
- rb_define_const ( mSicl, "BUF_READ" , INT2FIX(I_BUF_READ) );
681
- rb_define_const ( mSicl, "BUF_WRITE" , INT2FIX(I_BUF_WRITE) );
682
- rb_define_const ( mSicl, "BUF_WRITE_END" , INT2FIX(I_BUF_WRITE_END) );
683
- rb_define_const ( mSicl, "BUF_DISCARD_READ" , INT2FIX(I_BUF_DISCARD_READ) );
684
- rb_define_const ( mSicl, "BUF_DISCARD_WRITE", INT2FIX(I_BUF_DISCARD_WRITE) );
685
-
686
- rb_define_const ( mSicl, "EOL", rb_str_new2(EOL) );
687
-
688
- eSicl = rb_define_class_under ( mSicl, "Error", rb_eStandardError );
689
-
690
- ionerror ( err_handler );
691
-
692
- s_call = rb_intern ( "call" );
693
-
694
- s_buf_mode = rb_intern ( "buf_mode" );
695
-
696
- s_errhdlr = rb_intern ( "errhdlr" );
697
- s_srqhdlr = rb_intern ( "srqhdlr" );
698
- s_intrhdlr = rb_intern ( "intrhdlr" );
699
-
700
- gConfig = rb_hash_new ();
701
- rb_global_variable( & gConfig );
702
-
703
- cInstrument = rb_define_class_under ( mSicl, "Instrument", rb_cObject );
704
-
705
- rb_define_alloc_func ( cInstrument, sicl_s_allocate );
706
-
707
- rb_define_method ( cInstrument, "initialize" , sicl_initialize, 1 );
708
- rb_define_method ( cInstrument, "close" , sicl_iclose , 0 );
709
- rb_define_method ( cInstrument, "write" , sicl_iwrite_arg, -1 );
710
- rb_define_method ( cInstrument, "<<" , sicl_iwrite_op, 1 );
711
- rb_define_method ( cInstrument, "read" , sicl_iread_arg , -1 );
712
- rb_define_method ( cInstrument, "readstb" , sicl_ireadstb , 0 );
713
- rb_define_method ( cInstrument, "spoll" , sicl_ireadstb , 0 );
714
- rb_define_method ( cInstrument, "flush" , sicl_iflush , -1 );
715
-
716
- rb_define_method ( cInstrument, "timeout=", sicl_itimeout , 1 );
717
- rb_define_method ( cInstrument, "timeout" , sicl_igettimeout, 0 );
718
- rb_define_method ( cInstrument, "termchr=", sicl_itermchr , 1 );
719
- rb_define_method ( cInstrument, "termchr" , sicl_igettermchr, 0 );
720
- rb_define_method ( cInstrument, "addr" , sicl_igetaddr , 0 );
721
- rb_define_method ( cInstrument, "address" , sicl_igetaddr , 0 );
722
- rb_define_method ( cInstrument, "errstr" , sicl_igeterrstr , 0 );
723
- rb_define_method ( cInstrument, "errno" , sicl_igeterrno , 0 );
724
-
725
- rb_define_method ( cInstrument, "output" , sicl_output, 1 );
726
- rb_define_method ( cInstrument, "enter" , sicl_enter , 0 );
727
- rb_define_method ( cInstrument, "query" , sicl_query , 1 );
728
-
729
- rb_define_method ( cInstrument, "abort" , sicl_iabort , 0 );
730
- rb_define_method ( cInstrument, "clear" , sicl_iclear , 0 );
731
- rb_define_method ( cInstrument, "local" , sicl_ilocal , 0 );
732
- rb_define_method ( cInstrument, "remote" , sicl_iremote , 0 );
733
- rb_define_method ( cInstrument, "lock" , sicl_ilock , 0 );
734
- rb_define_method ( cInstrument, "unlock" , sicl_iunlock , 0 );
735
- rb_define_method ( cInstrument, "trigger", sicl_itrigger, 0 );
736
-
737
- rb_define_method ( cInstrument, "on_error=", sicl_ionerror , 1 );
738
- rb_define_method ( cInstrument, "on_error" , sicl_igetonerror, 0 );
739
- rb_define_method ( cInstrument, "on_srq=" , sicl_ionsrq , 1 );
740
- rb_define_method ( cInstrument, "on_srq" , sicl_igetonsrq , 0 );
741
- rb_define_method ( cInstrument, "on_intr=" , sicl_ionintr , 1 );
742
- rb_define_method ( cInstrument, "on_intr" , sicl_igetonintr , 0 );
743
- rb_define_method ( cInstrument, "on_interrupt=" , sicl_ionintr , 1 );
744
- rb_define_method ( cInstrument, "on_interrupt" , sicl_igetonintr , 0 );
745
-
746
- /* accessor for @buf_mode */
747
- rb_define_method ( cInstrument, "buf_mode=", sicl_set_buf_mode, 1 );
748
- rb_define_method ( cInstrument, "buf_mode" , sicl_get_buf_mode, 0 );
749
- }
1
+ /*
2
+ * Copyright (c) 2005-2006 Naoki Kobayashi
3
+ *
4
+ * This library is free software.(except for SICL Library)
5
+ * You can distribute/modify this file under the same terms of ruby.
6
+ *
7
+ * $LastChangedDate$
8
+ * $Revision$
9
+ * $Author$
10
+ *
11
+ */
12
+
13
+ #if defined(__BORLANDC__) || defined(__CYGWIN__)
14
+ # define _MSC_VER 1200
15
+ # include <windows.h>
16
+ #endif /* __BORLANDC__ */
17
+
18
+ #if defined(__CYGWIN__)
19
+ # define _REENT_ONLY
20
+ #endif /* __CYGWIN__ */
21
+
22
+ #if defined(__BORLANDC__)
23
+ __declspec(dllexport) BOOL __stdcall DllMain(HANDLE h, DWORD w, LPVOID p) { return TRUE; }
24
+ #endif /* __BORLANDC__ */
25
+
26
+ #include <ruby.h>
27
+ #include "sicl.h"
28
+
29
+ /* -----------------------------------------------------------------------
30
+ File Local Variables
31
+ ----------------------------------------------------------------------- */
32
+
33
+ /* prototype of class and module */
34
+ static VALUE mSicl;
35
+ static VALUE cInstrument;
36
+ static VALUE eSicl;
37
+
38
+ /* hash table of internal configurations */
39
+ static VALUE gConfig;
40
+
41
+ /* id of event handlers */
42
+ static ID s_errhdlr;
43
+ static ID s_srqhdlr;
44
+ static ID s_intrhdlr;
45
+
46
+ /* id of 'call' method of Proc object */
47
+ static ID s_call;
48
+
49
+ /* id of 'buf_mode' method of SICL::Instrument object */
50
+ static ID s_buf_mode;
51
+
52
+ static const VALUE READ_ALL_DATA = INT2FIX(-1);
53
+ static const char EOL[] = "\r\n";
54
+
55
+ /* -----------------------------------------------------------------------
56
+ struct cparse_params
57
+ ----------------------------------------------------------------------- */
58
+
59
+ struct instrument {
60
+ INST id;
61
+ int err;
62
+ };
63
+
64
+ /* -----------------------------------------------------------------------
65
+ Utils
66
+ ----------------------------------------------------------------------- */
67
+
68
+ #define CLEAR_ERROR(inst) do { inst->err=0; } while (0)
69
+
70
+ static VALUE sicl_return(VALUE self, struct instrument * inst, int error)
71
+ {
72
+ if (error) {
73
+ inst->err = error;
74
+ } else {
75
+ inst->err = 0;
76
+ }
77
+
78
+ return error ? Qnil : self;
79
+ }
80
+
81
+ /* -----------------------------------------------------------------------
82
+ SICL wrapper Main Routines
83
+ ----------------------------------------------------------------------- */
84
+
85
+ static VALUE sicl_close (VALUE self)
86
+ {
87
+ rb_funcall(self, rb_intern("close"), 0, 0);
88
+ }
89
+
90
+ /* error handler(dummy) for sicl_enter () */
91
+ static void SICLCALLBACK dmy_handler(INST id, int error)
92
+ {
93
+ ; /* do nothing. */
94
+ }
95
+
96
+ /* error handler */
97
+ static void SICLCALLBACK err_handler(INST id, int error)
98
+ {
99
+ VALUE self = rb_hash_aref(gConfig, INT2FIX(id));
100
+ VALUE hdlr = rb_ivar_get(self, s_errhdlr);
101
+
102
+ if (! NIL_P(self)) {
103
+ /* save error code */
104
+ struct instrument * inst = NULL;
105
+ Data_Get_Struct(self, struct instrument, inst);
106
+ inst->err = error;
107
+ }
108
+
109
+ if (! NIL_P(hdlr)) {
110
+ VALUE err = rb_ary_new ();
111
+ rb_ary_push(err, INT2FIX(error));
112
+ rb_ary_push(err, rb_str_new2(igeterrstr(error)));
113
+
114
+ rb_funcall(hdlr, s_call, 1, err);
115
+ } else {
116
+ rb_raise(eSicl, igeterrstr(error));
117
+ }
118
+ }
119
+
120
+ /* srq handler */
121
+ static void SICLCALLBACK srq_handler(INST id)
122
+ {
123
+ VALUE self = rb_hash_aref(gConfig, INT2FIX(id));
124
+ VALUE hdlr = rb_ivar_get(self, s_srqhdlr);
125
+
126
+ if (! NIL_P(hdlr))
127
+ rb_funcall(hdlr, s_call, 0);
128
+ }
129
+
130
+ /* interrupt handler */
131
+ static void SICLCALLBACK intr_handler(INST id, long reason, long secval)
132
+ {
133
+ VALUE self = rb_hash_aref(gConfig, INT2FIX(id));
134
+ VALUE hdlr = rb_ivar_get(self, s_srqhdlr);
135
+
136
+ if (! NIL_P(hdlr))
137
+ rb_funcall(hdlr, s_call, 2, INT2FIX(reason), INT2FIX(secval));
138
+ }
139
+
140
+ static VALUE sicl_s_igeterrstr(VALUE self, VALUE code)
141
+ {
142
+ Check_Type(code, T_FIXNUM);
143
+
144
+ return rb_str_new2(igeterrstr(FIX2INT(code)));
145
+ }
146
+
147
+ static VALUE sicl_igeterrstr(VALUE self)
148
+ {
149
+ struct instrument * inst = NULL;
150
+
151
+ Data_Get_Struct(self, struct instrument, inst);
152
+
153
+ return rb_str_new2(igeterrstr(inst->err));
154
+ }
155
+
156
+ static VALUE sicl_igeterrno(VALUE self)
157
+ {
158
+ struct instrument * inst = NULL;
159
+
160
+ Data_Get_Struct(self, struct instrument, inst);
161
+
162
+ return INT2FIX(inst->err);
163
+ }
164
+
165
+ static VALUE sicl_ionerror(VALUE self, VALUE proc)
166
+ {
167
+ if (NIL_P(proc) || CLASS_OF(proc)==rb_cProc) {
168
+
169
+ rb_ivar_set(self, s_errhdlr, proc);
170
+
171
+ } else {
172
+
173
+ rb_raise(rb_eTypeError, "Need a block or nil");
174
+
175
+ }
176
+
177
+ return self;
178
+ }
179
+
180
+ static VALUE sicl_igetonerror(VALUE self)
181
+ {
182
+ if (rb_block_given_p()) {
183
+
184
+ rb_ivar_set(self, s_errhdlr, rb_block_proc());
185
+
186
+ return self;
187
+
188
+ } else {
189
+
190
+ return rb_ivar_get(self, s_errhdlr);
191
+
192
+ }
193
+ }
194
+
195
+ static VALUE sicl_ionsrq(VALUE self, VALUE proc)
196
+ {
197
+ struct instrument * inst = NULL;
198
+
199
+ Data_Get_Struct(self, struct instrument, inst);
200
+
201
+ if (NIL_P(proc)) {
202
+
203
+ rb_ivar_set(self, s_srqhdlr, proc);
204
+ ionsrq(inst->id, 0);
205
+
206
+ } else if (CLASS_OF(proc)==rb_cProc) {
207
+
208
+ rb_ivar_set(self, s_srqhdlr, proc);
209
+ ionsrq(inst->id, srq_handler);
210
+
211
+ } else {
212
+
213
+ rb_raise(rb_eTypeError, "Need a block or nil");
214
+
215
+ }
216
+
217
+ return self;
218
+ }
219
+
220
+ static VALUE sicl_igetonsrq(VALUE self)
221
+ {
222
+ struct instrument * inst = NULL;
223
+
224
+ Data_Get_Struct(self, struct instrument, inst);
225
+
226
+ if (rb_block_given_p()) {
227
+
228
+ rb_ivar_set(self, s_srqhdlr, rb_block_proc());
229
+ ionsrq(inst->id, srq_handler);
230
+
231
+ return self;
232
+
233
+ } else {
234
+
235
+ return rb_ivar_get(self, s_srqhdlr);
236
+
237
+ }
238
+ }
239
+
240
+ static VALUE sicl_ionintr(VALUE self, VALUE proc)
241
+ {
242
+ struct instrument * inst = NULL;
243
+
244
+ Data_Get_Struct(self, struct instrument, inst);
245
+
246
+ if (NIL_P(proc)) {
247
+
248
+ rb_ivar_set(self, s_intrhdlr, proc);
249
+ ionintr(inst->id, 0);
250
+
251
+ } else if (CLASS_OF(proc)==rb_cProc) {
252
+
253
+ rb_ivar_set(self, s_intrhdlr, proc);
254
+ ionintr(inst->id, intr_handler);
255
+
256
+ } else {
257
+
258
+ rb_raise(rb_eTypeError, "Need a block or nil");
259
+
260
+ }
261
+
262
+ return self;
263
+ }
264
+
265
+ static VALUE sicl_igetonintr(VALUE self)
266
+ {
267
+ struct instrument * inst = NULL;
268
+
269
+ Data_Get_Struct(self, struct instrument, inst);
270
+
271
+ if (rb_block_given_p()) {
272
+
273
+ rb_ivar_set(self, s_intrhdlr, rb_block_proc());
274
+ ionintr(inst->id, intr_handler);
275
+
276
+ return self;
277
+
278
+ } else {
279
+
280
+ return rb_ivar_get(self, s_intrhdlr);
281
+
282
+ }
283
+ }
284
+
285
+ static VALUE sicl_s_allocate(VALUE klass)
286
+ {
287
+ VALUE self = 0;
288
+ struct instrument * inst = NULL;
289
+
290
+ self = Data_Make_Struct(klass, struct instrument, 0, 0, inst);
291
+
292
+ rb_ivar_set(self, s_errhdlr, Qnil);
293
+ rb_ivar_set(self, s_srqhdlr, Qnil);
294
+ rb_ivar_set(self, s_intrhdlr, Qnil);
295
+ rb_ivar_set(self, s_buf_mode, Qnil);
296
+
297
+ return self;
298
+ }
299
+
300
+ static VALUE sicl_iopen(VALUE self, VALUE addr)
301
+ {
302
+ struct instrument * inst = NULL;
303
+
304
+ Data_Get_Struct(self, struct instrument, inst);
305
+
306
+ Check_Type(addr, T_STRING);
307
+
308
+ inst->id = iopen(RSTRING(addr)->ptr);
309
+ itimeout(inst->id, 5000);
310
+
311
+ rb_hash_aset(gConfig, INT2FIX(inst->id), self);
312
+
313
+ CLEAR_ERROR(inst);
314
+
315
+ if (rb_block_given_p()) {
316
+ return rb_ensure(rb_yield, self, sicl_close, self);
317
+ }
318
+
319
+ return self;
320
+ }
321
+
322
+ static VALUE sicl_s_iopen(VALUE klass, VALUE addr)
323
+ {
324
+ VALUE self = sicl_s_allocate (cInstrument);
325
+
326
+ return sicl_iopen(self, addr);
327
+ }
328
+
329
+ static VALUE sicl_initialize(VALUE self, VALUE addr)
330
+ {
331
+ return sicl_iopen(self, addr);
332
+ }
333
+
334
+ static VALUE sicl_itimeout(VALUE self, VALUE ms)
335
+ {
336
+ int err = 0;
337
+ struct instrument * inst = NULL;
338
+
339
+ Check_Type(ms, T_FIXNUM);
340
+
341
+ Data_Get_Struct(self, struct instrument, inst);
342
+
343
+ err = itimeout(inst->id, FIX2INT(ms));
344
+
345
+ return sicl_return(self, inst, err);
346
+ }
347
+
348
+ static VALUE sicl_igettimeout(VALUE self)
349
+ {
350
+ int err = 0;
351
+ long tval = 0;
352
+ struct instrument * inst = NULL;
353
+
354
+ Data_Get_Struct(self, struct instrument, inst);
355
+
356
+ err = igettimeout(inst->id, & tval);
357
+
358
+ return sicl_return(INT2FIX(tval), inst, err);
359
+ }
360
+
361
+ static VALUE sicl_set_buf_mode(VALUE self, VALUE enabled)
362
+ {
363
+ if (NIL_P(enabled))
364
+ rb_ivar_set(self, s_buf_mode, Qnil);
365
+ else if (TYPE(enabled)==T_FALSE)
366
+ rb_ivar_set(self, s_buf_mode, Qfalse);
367
+ else
368
+ rb_ivar_set(self, s_buf_mode, Qtrue);
369
+
370
+ return self;
371
+ }
372
+
373
+ static VALUE sicl_get_buf_mode(VALUE self)
374
+ {
375
+ return rb_ivar_get(self, s_buf_mode);
376
+ }
377
+
378
+ static VALUE sicl_iwrite(VALUE self, VALUE cmd, VALUE eoi)
379
+ {
380
+ int err = 0;
381
+ struct instrument * inst = NULL;
382
+ int endi = 0; /* send END indicator, or not */
383
+ unsigned long actualcnt = 0;
384
+ char * ptr = NULL;
385
+ long len = 0;
386
+
387
+ int(* write_func)(INST, char *, unsigned long, int, unsigned long *) = iwrite;
388
+
389
+ Check_Type(cmd, T_STRING);
390
+
391
+ if (TYPE(eoi)==T_TRUE) endi = 1;
392
+
393
+ Data_Get_Struct(self, struct instrument, inst);
394
+
395
+ ptr = RSTRING(cmd)->ptr;
396
+ len = RSTRING(cmd)->len;
397
+
398
+ if (rb_ivar_get(self, s_buf_mode)==Qtrue) write_func = ifwrite;
399
+
400
+ while (len>0) {
401
+ err = write_func(inst->id, ptr, len, endi, & actualcnt);
402
+
403
+ if (err) break;
404
+
405
+ ptr += actualcnt;
406
+ len -= actualcnt;
407
+ }
408
+
409
+ return sicl_return(self, inst, err);
410
+ }
411
+
412
+ static VALUE sicl_iwrite_op(VALUE self, VALUE cmd)
413
+ {
414
+ VALUE eoi = Qfalse;
415
+ char ch = 0;
416
+
417
+ Check_Type(cmd, T_STRING);
418
+
419
+ ch = RSTRING(cmd)->ptr[RSTRING(cmd)->len-1];
420
+
421
+ if (ch == '\r' || ch == '\n') eoi = Qtrue;
422
+
423
+ return sicl_iwrite(self, cmd, eoi);
424
+ }
425
+
426
+ static VALUE sicl_iwrite_arg(int argc, VALUE * argv, VALUE self)
427
+ {
428
+ VALUE cmd = 0;
429
+ VALUE eoi = 0;
430
+
431
+ rb_scan_args(argc, argv, "11", & cmd, & eoi);
432
+
433
+ return sicl_iwrite(self, cmd, eoi);
434
+ }
435
+
436
+ static VALUE sicl_iread(VALUE self, VALUE max)
437
+ {
438
+ int err = 0;
439
+ struct instrument * inst = NULL;
440
+ char buf[0x100];
441
+ int size = sizeof(buf)-1;
442
+ int len = NUM2INT(READ_ALL_DATA);
443
+ int reason = 0;
444
+ unsigned long actualcnt = 0;
445
+
446
+ int(* read_func)(INST, char *, unsigned long, int *, unsigned long *) = iread;
447
+
448
+ VALUE str = rb_str_new2 ("");
449
+
450
+ buf[0] = '\0';
451
+
452
+ if (! NIL_P(max)) len = NUM2INT(max);
453
+
454
+ Data_Get_Struct(self, struct instrument, inst);
455
+
456
+ if (rb_ivar_get(self, s_buf_mode)==Qtrue) read_func = ifread;
457
+
458
+ for (;;) {
459
+ if (len==0) break;
460
+
461
+ if (0<len && len<size) size = len;
462
+
463
+ err = read_func(inst->id, buf, size, & reason, & actualcnt);
464
+
465
+ if (err && err!=I_ERR_TIMEOUT) break;
466
+
467
+ len -= actualcnt;
468
+ rb_str_cat(str, buf, actualcnt);
469
+
470
+ if (err==I_ERR_TIMEOUT) {
471
+ if (RSTRING(str)->len>0) err = I_ERR_NOERROR;
472
+ break;
473
+ }
474
+ if (reason==I_TERM_END) break;
475
+ if (reason==I_TERM_CHR) break;
476
+ }
477
+
478
+ return sicl_return(str, inst, err);
479
+ }
480
+
481
+ static VALUE sicl_iread_arg(int argc, VALUE * argv, VALUE self)
482
+ {
483
+ VALUE max = 0;
484
+
485
+ rb_scan_args(argc, argv, "01", & max);
486
+
487
+ return sicl_iread(self, max);
488
+ }
489
+
490
+ static VALUE sicl_output(VALUE self, VALUE cmd)
491
+ {
492
+ VALUE tmp = 0;
493
+ VALUE eoi = Qtrue;
494
+ VALUE reg1 = 0;
495
+ VALUE reg2 = 0;
496
+ VALUE blnk = 0;
497
+ ID s_gsub = 0;
498
+
499
+ Check_Type(cmd, T_STRING);
500
+
501
+ reg1 = rb_reg_new("[\\s;]*$", 7, 0);
502
+ reg2 = rb_reg_new("^[\\s;]*", 7, 0);
503
+ blnk = rb_str_new2("");
504
+ s_gsub = rb_intern("gsub");
505
+
506
+ tmp = rb_funcall(cmd, s_gsub, 2, reg1, blnk);
507
+ cmd = rb_funcall(tmp, s_gsub, 2, reg2, blnk);
508
+
509
+ rb_str_cat2(cmd, EOL);
510
+
511
+ self = sicl_iwrite(self, cmd, eoi);
512
+
513
+ return self;
514
+ }
515
+
516
+ static VALUE sicl_enter(VALUE self)
517
+ {
518
+ VALUE str = 0;
519
+ const ID s_chomp = rb_intern("chomp");
520
+
521
+ ionerror(dmy_handler);
522
+
523
+ str = sicl_iread(self, READ_ALL_DATA);
524
+
525
+ ionerror(err_handler);
526
+
527
+ if (NIL_P(str)) return Qnil;
528
+
529
+ str = rb_funcall(str, s_chomp, 0);
530
+
531
+ return str;
532
+ }
533
+
534
+ static VALUE sicl_enter2(VALUE self, VALUE out)
535
+ {
536
+ VALUE str = 0;
537
+ const VALUE reg = rb_reg_new(".*", 2, 0);
538
+ const ID s_gsub = rb_intern ("gsub!");
539
+
540
+ Check_Type(out, T_STRING);
541
+
542
+ str = sicl_enter(self);
543
+
544
+ if (NIL_P(str)) return Qnil;
545
+
546
+ rb_funcall(out, s_gsub, 2, reg, str);
547
+
548
+ return self;
549
+ }
550
+
551
+ static VALUE sicl_query(VALUE self, VALUE cmd)
552
+ {
553
+ self = sicl_output(self, cmd);
554
+
555
+ return NIL_P(self) ? Qnil : sicl_enter(self);
556
+ }
557
+
558
+ static VALUE sicl_ireadstb(VALUE self)
559
+ {
560
+ int err = 0;
561
+ struct instrument * inst = NULL;
562
+ unsigned char stb = 0;
563
+
564
+ Data_Get_Struct(self, struct instrument, inst);
565
+
566
+ err = ireadstb(inst->id, & stb);
567
+
568
+ return sicl_return(INT2FIX(stb), inst, err);
569
+ }
570
+
571
+ static int sicl_func(VALUE self, int (*func)(INST))
572
+ {
573
+ int err = 0;
574
+ struct instrument * inst = NULL;
575
+
576
+ Data_Get_Struct(self, struct instrument, inst);
577
+
578
+ err = func(inst->id);
579
+
580
+ return sicl_return(self, inst, err);
581
+ }
582
+
583
+ static VALUE sicl_iclose (VALUE self)
584
+ {
585
+ int err = 0;
586
+ struct instrument * inst = NULL;
587
+ INST id = 0;
588
+
589
+ Data_Get_Struct(self, struct instrument, inst);
590
+
591
+ id = inst->id;
592
+
593
+ err = iclose(inst->id);
594
+
595
+ if (!err)
596
+ rb_hash_delete(gConfig, INT2FIX(id));
597
+
598
+ return !err ? Qtrue : Qfalse;
599
+ }
600
+
601
+ static VALUE sicl_iflush (int argc, VALUE * argv, VALUE self)
602
+ {
603
+ int err = 0;
604
+ struct instrument * inst = NULL;
605
+ VALUE mask = 0;
606
+
607
+ rb_scan_args(argc, argv, "01", & mask);
608
+
609
+ if (NIL_P(mask)) mask = INT2FIX(I_BUF_READ | I_BUF_WRITE_END);
610
+
611
+ Check_Type(mask, T_FIXNUM);
612
+
613
+ Data_Get_Struct(self, struct instrument, inst);
614
+
615
+ err = iflush(inst->id, FIX2INT(mask));
616
+
617
+ return sicl_return(self, inst, err);
618
+ }
619
+
620
+ static VALUE sicl_igetaddr (VALUE self)
621
+ {
622
+ int err = 0;
623
+ struct instrument * inst = NULL;
624
+ char * addr = NULL;
625
+
626
+ Data_Get_Struct(self, struct instrument, inst);
627
+
628
+ err = igetaddr(inst->id, & addr);
629
+
630
+ return sicl_return(rb_str_new2(addr), inst, err);
631
+ }
632
+
633
+ static VALUE sicl_itermchr (VALUE self, VALUE tchr)
634
+ {
635
+ int err = 0;
636
+ struct instrument * inst = NULL;
637
+
638
+ Data_Get_Struct(self, struct instrument, inst);
639
+
640
+ Check_Type(tchr, T_FIXNUM);
641
+
642
+ err = itermchr(inst->id, FIX2INT(tchr));
643
+
644
+ return sicl_return(self, inst, err);
645
+ }
646
+
647
+ static VALUE sicl_igettermchr (VALUE self)
648
+ {
649
+ int err = 0;
650
+ struct instrument * inst = NULL;
651
+ int tchr = 0;
652
+
653
+ Data_Get_Struct(self, struct instrument, inst);
654
+
655
+ err = igettermchr(inst->id, & tchr);
656
+
657
+ return sicl_return(INT2FIX(tchr), inst, err);
658
+ }
659
+
660
+ static VALUE sicl_iabort (VALUE self) { sicl_func(self, iabort); }
661
+ static VALUE sicl_iclear (VALUE self) { sicl_func(self, iclear); }
662
+ static VALUE sicl_ilocal (VALUE self) { sicl_func(self, ilocal); }
663
+ static VALUE sicl_iremote(VALUE self) { sicl_func(self, iremote); }
664
+ static VALUE sicl_ilock (VALUE self) { sicl_func(self, ilock); }
665
+ static VALUE sicl_iunlock(VALUE self) { sicl_func(self, iunlock); }
666
+ static VALUE sicl_itrigger(VALUE self) { sicl_func(self, itrigger); }
667
+
668
+ /* -----------------------------------------------------------------------
669
+ Ruby Interface
670
+ ----------------------------------------------------------------------- */
671
+ #if defined(__BORLANDC__)
672
+ __declspec(dllexport) void __stdcall Init_sicl()
673
+ #else /* __BORLANDC__ */
674
+ void Init_sicl()
675
+ #endif /* __BORLANDC__ */
676
+ {
677
+ mSicl = rb_define_module("SICL");
678
+
679
+ rb_define_module_function(mSicl, "open", sicl_s_iopen, 1);
680
+
681
+ rb_define_const(mSicl, "BUF_READ" , INT2FIX(I_BUF_READ) );
682
+ rb_define_const(mSicl, "BUF_WRITE" , INT2FIX(I_BUF_WRITE) );
683
+ rb_define_const(mSicl, "BUF_WRITE_END" , INT2FIX(I_BUF_WRITE_END) );
684
+ rb_define_const(mSicl, "BUF_DISCARD_READ" , INT2FIX(I_BUF_DISCARD_READ) );
685
+ rb_define_const(mSicl, "BUF_DISCARD_WRITE", INT2FIX(I_BUF_DISCARD_WRITE));
686
+
687
+ rb_define_const(mSicl, "EOL", rb_str_new2(EOL));
688
+
689
+ eSicl = rb_define_class_under(mSicl, "Error", rb_eStandardError);
690
+ rb_define_singleton_method(eSicl, "errstr", sicl_s_igeterrstr, 1);
691
+
692
+ ionerror(err_handler);
693
+
694
+ s_call = rb_intern("call");
695
+
696
+ s_buf_mode = rb_intern("buf_mode");
697
+
698
+ s_errhdlr = rb_intern("errhdlr" );
699
+ s_srqhdlr = rb_intern("srqhdlr" );
700
+ s_intrhdlr = rb_intern("intrhdlr");
701
+
702
+ gConfig = rb_hash_new ();
703
+ rb_global_variable(& gConfig);
704
+
705
+ cInstrument = rb_define_class_under(mSicl, "Instrument", rb_cObject);
706
+
707
+ rb_define_alloc_func(cInstrument, sicl_s_allocate);
708
+ rb_define_singleton_method(cInstrument, "open", sicl_s_iopen, 1);
709
+
710
+ rb_define_method(cInstrument, "initialize" , sicl_initialize, 1);
711
+ rb_define_method(cInstrument, "close" , sicl_iclose , 0);
712
+ rb_define_method(cInstrument, "write" , sicl_iwrite_arg, -1);
713
+ rb_define_method(cInstrument, "<<" , sicl_iwrite_op, 1);
714
+ rb_define_method(cInstrument, "read" , sicl_iread_arg , -1);
715
+ rb_define_method(cInstrument, "readstb" , sicl_ireadstb , 0);
716
+ rb_define_method(cInstrument, "spoll" , sicl_ireadstb , 0);
717
+ rb_define_method(cInstrument, "flush" , sicl_iflush , -1);
718
+
719
+ rb_define_method(cInstrument, "timeout=", sicl_itimeout , 1);
720
+ rb_define_method(cInstrument, "timeout" , sicl_igettimeout, 0);
721
+ rb_define_method(cInstrument, "termchr=", sicl_itermchr , 1);
722
+ rb_define_method(cInstrument, "termchr" , sicl_igettermchr, 0);
723
+ rb_define_method(cInstrument, "addr" , sicl_igetaddr , 0);
724
+ rb_define_method(cInstrument, "address" , sicl_igetaddr , 0);
725
+ rb_define_method(cInstrument, "errstr" , sicl_igeterrstr , 0);
726
+ rb_define_method(cInstrument, "errno" , sicl_igeterrno , 0);
727
+
728
+ rb_define_method(cInstrument, "output" , sicl_output, 1);
729
+ rb_define_method(cInstrument, "enter" , sicl_enter , 0);
730
+ rb_define_method(cInstrument, "query" , sicl_query , 1);
731
+
732
+ rb_define_method(cInstrument, "abort" , sicl_iabort , 0);
733
+ rb_define_method(cInstrument, "clear" , sicl_iclear , 0);
734
+ rb_define_method(cInstrument, "local" , sicl_ilocal , 0);
735
+ rb_define_method(cInstrument, "remote" , sicl_iremote , 0);
736
+ rb_define_method(cInstrument, "lock" , sicl_ilock , 0);
737
+ rb_define_method(cInstrument, "unlock" , sicl_iunlock , 0);
738
+ rb_define_method(cInstrument, "trigger", sicl_itrigger, 0);
739
+
740
+ rb_define_method(cInstrument, "on_error=", sicl_ionerror , 1);
741
+ rb_define_method(cInstrument, "on_error" , sicl_igetonerror, 0);
742
+ rb_define_method(cInstrument, "on_srq=" , sicl_ionsrq , 1);
743
+ rb_define_method(cInstrument, "on_srq" , sicl_igetonsrq , 0);
744
+ rb_define_method(cInstrument, "on_intr=" , sicl_ionintr , 1);
745
+ rb_define_method(cInstrument, "on_intr" , sicl_igetonintr , 0);
746
+ rb_define_method(cInstrument, "on_interrupt=" , sicl_ionintr , 1);
747
+ rb_define_method(cInstrument, "on_interrupt" , sicl_igetonintr , 0);
748
+
749
+ /* accessor for @buf_mode */
750
+ rb_define_method(cInstrument, "buf_mode=", sicl_set_buf_mode, 1);
751
+ rb_define_method(cInstrument, "buf_mode" , sicl_get_buf_mode, 0);
752
+ }