gpib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * gpib.c - guile binding for LinuxGpib
  3. *
  4. * Copyright (C) 2003 Stefan Jahn <stefan@lkcc.org>
  5. *
  6. * LinuxGpib is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * LinuxGpib is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with LinuxGpib; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <guile/gh.h>
  25. #include <gpib/ib.h>
  26. #include "gpib.h"
  27. #define FUNC_NAME "ibdev"
  28. SCM
  29. guile_ibdev (SCM board_index, SCM pad, SCM sad, SCM timeout, SCM eoi, SCM eos)
  30. {
  31. int fd, _eoi;
  32. SCM_ASSERT_TYPE (SCM_EXACTP (board_index), board_index,
  33. SCM_ARG1, FUNC_NAME, "exact");
  34. SCM_ASSERT_TYPE (SCM_EXACTP (pad), pad,
  35. SCM_ARG2, FUNC_NAME, "exact");
  36. SCM_ASSERT_TYPE (SCM_EXACTP (sad), sad,
  37. SCM_ARG3, FUNC_NAME, "exact");
  38. SCM_ASSERT_TYPE (SCM_EXACTP (timeout), timeout,
  39. SCM_ARG4, FUNC_NAME, "exact");
  40. SCM_ASSERT_TYPE (SCM_EXACTP (eoi) || SCM_BOOLP (eoi), eoi,
  41. SCM_ARG5, FUNC_NAME, "exact or bool");
  42. SCM_ASSERT_TYPE (SCM_EXACTP (eos), eos,
  43. SCM_ARG6, FUNC_NAME, "exact");
  44. if (SCM_BOOLP (eoi))
  45. _eoi = SCM_NFALSEP (eoi);
  46. else
  47. _eoi = SCM_NUM2INT (SCM_ARG5, eoi);
  48. if ((fd = ibdev (SCM_NUM2INT (SCM_ARG1, board_index),
  49. SCM_NUM2INT (SCM_ARG2, pad),
  50. SCM_NUM2INT (SCM_ARG3, sad),
  51. SCM_NUM2INT (SCM_ARG4, timeout),
  52. _eoi,
  53. SCM_NUM2INT (SCM_ARG6, eos))) < 0) {
  54. scm_syserror (FUNC_NAME);
  55. }
  56. return scm_int2num (fd);
  57. }
  58. #undef FUNC_NAME
  59. #define FUNC_NAME "ibwrt"
  60. SCM
  61. guile_ibwrt (SCM ud, SCM data)
  62. {
  63. int ret;
  64. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  65. SCM_ARG1, FUNC_NAME, "exact");
  66. SCM_ASSERT_TYPE (SCM_STRINGP (data), data,
  67. SCM_ARG2, FUNC_NAME, "string");
  68. if ((ret = ibwrt (SCM_NUM2INT (SCM_ARG1, ud),
  69. SCM_STRING_CHARS (data),
  70. SCM_NUM2INT (SCM_ARG2,
  71. scm_string_length (data)))) & ERR) {
  72. return SCM_BOOL_F;
  73. }
  74. return scm_int2num (ret);
  75. }
  76. #undef FUNC_NAME
  77. #define FUNC_NAME "ibcmd"
  78. SCM
  79. guile_ibcmd (SCM ud, SCM commands)
  80. {
  81. int ret;
  82. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  83. SCM_ARG1, FUNC_NAME, "exact");
  84. SCM_ASSERT_TYPE (SCM_STRINGP (commands), commands,
  85. SCM_ARG2, FUNC_NAME, "string");
  86. if ((ret = ibcmd (SCM_NUM2INT (SCM_ARG1, ud),
  87. SCM_STRING_CHARS (commands),
  88. SCM_NUM2INT (SCM_ARG2,
  89. scm_string_length (commands)))) & ERR) {
  90. return SCM_BOOL_F;
  91. }
  92. return scm_int2num (ret);
  93. }
  94. #undef FUNC_NAME
  95. #define FUNC_NAME "ibrd"
  96. SCM
  97. guile_ibrd (SCM ud, SCM bytes)
  98. {
  99. int ret, len;
  100. char *data;
  101. SCM val;
  102. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  103. SCM_ARG1, FUNC_NAME, "exact");
  104. SCM_ASSERT_TYPE (SCM_EXACTP (bytes), bytes,
  105. SCM_ARG2, FUNC_NAME, "exact");
  106. len = SCM_NUM2INT (SCM_ARG2, bytes);
  107. if ((data = (char *) malloc (len)) == NULL) {
  108. scm_memory_error (FUNC_NAME);
  109. }
  110. if ((ret = ibrd (SCM_NUM2INT (SCM_ARG1, ud), data, len)) & ERR) {
  111. free (data);
  112. return SCM_BOOL_F;
  113. }
  114. val = scm_mem2string (data, ibcnt);
  115. free (data);
  116. return val;
  117. }
  118. #undef FUNC_NAME
  119. #define FUNC_NAME "ibfind"
  120. SCM
  121. guile_ibfind (SCM name)
  122. {
  123. char *str;
  124. int ret;
  125. SCM_ASSERT_TYPE (SCM_STRINGP (name), name,
  126. SCM_ARG1, FUNC_NAME, "string");
  127. str = guile_to_string (name);
  128. if ((ret = ibfind (str)) < 0) {
  129. free (str);
  130. return SCM_BOOL_F;
  131. }
  132. free (str);
  133. return scm_int2num (ret);
  134. }
  135. #undef FUNC_NAME
  136. #define FUNC_NAME "ibsre"
  137. SCM
  138. guile_ibsre (SCM ud, SCM enable)
  139. {
  140. int ret, val;
  141. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  142. SCM_ARG1, FUNC_NAME, "exact");
  143. SCM_ASSERT_TYPE (SCM_EXACTP (enable) || SCM_BOOLP (enable), enable,
  144. SCM_ARG2, FUNC_NAME, "exact or bool");
  145. if (SCM_BOOLP (enable))
  146. val = SCM_NFALSEP (enable);
  147. else
  148. val = SCM_NUM2INT (SCM_ARG2, enable);
  149. if ((ret = ibsre (SCM_NUM2INT (SCM_ARG1, ud), val)) & ERR) {
  150. return SCM_BOOL_F;
  151. }
  152. return scm_int2num (ret);
  153. }
  154. #undef FUNC_NAME
  155. #define FUNC_NAME "ibsic"
  156. SCM
  157. guile_ibsic (SCM ud)
  158. {
  159. int ret;
  160. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  161. SCM_ARG1, FUNC_NAME, "exact");
  162. if ((ret = ibsic (SCM_NUM2INT (SCM_ARG1, ud))) & ERR) {
  163. return SCM_BOOL_F;
  164. }
  165. return scm_int2num (ret);
  166. }
  167. #undef FUNC_NAME
  168. #define FUNC_NAME "ibclr"
  169. SCM
  170. guile_ibclr (SCM ud)
  171. {
  172. int ret;
  173. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  174. SCM_ARG1, FUNC_NAME, "exact");
  175. if ((ret = ibclr (SCM_NUM2INT (SCM_ARG1, ud))) & ERR) {
  176. return SCM_BOOL_F;
  177. }
  178. return scm_int2num (ret);
  179. }
  180. #undef FUNC_NAME
  181. #define FUNC_NAME "ibonl"
  182. SCM
  183. guile_ibonl (SCM ud, SCM online)
  184. {
  185. int ret, val;
  186. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  187. SCM_ARG1, FUNC_NAME, "exact");
  188. SCM_ASSERT_TYPE (SCM_EXACTP (online) || SCM_BOOLP (online), online,
  189. SCM_ARG2, FUNC_NAME, "exact or bool");
  190. if (SCM_BOOLP (online))
  191. val = SCM_NFALSEP (online);
  192. else
  193. val = SCM_NUM2INT (SCM_ARG2, online);
  194. if ((ret = ibonl (SCM_NUM2INT (SCM_ARG1, ud), val)) & ERR) {
  195. return SCM_BOOL_F;
  196. }
  197. return scm_int2num (ret);
  198. }
  199. #undef FUNC_NAME
  200. #define FUNC_NAME "ibwait"
  201. SCM
  202. guile_ibwait (SCM ud, SCM status_mask)
  203. {
  204. int ret;
  205. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  206. SCM_ARG1, FUNC_NAME, "exact");
  207. SCM_ASSERT_TYPE (SCM_EXACTP (status_mask), status_mask,
  208. SCM_ARG2, FUNC_NAME, "exact");
  209. if ((ret = ibwait (SCM_NUM2INT (SCM_ARG1, ud),
  210. SCM_NUM2INT (SCM_ARG2, status_mask))) & ERR) {
  211. return SCM_BOOL_F;
  212. }
  213. return scm_int2num (ret);
  214. }
  215. #undef FUNC_NAME
  216. #define FUNC_NAME "ibrsp"
  217. SCM
  218. guile_ibrsp (SCM ud)
  219. {
  220. int ret;
  221. char poll[16];
  222. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  223. SCM_ARG1, FUNC_NAME, "exact");
  224. if ((ret = ibrsp (SCM_NUM2INT (SCM_ARG1, ud), poll)) & ERR) {
  225. return SCM_BOOL_F;
  226. }
  227. return scm_int2num (poll[0]);
  228. }
  229. #undef FUNC_NAME
  230. #define FUNC_NAME "ibtrg"
  231. SCM
  232. guile_ibtrg (SCM ud)
  233. {
  234. int ret;
  235. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  236. SCM_ARG1, FUNC_NAME, "exact");
  237. if ((ret = ibtrg (SCM_NUM2INT (SCM_ARG1, ud))) & ERR) {
  238. return SCM_BOOL_F;
  239. }
  240. return scm_int2num (ret);
  241. }
  242. #undef FUNC_NAME
  243. #define FUNC_NAME "ibrsv"
  244. SCM
  245. guile_ibrsv (SCM ud, SCM status_byte)
  246. {
  247. int ret;
  248. SCM_ASSERT_TYPE (SCM_EXACTP (ud), ud,
  249. SCM_ARG1, FUNC_NAME, "exact");
  250. SCM_ASSERT_TYPE (SCM_EXACTP (status_byte), status_byte,
  251. SCM_ARG2, FUNC_NAME, "exact");
  252. if ((ret = ibrsv (SCM_NUM2INT (SCM_ARG1, ud),
  253. SCM_NUM2INT (SCM_ARG2, status_byte))) & ERR) {
  254. return SCM_BOOL_F;
  255. }
  256. return scm_int2num (ret);
  257. }
  258. #undef FUNC_NAME
  259. #define FUNC_NAME "iberr"
  260. SCM guile_iberr (void)
  261. {
  262. return scm_int2num (iberr);
  263. }
  264. #undef FUNC_NAME
  265. #define FUNC_NAME "ibcnt"
  266. SCM guile_ibcnt (void)
  267. {
  268. return scm_long2num (ibcntl);
  269. }
  270. #undef FUNC_NAME
  271. void
  272. gpib_bindings (void)
  273. {
  274. scm_c_define_gsubr ("ibdev", 6, 0, 0, guile_ibdev);
  275. scm_c_define_gsubr ("ibwrt", 2, 0, 0, guile_ibwrt);
  276. scm_c_define_gsubr ("ibcmd", 2, 0, 0, guile_ibcmd);
  277. scm_c_define_gsubr ("ibrd", 2, 0, 0, guile_ibrd);
  278. scm_c_define_gsubr ("ibfind", 1, 0, 0, guile_ibfind);
  279. scm_c_define_gsubr ("ibsre", 2, 0, 0, guile_ibsre);
  280. scm_c_define_gsubr ("ibsic", 1, 0, 0, guile_ibsic);
  281. scm_c_define_gsubr ("ibclr", 1, 0, 0, guile_ibclr);
  282. scm_c_define_gsubr ("ibonl", 2, 0, 0, guile_ibonl);
  283. scm_c_define_gsubr ("ibwait", 2, 0, 0, guile_ibwait);
  284. scm_c_define_gsubr ("ibrsp", 1, 0, 0, guile_ibrsp);
  285. scm_c_define_gsubr ("ibtrg", 1, 0, 0, guile_ibtrg);
  286. scm_c_define_gsubr ("ibrsv", 1, 0, 0, guile_ibrsv);
  287. scm_c_define_gsubr ("iberr", 0, 0, 0, guile_iberr);
  288. scm_c_define_gsubr ("ibcnt", 0, 0, 0, guile_ibcnt);
  289. scm_c_define ("DCAS", scm_int2num (DCAS));
  290. scm_c_define ("DTAS", scm_int2num (DTAS));
  291. scm_c_define ("LACS", scm_int2num (LACS));
  292. scm_c_define ("TACS", scm_int2num (TACS));
  293. scm_c_define ("ATN", scm_int2num (ATN));
  294. scm_c_define ("CIC", scm_int2num (CIC));
  295. scm_c_define ("REM", scm_int2num (REM));
  296. scm_c_define ("LOK", scm_int2num (LOK));
  297. scm_c_define ("CMPL", scm_int2num (CMPL));
  298. scm_c_define ("EVENT", scm_int2num (EVENT));
  299. scm_c_define ("SPOLL", scm_int2num (SPOLL));
  300. scm_c_define ("RQS", scm_int2num (RQS));
  301. scm_c_define ("SRQI", scm_int2num (SRQI));
  302. scm_c_define ("END", scm_int2num (END));
  303. scm_c_define ("TIMO", scm_int2num (TIMO));
  304. scm_c_define ("ERR", scm_int2num (ERR));
  305. scm_c_define ("GTL", scm_int2num (GTL));
  306. scm_c_define ("SDC", scm_int2num (SDC));
  307. scm_c_define ("PPC", scm_int2num (PPC));
  308. scm_c_define ("GET", scm_int2num (GET));
  309. scm_c_define ("TCT", scm_int2num (TCT));
  310. scm_c_define ("LLO", scm_int2num (LLO));
  311. scm_c_define ("DCL", scm_int2num (DCL));
  312. scm_c_define ("PPU", scm_int2num (PPU));
  313. scm_c_define ("SPE", scm_int2num (SPE));
  314. scm_c_define ("SPD", scm_int2num (SPD));
  315. scm_c_define ("UNL", scm_int2num (UNL));
  316. scm_c_define ("UNT", scm_int2num (UNT));
  317. scm_c_define ("PPD", scm_int2num (PPD));
  318. scm_c_define ("EDVR", scm_int2num (EDVR));
  319. scm_c_define ("ECIC", scm_int2num (ECIC));
  320. scm_c_define ("ENOL", scm_int2num (ENOL));
  321. scm_c_define ("EADR", scm_int2num (EADR));
  322. scm_c_define ("EARG", scm_int2num (EARG));
  323. scm_c_define ("ESAC", scm_int2num (ESAC));
  324. scm_c_define ("EABO", scm_int2num (EABO));
  325. scm_c_define ("ENEB", scm_int2num (ENEB));
  326. scm_c_define ("EOIP", scm_int2num (EOIP));
  327. scm_c_define ("ECAP", scm_int2num (ECAP));
  328. scm_c_define ("EFSO", scm_int2num (EFSO));
  329. scm_c_define ("EBUS", scm_int2num (EBUS));
  330. scm_c_define ("ESTB", scm_int2num (ESTB));
  331. scm_c_define ("ESRQ", scm_int2num (ESRQ));
  332. scm_c_define ("ETAB", scm_int2num (ETAB));
  333. scm_c_define ("REOS", scm_int2num (REOS));
  334. scm_c_define ("XEOS", scm_int2num (XEOS));
  335. scm_c_define ("BIN", scm_int2num (BIN));
  336. scm_c_define ("TNONE", scm_int2num (TNONE));
  337. scm_c_define ("T10us", scm_int2num (T10us));
  338. scm_c_define ("T30us", scm_int2num (T30us));
  339. scm_c_define ("T100us", scm_int2num (T100us));
  340. scm_c_define ("T300us", scm_int2num (T300us));
  341. scm_c_define ("T1ms", scm_int2num (T1ms));
  342. scm_c_define ("T3ms", scm_int2num (T3ms));
  343. scm_c_define ("T10ms", scm_int2num (T10ms));
  344. scm_c_define ("T30ms", scm_int2num (T30ms));
  345. scm_c_define ("T100ms", scm_int2num (T100ms));
  346. scm_c_define ("T300ms", scm_int2num (T300ms));
  347. scm_c_define ("T1s", scm_int2num (T1s));
  348. scm_c_define ("T3s", scm_int2num (T3s));
  349. scm_c_define ("T10s", scm_int2num (T10s));
  350. scm_c_define ("T30s", scm_int2num (T30s));
  351. scm_c_define ("T100s", scm_int2num (T100s));
  352. scm_c_define ("T300s", scm_int2num (T300s));
  353. scm_c_define ("T1000s", scm_int2num (T1000s));
  354. }
  355. extern void
  356. gpib_init ()
  357. {
  358. scm_add_feature ("gpib");
  359. gpib_bindings ();
  360. }