File: ibCmds.c

package info (click to toggle)
linux-gpib-user 4.3.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,760 kB
  • sloc: ansic: 10,381; perl: 1,120; xml: 375; makefile: 335; yacc: 335; tcl: 308; python: 173; php: 157; lex: 144; sh: 134; lisp: 94
file content (462 lines) | stat: -rw-r--r-- 11,596 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#include <gpib/ib.h>

#undef _ANSI_ARGS_
#define _ANSI_ARGS_(x) x

void ib_CreateVerboseError(Tcl_Interp *interp,char *entry );

int Gpib_tcl_Init ( Tcl_Interp *interp ){


extern int gpibCmd _ANSI_ARGS_(( ClientData clientData,
			      Tcl_Interp *interp,
			       int argc,
			       const char *argv[]
			       ));

    Tcl_CreateCommand(interp,"gpib",gpibCmd,
		         (ClientData) NULL,
			 (Tcl_CmdDeleteProc *) NULL );

    return TCL_OK;
}


/**********************************************************************/
int ibWrite _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])){

  if( argc != 3 ){
    Tcl_SetResult(interp, "Error: write <dev> <string> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibwrt( atoi( argv[1]) , argv[2], strlen(argv[2]) ) & ERR ){
    ib_CreateVerboseError(interp,"ibwrt");
    return TCL_ERROR;
  }
     return TCL_OK;

}
/**********************************************************************/
int ibCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc != 3 ){
    Tcl_SetResult(interp, "Error: cmd <dev> <string> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibcmd( atoi( argv[1]) , argv[2], strlen(argv[2]) ) & ERR ){
    ib_CreateVerboseError(interp,"ibcmd");
    return TCL_ERROR;
  }
     return TCL_OK;

}
/**********************************************************************/
int ibRead  _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){


  char *buf;

  int len;
	int desc;

  if( argc != 3 ){
    Tcl_SetResult(interp, "Error: read <dev> <num bytes>", TCL_STATIC);
    return TCL_ERROR;
  }

	desc = strtol(argv[1], NULL, 0);
	len = strtol(argv[2], NULL, 0);
  

  if ((buf = (char *)malloc( len + 1 )) == NULL) {
    Tcl_SetResult(interp, "Error: Out of Memory", TCL_STATIC);
    return TCL_ERROR;
  }


  if( ibrd( desc , buf, len ) & ERR ){
/*  ib_CreateVerboseError(interp,"ibrd");
    return TCL_ERROR;
*/

  Tcl_AppendResult(interp, "ERROR" , (char *) NULL );
  free(buf);

  return TCL_ERROR;
  }


  buf[ibcnt] = '\0';
  Tcl_AppendResult(interp, buf, (char *) NULL );
  free(buf);

  return TCL_OK;
}

/**********************************************************************/

int ibDev _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]))
{
	int dev;
	char res[10];
	int minor, pad, sad, timo, eot, eos;

	if( argc != 7 )
	{
		Tcl_SetResult(interp, "Error: dev <minor> <pad> <sad> <timo> <eot> <eosmode>", TCL_STATIC);
		return TCL_ERROR;
	}

	minor = strtol(argv[1], NULL, 0);
	pad = strtol(argv[2], NULL, 0);
	sad = strtol(argv[3], NULL, 0);
	timo = strtol(argv[4], NULL, 0);
	eot = strtol(argv[5], NULL, 0);
	eos = strtol(argv[6], NULL, 0);

	if(( dev = ibdev(minor, pad, sad, timo, eot, eos)) < 0)
	{
		ib_CreateVerboseError(interp,"ibdev");
		return TCL_ERROR;
	}

	sprintf(res, "%4d", dev);
	Tcl_SetResult( interp, res, TCL_VOLATILE );

	return TCL_OK;
}
/**********************************************************************/

int ibFind  _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[]))
{
	int dev;
	char res[10];

	if( argc != 2 )
	{
		Tcl_SetResult(interp, "Error: find <string>", TCL_STATIC);
		return TCL_ERROR;
	}

	if(( dev = ibfind(argv[1])) < 0 )
	{
		ib_CreateVerboseError(interp,"ibfind");
		return TCL_ERROR;
	}

	sprintf(res, "%4d", dev);
	Tcl_SetResult( interp, res, TCL_VOLATILE );

	return TCL_OK;
}

