ibCmds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <tcl.h>
  5. #include <gpib/ib.h>
  6. void ib_CreateVerboseError(Tcl_Interp *interp,char *entry );
  7. int Gpib_tcl_Init ( Tcl_Interp *interp ){
  8. extern int gpibCmd _ANSI_ARGS_(( ClientData clientData,
  9. Tcl_Interp *interp,
  10. int argc,
  11. char *argv[]
  12. ));
  13. Tcl_CreateCommand(interp,"gpib",gpibCmd,
  14. (ClientData) NULL,
  15. (Tcl_CmdDeleteProc *) NULL );
  16. return TCL_OK;
  17. }
  18. /**********************************************************************/
  19. int ibWrite _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  20. if( argc != 3 ){
  21. Tcl_SetResult(interp, "Error: write <dev> <string> ", TCL_STATIC);
  22. return TCL_ERROR;
  23. }
  24. if( ibwrt( atoi( argv[1]) , argv[2], strlen(argv[2]) ) & ERR ){
  25. ib_CreateVerboseError(interp,"ibwrt");
  26. return TCL_ERROR;
  27. }
  28. return TCL_OK;
  29. }
  30. /**********************************************************************/
  31. int ibCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  32. if( argc != 3 ){
  33. Tcl_SetResult(interp, "Error: cmd <dev> <string> ", TCL_STATIC);
  34. return TCL_ERROR;
  35. }
  36. if( ibcmd( atoi( argv[1]) , argv[2], strlen(argv[2]) ) & ERR ){
  37. ib_CreateVerboseError(interp,"ibcmd");
  38. return TCL_ERROR;
  39. }
  40. return TCL_OK;
  41. }
  42. /**********************************************************************/
  43. int ibRead _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  44. char *buf;
  45. int len;
  46. int desc;
  47. if( argc != 3 ){
  48. Tcl_SetResult(interp, "Error: read <dev> <num bytes>", TCL_STATIC);
  49. return TCL_ERROR;
  50. }
  51. desc = strtol(argv[1], NULL, 0);
  52. len = strtol(argv[2], NULL, 0);
  53. if ((buf = (char *)malloc( len + 1 )) == NULL) {
  54. Tcl_SetResult(interp, "Error: Out of Memory", TCL_STATIC);
  55. return TCL_ERROR;
  56. }
  57. if( ibrd( desc , buf, len ) & ERR ){
  58. /* ib_CreateVerboseError(interp,"ibrd");
  59. return TCL_ERROR;
  60. */
  61. Tcl_AppendResult(interp, "ERROR" , (char *) NULL );
  62. free(buf);
  63. return TCL_ERROR;
  64. }
  65. buf[ibcnt] = '\0';
  66. Tcl_AppendResult(interp, buf, (char *) NULL );
  67. free(buf);
  68. return TCL_OK;
  69. }
  70. /**********************************************************************/
  71. int ibDev _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]))
  72. {
  73. int dev;
  74. char res[10];
  75. int minor, pad, sad, timo, eot, eos;
  76. if( argc != 7 )
  77. {
  78. Tcl_SetResult(interp, "Error: dev <minor> <pad> <sad> <timo> <eot> <eosmode>", TCL_STATIC);
  79. return TCL_ERROR;
  80. }
  81. minor = strtol(argv[1], NULL, 0);
  82. pad = strtol(argv[2], NULL, 0);
  83. sad = strtol(argv[3], NULL, 0);
  84. timo = strtol(argv[4], NULL, 0);
  85. eot = strtol(argv[5], NULL, 0);
  86. eos = strtol(argv[6], NULL, 0);
  87. if(( dev = ibdev(minor, pad, sad, timo, eot, eos)) < 0)
  88. {
  89. ib_CreateVerboseError(interp,"ibdev");
  90. return TCL_ERROR;
  91. }
  92. sprintf(res, "%4d", dev);
  93. Tcl_SetResult( interp, res, TCL_VOLATILE );
  94. return TCL_OK;
  95. }
  96. /**********************************************************************/
  97. int ibFind _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[]))
  98. {
  99. int dev;
  100. char res[10];
  101. if( argc != 2 )
  102. {
  103. Tcl_SetResult(interp, "Error: find <string>", TCL_STATIC);
  104. return TCL_ERROR;
  105. }
  106. if(( dev = ibfind(argv[1])) < 0 )
  107. {
  108. ib_CreateVerboseError(interp,"ibfind");
  109. return TCL_ERROR;
  110. }
  111. sprintf(res, "%4d", dev);
  112. Tcl_SetResult( interp, res, TCL_VOLATILE );
  113. return TCL_OK;
  114. }
  115. /**********************************************************************/
  116. int ibSre _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  117. if( argc != 3 ){
  118. Tcl_SetResult(interp, "Error: ibsre <bool> ", TCL_STATIC);
  119. return TCL_ERROR;
  120. }
  121. if( ibsre( atoi( argv[1]) , atoi( argv[2] )) & ERR ){
  122. ib_CreateVerboseError(interp,"ibsre");
  123. return TCL_ERROR;
  124. }
  125. return TCL_OK;
  126. }
  127. /**********************************************************************/
  128. int ibSic _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  129. if( argc != 2 ){
  130. Tcl_SetResult(interp, "Error: sic <dev> ", TCL_STATIC);
  131. return TCL_ERROR;
  132. }
  133. if( ibsic( atoi( argv[1])) & ERR ){
  134. ib_CreateVerboseError(interp,"ibsic");
  135. return TCL_ERROR;
  136. }
  137. return TCL_OK;
  138. }
  139. /**********************************************************************/
  140. int ibClr _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  141. if( argc != 2 ){
  142. Tcl_SetResult(interp, "Error: clear <dev> ", TCL_STATIC);
  143. return TCL_ERROR;
  144. }
  145. if( ibclr( atoi( argv[1])) & ERR ){
  146. ib_CreateVerboseError(interp,"ibclr");
  147. return TCL_ERROR;
  148. }
  149. return TCL_OK;
  150. }
  151. /**********************************************************************/
  152. int ibOnl _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  153. if( argc != 3 ){
  154. Tcl_SetResult(interp, "Error: onl <dev> <val>", TCL_STATIC);
  155. return TCL_ERROR;
  156. }
  157. if( ibonl( atoi( argv[1]),atoi(argv[2])) & ERR ){
  158. ib_CreateVerboseError(interp,"ibonl");
  159. return TCL_ERROR;
  160. }
  161. return TCL_OK;
  162. }
  163. /**********************************************************************/
  164. int ibWait _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  165. int mask=0;
  166. register int i;
  167. if( argc < 3 ){
  168. Tcl_SetResult(interp, "Error: wait <dev> <what>", TCL_STATIC);
  169. return TCL_ERROR;
  170. }
  171. for( i=2; i<argc; i++ ){
  172. if( *argv[i] == 's' && !strcmp(argv[i],"srq")) mask |= SRQI;
  173. else if( *argv[i] == 'c' && !strcmp(argv[i],"cmpl")) mask |= CMPL;
  174. else if( *argv[i] == 'r' && !strcmp(argv[i],"rqs")) mask |= RQS;
  175. else if( *argv[i] == 'c' && !strcmp(argv[i],"cic")) mask |= CIC;
  176. else if( *argv[i] == 'a' && !strcmp(argv[i],"atn")) mask |= ATN;
  177. else if( *argv[i] == 't' && !strcmp(argv[i],"timo")) mask |= TIMO;
  178. else {
  179. Tcl_SetResult(interp, "Wait: illegal Argument", TCL_STATIC );
  180. return TCL_ERROR;
  181. }
  182. }
  183. if( ibwait( atoi( argv[1]),SRQI) & ERR ){
  184. ib_CreateVerboseError(interp,"ibwait");
  185. return TCL_ERROR;
  186. }
  187. return TCL_OK;
  188. }
  189. /**********************************************************************/
  190. int ibClose _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  191. if( argc < 2){
  192. Tcl_SetResult(interp, "Error: close <dev> ", TCL_STATIC);
  193. return TCL_ERROR;
  194. }
  195. if( ibonl( atoi( argv[1]),0 ) & ERR ){
  196. ib_CreateVerboseError(interp,"ibclose");
  197. return TCL_ERROR;
  198. }
  199. return TCL_OK;
  200. }
  201. /**********************************************************************/
  202. int ibRsp _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  203. char spb;
  204. char spr[5];
  205. if( argc < 2){
  206. Tcl_SetResult(interp, "Error: rsp <dev> ", TCL_STATIC);
  207. return TCL_ERROR;
  208. }
  209. if( ibrsp( atoi( argv[1]), &spb ) & ERR ){
  210. ib_CreateVerboseError(interp,"ibrsp");
  211. return TCL_ERROR;
  212. }
  213. sprintf(spr,"%3d",spb); /* convert serial response to integer string */
  214. Tcl_AppendResult(interp,spr,(char *)NULL);
  215. return TCL_OK;
  216. }
  217. /**********************************************************************/
  218. int ibTrg _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  219. if( argc < 2){
  220. Tcl_SetResult(interp, "Error: trg <dev> ", TCL_STATIC);
  221. return TCL_ERROR;
  222. }
  223. if( ibtrg( atoi( argv[1]) ) & ERR ){
  224. ib_CreateVerboseError(interp,"ibtrg");
  225. return TCL_ERROR;
  226. }
  227. return TCL_OK;
  228. }
  229. /**********************************************************************/
  230. int ibRsv _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc,char *argv[])){
  231. if( argc < 2){
  232. Tcl_SetResult(interp, "Error: rsv <dev> <val>", TCL_STATIC);
  233. return TCL_ERROR;
  234. }
  235. if( ibrsv( atoi( argv[1]) , (char) atoi( argv[2] ) ) & ERR ){
  236. ib_CreateVerboseError(interp,"ibrsv");
  237. return TCL_ERROR;
  238. }
  239. return TCL_OK;
  240. }
  241. /**********************************************************************/
  242. static char errbuf[80];
  243. void ib_CreateVerboseError(Tcl_Interp *interp,char *entry ){
  244. strcpy(errbuf,entry);
  245. strcat(errbuf,": \nIBSTAT = <");
  246. if ( ibsta & ERR ) strcat(errbuf," ERR");
  247. if ( ibsta & TIMO ) strcat(errbuf," | TIMO");
  248. if ( ibsta & END ) strcat(errbuf," | END");
  249. if ( ibsta & SRQI ) strcat(errbuf," | SRQI");
  250. if ( ibsta & RQS ) strcat(errbuf," | RQS");
  251. if ( ibsta & CMPL ) strcat(errbuf," | CMPL");
  252. /*if ( ibsta & LOK ) strcat(errbuf," | LOK");*/
  253. /*if ( ibsta & REM ) strcat(errbuf," | REM");*/
  254. if ( ibsta & CIC ) strcat(errbuf," | CIC");
  255. if ( ibsta & ATN ) strcat(errbuf," | ATM");
  256. if ( ibsta & TACS ) strcat(errbuf," | TACS");
  257. if ( ibsta & LACS ) strcat(errbuf," | LACS");
  258. /*if ( ibsta & DTAS ) strcat(errbuf," | DATS");*/
  259. /*if ( ibsta & DCAS ) strcat(errbuf," | DCTS");*/
  260. strcat(errbuf,"> \nIBERR = ");
  261. if ( iberr == EDVR) strcat(errbuf," EDVR <OS Error>");
  262. if ( iberr == ECIC) strcat(errbuf," ECIC <Not CIC>");
  263. if ( iberr == ENOL) strcat(errbuf," ENOL <No Listener>");
  264. if ( iberr == EADR) strcat(errbuf," EADR <Adress Error>");
  265. if ( iberr == EARG) strcat(errbuf," ECIC <Invalid Argument>");
  266. if ( iberr == ESAC) strcat(errbuf," ESAC <No Sys Ctrlr>");
  267. if ( iberr == EABO) strcat(errbuf," EABO <Operation Aborted>");
  268. if ( iberr == ENEB) strcat(errbuf," ENEB <No Gpib Board>");
  269. if ( iberr == EOIP) strcat(errbuf," EOIP <Async I/O in prg>");
  270. if ( iberr == ECAP) strcat(errbuf," ECAP <No Capability>");
  271. if ( iberr == EFSO) strcat(errbuf," EFSO <File sys. error>");
  272. if ( iberr == EBUS) strcat(errbuf," EBUS <Command error>");
  273. if ( iberr == ESTB) strcat(errbuf," ESTB <Status byte lost>");
  274. if ( iberr == ESRQ) strcat(errbuf," ESRQ <SRQ stuck on>");
  275. if ( iberr == ETAB) strcat(errbuf," ETAB <Device Table Overflow>");
  276. Tcl_AppendResult(interp, errbuf , (char *) NULL );
  277. }
  278. /**********************************************************************/
  279. int gpibCmd _ANSI_ARGS_(( ClientData clientData,
  280. Tcl_Interp *interp,
  281. int argc,
  282. char *argv[]
  283. ))
  284. {
  285. if(argc < 2)
  286. {
  287. Tcl_SetResult(interp,"Error: unspecified gpib command",TCL_STATIC);
  288. return TCL_ERROR;
  289. }
  290. if( !strcmp(argv[1],"dev")){
  291. return ibDev( clientData, interp, argc-1,argv+1 );
  292. }
  293. if( !strcmp(argv[1],"find")){
  294. return ibFind( clientData, interp, argc-1,argv+1 );
  295. }
  296. if( !strcmp(argv[1],"read")){
  297. return ibRead( clientData, interp, argc-1,argv+1 );
  298. }
  299. if( !strcmp(argv[1],"write")){
  300. return ibWrite( clientData, interp, argc-1,argv+1 );
  301. }
  302. if( !strcmp(argv[1],"online")){
  303. return ibOnl( clientData, interp, argc-1,argv+1 );
  304. }
  305. if( !strcmp(argv[1],"clear")){
  306. return ibClr( clientData, interp, argc-1,argv+1 );
  307. }
  308. if( !strcmp(argv[1],"ren")){
  309. return ibSre( clientData, interp, argc-1,argv+1 );
  310. }
  311. if( !strcmp(argv[1],"sic")){
  312. return ibSic( clientData, interp, argc-1,argv+1 );
  313. }
  314. if( !strcmp(argv[1],"cmd")){
  315. return ibCmd( clientData, interp, argc-1,argv+1 );
  316. }
  317. if( !strcmp(argv[1],"wait")){
  318. return ibWait( clientData, interp, argc-1,argv+1 );
  319. }
  320. if( !strcmp(argv[1],"close")){
  321. return ibClose( clientData, interp, argc-1,argv+1 );
  322. }
  323. if( !strcmp(argv[1],"rsp")){
  324. return ibRsp( clientData, interp, argc-1,argv+1 );
  325. }
  326. if( !strcmp(argv[1],"rsv")){
  327. return ibRsv( clientData, interp, argc-1,argv+1 );
  328. }
  329. if( !strcmp(argv[1],"trg")){
  330. return ibTrg( clientData, interp, argc-1,argv+1 );
  331. }
  332. Tcl_SetResult(interp,"Error: unrecognized gpib command",TCL_STATIC);
  333. return TCL_ERROR;
  334. }