/**********************************************************************/

int ibSre   _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc != 3 ){
    Tcl_SetResult(interp, "Error: ibsre <bool> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibsre( atoi( argv[1]) , atoi( argv[2] )) & ERR ){
    ib_CreateVerboseError(interp,"ibsre");
    return TCL_ERROR;
  }
     return TCL_OK;
}
/**********************************************************************/

int ibSic   _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc != 2 ){
    Tcl_SetResult(interp, "Error: sic <dev> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibsic( atoi( argv[1])) & ERR ){
    ib_CreateVerboseError(interp,"ibsic");
    return TCL_ERROR;
  }
     return TCL_OK;

}
/**********************************************************************/
int ibClr   _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc != 2 ){
    Tcl_SetResult(interp, "Error: clear <dev> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibclr( atoi( argv[1])) & ERR ){
    ib_CreateVerboseError(interp,"ibclr");
    return TCL_ERROR;
  }
     return TCL_OK;
}
/**********************************************************************/
int ibOnl   _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc != 3 ){
    Tcl_SetResult(interp, "Error: onl <dev> <val>", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibonl( atoi( argv[1]),atoi(argv[2])) & ERR ){
    ib_CreateVerboseError(interp,"ibonl");
    return TCL_ERROR;
  }
     return TCL_OK;
}

/**********************************************************************/
int ibWait   _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  int mask=0;
  register int i;

  if( argc < 3 ){
    Tcl_SetResult(interp, "Error: wait <dev> <what>", TCL_STATIC);
    return TCL_ERROR;
  }

  for( i=2; i<argc; i++ ){

    if( *argv[i] == 's' && !strcmp(argv[i],"srq"))  mask |= SRQI;
    else if( *argv[i] == 'c' && !strcmp(argv[i],"cmpl")) mask |= CMPL;
    else if( *argv[i] == 'r' && !strcmp(argv[i],"rqs"))  mask |= RQS;
    else if( *argv[i] == 'c' && !strcmp(argv[i],"cic"))  mask |= CIC;
    else if( *argv[i] == 'a' && !strcmp(argv[i],"atn"))  mask |= ATN;
    else if( *argv[i] == 't' && !strcmp(argv[i],"timo")) mask |= TIMO;

    else {
      Tcl_SetResult(interp, "Wait: illegal Argument", TCL_STATIC );
      return TCL_ERROR;
    }

  }

  if( ibwait( atoi( argv[1]),SRQI) & ERR ){
    ib_CreateVerboseError(interp,"ibwait");
    return TCL_ERROR;
  }
     return TCL_OK;
}
/**********************************************************************/
int ibClose  _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){


  if( argc < 2){
    Tcl_SetResult(interp, "Error: close <dev> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibonl( atoi( argv[1]),0 ) & ERR ){
    ib_CreateVerboseError(interp,"ibclose");
    return TCL_ERROR;
  }
  return TCL_OK;
}

/**********************************************************************/
int ibRsp  _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){


  char spb;
  char spr[5];

  if( argc < 2){
    Tcl_SetResult(interp, "Error: rsp <dev> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibrsp( atoi( argv[1]), &spb ) & ERR ){
    ib_CreateVerboseError(interp,"ibrsp");
    return TCL_ERROR;
  }
  sprintf(spr,"%3d",spb); /* convert serial response to integer string */
  Tcl_AppendResult(interp,spr,(char *)NULL);
  return TCL_OK;
}
/**********************************************************************/
int ibTrg  _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc < 2){
    Tcl_SetResult(interp, "Error: trg <dev> ", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibtrg( atoi( argv[1]) ) & ERR ){
    ib_CreateVerboseError(interp,"ibtrg");
    return TCL_ERROR;
  }
  return TCL_OK;
}
/**********************************************************************/
int ibRsv  _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,const char*argv[])){

  if( argc < 2){
    Tcl_SetResult(interp, "Error: rsv <dev> <val>", TCL_STATIC);
    return TCL_ERROR;
  }

  if( ibrsv( atoi( argv[1]) , (char) atoi( argv[2] ) ) & ERR ){
    ib_CreateVerboseError(interp,"ibrsv");
    return TCL_ERROR;
  }
  return TCL_OK;
}
/**********************************************************************/




static char errbuf[80]; 

void ib_CreateVerboseError(Tcl_Interp *interp,char *entry ){

strcpy(errbuf,entry);

strcat(errbuf,": \nIBSTAT = <");

  if ( ibsta & ERR )  strcat(errbuf," ERR");
  if ( ibsta & TIMO ) strcat(errbuf," | TIMO");
  if ( ibsta & END )  strcat(errbuf," | END"); 
  if ( ibsta & SRQI ) strcat(errbuf," | SRQI");
  if ( ibsta & RQS )  strcat(errbuf," | RQS");
  if ( ibsta & CMPL ) strcat(errbuf," | CMPL");
/*if ( ibsta & LOK )  strcat(errbuf," | LOK");*/
/*if ( ibsta & REM )  strcat(errbuf," | REM");*/ 
  if ( ibsta & CIC )  strcat(errbuf," | CIC"); 
  if ( ibsta & ATN )  strcat(errbuf," | ATM");
  if ( ibsta & TACS ) strcat(errbuf," | TACS");
  if ( ibsta & LACS ) strcat(errbuf," | LACS");
/*if ( ibsta & DTAS ) strcat(errbuf," | DATS");*/
/*if ( ibsta & DCAS ) strcat(errbuf," | DCTS");*/

strcat(errbuf,"> \nIBERR = ");

if ( iberr == EDVR) strcat(errbuf," EDVR <OS Error>");
if ( iberr == ECIC) strcat(errbuf," ECIC <Not CIC>");
if ( iberr == ENOL) strcat(errbuf," ENOL <No Listener>");
if ( iberr == EADR) strcat(errbuf," EADR <Adress Error>");
if ( iberr == EARG) strcat(errbuf," ECIC <Invalid Argument>");
if ( iberr == ESAC) strcat(errbuf," ESAC <No Sys Ctrlr>");
if ( iberr == EABO) strcat(errbuf," EABO <Operation Aborted>");
if ( iberr == ENEB) strcat(errbuf," ENEB <No Gpib Board>");
if ( iberr == EOIP) strcat(errbuf," EOIP <Async I/O in prg>");
if ( iberr == ECAP) strcat(errbuf," ECAP <No Capability>");
if ( iberr == EFSO) strcat(errbuf," EFSO <File sys. error>");
if ( iberr == EBUS) strcat(errbuf," EBUS <Command error>");
if ( iberr == ESTB) strcat(errbuf," ESTB <Status byte lost>");
if ( iberr == ESRQ) strcat(errbuf," ESRQ <SRQ stuck on>");
if ( iberr == ETAB) strcat(errbuf," ETAB <Device Table Overflow>");



Tcl_AppendResult(interp, errbuf , (char *) NULL );

}







/**********************************************************************/


int gpibCmd _ANSI_ARGS_(( ClientData clientData,
			      Tcl_Interp *interp,
			       int argc,
			       const char *argv[]
			       ))
{
 
	if(argc < 2)
	{
		Tcl_SetResult(interp,"Error: unspecified gpib command",TCL_STATIC);
		return TCL_ERROR;
	}

if( !strcmp(argv[1],"dev")){
  return ibDev( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"find")){
  return ibFind( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"read")){
  return ibRead( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"write")){
  return ibWrite( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"online")){
  return ibOnl( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"clear")){
  return ibClr( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"ren")){
  return ibSre( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"sic")){
  return ibSic( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"cmd")){
  return ibCmd( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"wait")){
  return ibWait( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"close")){
  return ibClose( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"rsp")){
  return ibRsp( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"rsv")){
  return ibRsv( clientData, interp, argc-1,argv+1 );
}
if( !strcmp(argv[1],"trg")){
  return ibTrg( clientData, interp, argc-1,argv+1 );
}

Tcl_SetResult(interp,"Error: unrecognized gpib command",TCL_STATIC);
return TCL_ERROR;

}