serialport 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,409 @@
1
+ /* Ruby/SerialPort $Id$
2
+ * Guillaume Pierronnet <moumar@netcourrier.com>
3
+ * Alan Stern <stern@rowland.harvard.edu>
4
+ * Daniel E. Shipton <dshipton@redshiptechnologies.com>
5
+ *
6
+ * This code is hereby licensed for public consumption under either the
7
+ * GNU GPL v2 or greater.
8
+ *
9
+ * You should have received a copy of the GNU General Public License
10
+ * along with this program; if not, write to the Free Software
11
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12
+ *
13
+ * For documentation on serial programming, see the excellent:
14
+ * "Serial Programming Guide for POSIX Operating Systems"
15
+ * written Michael R. Sweet.
16
+ * http://www.easysw.com/~mike/serial/
17
+ */
18
+
19
+ #include "serialport.h"
20
+
21
+ VALUE cSerialPort; /* serial port class */
22
+
23
+ VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
24
+ VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
25
+
26
+ /*
27
+ */
28
+ static VALUE sp_create(class, _port)
29
+ VALUE class, _port;
30
+ {
31
+ return sp_create_impl(class, _port);
32
+ }
33
+
34
+ /*
35
+ */
36
+ static VALUE sp_set_modem_params(argc, argv, self)
37
+ int argc;
38
+ VALUE *argv, self;
39
+ {
40
+ return sp_set_modem_params_impl(argc, argv, self);
41
+ }
42
+
43
+ /*
44
+ */
45
+ static VALUE sp_break(self, time)
46
+ VALUE self, time;
47
+ {
48
+ return sp_break_impl(self, time);
49
+ }
50
+
51
+ /*
52
+ */
53
+ static VALUE sp_get_dtr(self)
54
+ VALUE self;
55
+ {
56
+ return sp_get_dtr_impl(self);
57
+ }
58
+
59
+ /*
60
+ */
61
+ static VALUE sp_get_flow_control(self)
62
+ VALUE self;
63
+ {
64
+ return sp_get_flow_control_impl(self);
65
+ }
66
+
67
+ /*
68
+ */
69
+ static VALUE sp_get_read_timeout(self)
70
+ VALUE self;
71
+ {
72
+ return sp_get_read_timeout_impl(self);
73
+ }
74
+
75
+ /*
76
+ */
77
+ static VALUE sp_get_rts(self)
78
+ VALUE self;
79
+ {
80
+ return sp_get_rts_impl(self);
81
+ }
82
+
83
+ /*
84
+ */
85
+ static VALUE sp_get_write_timeout(self)
86
+ VALUE self;
87
+ {
88
+ return sp_get_write_timeout_impl(self);
89
+ }
90
+
91
+ /*
92
+ */
93
+ static VALUE sp_set_dtr(self, val)
94
+ {
95
+ return sp_set_dtr_impl(self, val);
96
+ }
97
+
98
+ /*
99
+ */
100
+ static VALUE sp_set_flow_control(self, val)
101
+ {
102
+ return sp_set_flow_control_impl(self, val);
103
+ }
104
+
105
+ /*
106
+ */
107
+ static VALUE sp_set_read_timeout(self, val)
108
+ {
109
+ return sp_set_read_timeout_impl(self, val);
110
+ }
111
+
112
+ /*
113
+ */
114
+ static VALUE sp_set_rts(self, val)
115
+ {
116
+ return sp_set_rts_impl(self, val);
117
+ }
118
+
119
+ /*
120
+ */
121
+ static VALUE sp_set_write_timeout(self, val)
122
+ {
123
+ return sp_set_write_timeout_impl(self, val);
124
+ }
125
+
126
+ /*
127
+ */
128
+ static void get_modem_params(self, mp)
129
+ VALUE self;
130
+ struct modem_params *mp;
131
+ {
132
+ get_modem_params_impl(self, mp);
133
+ }
134
+
135
+ /*
136
+ */
137
+ static VALUE sp_set_data_rate(self, data_rate)
138
+ VALUE self, data_rate;
139
+ {
140
+ VALUE argv[4];
141
+
142
+ argv[0] = data_rate;
143
+ argv[1] = argv[2] = argv[3] = Qnil;
144
+ sp_set_modem_params(4, argv, self);
145
+
146
+ return data_rate;
147
+ }
148
+
149
+ /*
150
+ */
151
+ static VALUE sp_set_data_bits(self, data_bits)
152
+ VALUE self, data_bits;
153
+ {
154
+ VALUE argv[4];
155
+
156
+ argv[1] = data_bits;
157
+ argv[0] = argv[2] = argv[3] = Qnil;
158
+ sp_set_modem_params(4, argv, self);
159
+
160
+ return data_bits;
161
+ }
162
+
163
+ /*
164
+ */
165
+ static VALUE sp_set_stop_bits(self, stop_bits)
166
+ VALUE self, stop_bits;
167
+ {
168
+ VALUE argv[4];
169
+
170
+ argv[2] = stop_bits;
171
+ argv[0] = argv[1] = argv[3] = Qnil;
172
+ sp_set_modem_params(4, argv, self);
173
+
174
+ return stop_bits;
175
+ }
176
+
177
+ /*
178
+ */
179
+ static VALUE sp_set_parity(self, parity)
180
+ VALUE self, parity;
181
+ {
182
+ VALUE argv[4];
183
+
184
+ argv[3] = parity;
185
+ argv[0] = argv[1] = argv[2] = Qnil;
186
+ sp_set_modem_params(4, argv, self);
187
+
188
+ return parity;
189
+ }
190
+
191
+ /*
192
+ */
193
+ static VALUE sp_get_data_rate(self)
194
+ VALUE self;
195
+ {
196
+ struct modem_params mp;
197
+
198
+ get_modem_params(self, &mp);
199
+
200
+ return INT2FIX(mp.data_rate);
201
+ }
202
+
203
+ /*
204
+ */
205
+ static VALUE sp_get_data_bits(self)
206
+ VALUE self;
207
+ {
208
+ struct modem_params mp;
209
+
210
+ get_modem_params(self, &mp);
211
+
212
+ return INT2FIX(mp.data_bits);
213
+ }
214
+
215
+ /*
216
+ */
217
+ static VALUE sp_get_stop_bits(self)
218
+ VALUE self;
219
+ {
220
+ struct modem_params mp;
221
+
222
+ get_modem_params(self, &mp);
223
+
224
+ return INT2FIX(mp.stop_bits);
225
+ }
226
+
227
+ /*
228
+ */
229
+ static VALUE sp_get_parity(self)
230
+ VALUE self;
231
+ {
232
+ struct modem_params mp;
233
+
234
+ get_modem_params(self, &mp);
235
+
236
+ return INT2FIX(mp.parity);
237
+ }
238
+
239
+ /*
240
+ */
241
+ static VALUE sp_get_modem_params(self)
242
+ VALUE self;
243
+ {
244
+ struct modem_params mp;
245
+ VALUE hash;
246
+
247
+ get_modem_params(self, &mp);
248
+
249
+ hash = rb_hash_new();
250
+
251
+ rb_hash_aset(hash, sBaud, INT2FIX(mp.data_rate));
252
+ rb_hash_aset(hash, sDataBits, INT2FIX(mp.data_bits));
253
+ rb_hash_aset(hash, sStopBits, INT2FIX(mp.stop_bits));
254
+ rb_hash_aset(hash, sParity, INT2FIX(mp.parity));
255
+
256
+ return hash;
257
+ }
258
+
259
+ void get_line_signals_helper(obj, ls)
260
+ VALUE obj;
261
+ struct line_signals *ls;
262
+ {
263
+ get_line_signals_helper_impl(obj, ls);
264
+ }
265
+
266
+ /*
267
+ */
268
+ static VALUE sp_get_cts(self)
269
+ VALUE self;
270
+ {
271
+ struct line_signals ls;
272
+
273
+ get_line_signals_helper(self, &ls);
274
+
275
+ return INT2FIX(ls.cts);
276
+ }
277
+
278
+ /*
279
+ */
280
+ static VALUE sp_get_dsr(self)
281
+ VALUE self;
282
+ {
283
+ struct line_signals ls;
284
+
285
+ get_line_signals_helper(self, &ls);
286
+
287
+ return INT2FIX(ls.dsr);
288
+ }
289
+
290
+ /*
291
+ */
292
+ static VALUE sp_get_dcd(self)
293
+ VALUE self;
294
+ {
295
+ struct line_signals ls;
296
+
297
+ get_line_signals_helper(self, &ls);
298
+
299
+ return INT2FIX(ls.dcd);
300
+ }
301
+
302
+ /*
303
+ */
304
+ static VALUE sp_get_ri(self)
305
+ VALUE self;
306
+ {
307
+ struct line_signals ls;
308
+
309
+ get_line_signals_helper(self, &ls);
310
+
311
+ return INT2FIX(ls.ri);
312
+ }
313
+
314
+ /*
315
+ */
316
+ static VALUE sp_signals(self)
317
+ VALUE self;
318
+ {
319
+ struct line_signals ls;
320
+ VALUE hash;
321
+
322
+ get_line_signals_helper(self, &ls);
323
+
324
+ hash = rb_hash_new();
325
+
326
+ #if !(defined(OS_MSWIN) || defined(OS_BCCWIN) || defined(OS_MINGW))
327
+ rb_hash_aset(hash, sRts, INT2FIX(ls.rts));
328
+ rb_hash_aset(hash, sDtr, INT2FIX(ls.dtr));
329
+ #endif
330
+ rb_hash_aset(hash, sCts, INT2FIX(ls.cts));
331
+ rb_hash_aset(hash, sDsr, INT2FIX(ls.dsr));
332
+ rb_hash_aset(hash, sDcd, INT2FIX(ls.dcd));
333
+ rb_hash_aset(hash, sRi, INT2FIX(ls.ri));
334
+
335
+ return hash;
336
+ }
337
+
338
+ void Init_serialport()
339
+ {
340
+ sBaud = rb_str_new2("baud");
341
+ sDataBits = rb_str_new2("data_bits");
342
+ sStopBits = rb_str_new2("stop_bits");
343
+ sParity = rb_str_new2("parity");
344
+ sRts = rb_str_new2("rts");
345
+ sDtr = rb_str_new2("dtr");
346
+ sCts = rb_str_new2("cts");
347
+ sDsr = rb_str_new2("dsr");
348
+ sDcd = rb_str_new2("dcd");
349
+ sRi = rb_str_new2("ri");
350
+
351
+ rb_gc_register_address(&sBaud);
352
+ rb_gc_register_address(&sDataBits);
353
+ rb_gc_register_address(&sStopBits);
354
+ rb_gc_register_address(&sParity);
355
+ rb_gc_register_address(&sRts);
356
+ rb_gc_register_address(&sDtr);
357
+ rb_gc_register_address(&sCts);
358
+ rb_gc_register_address(&sDsr);
359
+ rb_gc_register_address(&sDcd);
360
+ rb_gc_register_address(&sRi);
361
+
362
+ cSerialPort = rb_define_class("SerialPort", rb_cIO);
363
+ rb_define_singleton_method(cSerialPort, "create", sp_create, 1);
364
+
365
+ rb_define_method(cSerialPort, "get_modem_params", sp_get_modem_params, 0);
366
+ rb_define_method(cSerialPort, "set_modem_params", sp_set_modem_params, -1);
367
+ rb_define_method(cSerialPort, "modem_params", sp_get_modem_params, 0);
368
+ rb_define_method(cSerialPort, "modem_params=", sp_set_modem_params, -1);
369
+ rb_define_method(cSerialPort, "baud", sp_get_data_rate, 0);
370
+ rb_define_method(cSerialPort, "baud=", sp_set_data_rate, 1);
371
+ rb_define_method(cSerialPort, "data_bits", sp_get_data_bits, 0);
372
+ rb_define_method(cSerialPort, "data_bits=", sp_set_data_bits, 1);
373
+ rb_define_method(cSerialPort, "stop_bits", sp_get_stop_bits, 0);
374
+ rb_define_method(cSerialPort, "stop_bits=", sp_set_stop_bits, 1);
375
+ rb_define_method(cSerialPort, "parity", sp_get_parity, 0);
376
+ rb_define_method(cSerialPort, "parity=", sp_set_parity, 1);
377
+
378
+ rb_define_method(cSerialPort, "flow_control=", sp_set_flow_control, 1);
379
+ rb_define_method(cSerialPort, "flow_control", sp_get_flow_control, 0);
380
+
381
+ rb_define_method(cSerialPort, "read_timeout", sp_get_read_timeout, 0);
382
+ rb_define_method(cSerialPort, "read_timeout=", sp_set_read_timeout, 1);
383
+ rb_define_method(cSerialPort, "write_timeout", sp_get_write_timeout, 0);
384
+ rb_define_method(cSerialPort, "write_timeout=", sp_set_write_timeout, 1);
385
+
386
+ rb_define_method(cSerialPort, "break", sp_break, 1);
387
+
388
+ rb_define_method(cSerialPort, "signals", sp_signals, 0);
389
+ rb_define_method(cSerialPort, "get_signals", sp_signals, 0);
390
+ rb_define_method(cSerialPort, "rts", sp_get_rts, 0);
391
+ rb_define_method(cSerialPort, "rts=", sp_set_rts, 1);
392
+ rb_define_method(cSerialPort, "dtr", sp_get_dtr, 0);
393
+ rb_define_method(cSerialPort, "dtr=", sp_set_dtr, 1);
394
+ rb_define_method(cSerialPort, "cts", sp_get_cts, 0);
395
+ rb_define_method(cSerialPort, "dsr", sp_get_dsr, 0);
396
+ rb_define_method(cSerialPort, "dcd", sp_get_dcd, 0);
397
+ rb_define_method(cSerialPort, "ri", sp_get_ri, 0);
398
+
399
+ rb_define_const(cSerialPort, "NONE", INT2FIX(NONE));
400
+ rb_define_const(cSerialPort, "HARD", INT2FIX(HARD));
401
+ rb_define_const(cSerialPort, "SOFT", INT2FIX(SOFT));
402
+
403
+ rb_define_const(cSerialPort, "SPACE", INT2FIX(SPACE));
404
+ rb_define_const(cSerialPort, "MARK", INT2FIX(MARK));
405
+ rb_define_const(cSerialPort, "EVEN", INT2FIX(EVEN));
406
+ rb_define_const(cSerialPort, "ODD", INT2FIX(ODD));
407
+
408
+ rb_define_const(cSerialPort, "VERSION", rb_str_new2(RUBY_SERIAL_PORT_VERSION));
409
+ }
@@ -0,0 +1,94 @@
1
+ /* Ruby/SerialPort $Id$
2
+ * Guillaume Pierronnet <moumar@netcourrier.com>
3
+ * Alan Stern <stern@rowland.harvard.edu>
4
+ * Daniel E. Shipton <dshipton@redshiptechnologies.com>
5
+ * Tobin Richard <tobin.richard@gmail.com>
6
+ *
7
+ * This code is hereby licensed for public consumption under either the
8
+ * GNU GPL v2 or greater.
9
+ *
10
+ * You should have received a copy of the GNU General Public License
11
+ * along with this program; if not, write to the Free Software
12
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
13
+ *
14
+ * For documentation on serial programming, see the excellent:
15
+ * "Serial Programming Guide for POSIX Operating Systems"
16
+ * written Michael R. Sweet.
17
+ * http://www.easysw.com/~mike/serial/
18
+ */
19
+
20
+ #ifndef _RUBY_SERIAL_PORT_H_
21
+ #define _RUBY_SERIAL_PORT_H_
22
+
23
+ #define RUBY_SERIAL_PORT_VERSION "0.7.3"
24
+
25
+ #include <ruby.h> /* ruby inclusion */
26
+
27
+ #if RUBY_VERSION >= '1.9.0'
28
+ #include <ruby/io.h>
29
+ #else
30
+ #include <rubyio.h>
31
+ #endif
32
+
33
+ struct modem_params
34
+ {
35
+ int data_rate;
36
+ int data_bits;
37
+ int stop_bits;
38
+ int parity;
39
+ };
40
+
41
+ struct line_signals
42
+ {
43
+ int rts;
44
+ int dtr;
45
+ int cts;
46
+ int dsr;
47
+ int dcd;
48
+ int ri;
49
+ };
50
+
51
+ #define NONE 0
52
+ #define HARD 1
53
+ #define SOFT 2
54
+
55
+ #if defined(OS_MSWIN) || defined(OS_BCCWIN) || defined(OS_MINGW)
56
+ #define SPACE SPACEPARITY
57
+ #define MARK MARKPARITY
58
+ #define EVEN EVENPARITY
59
+ #define ODD ODDPARITY
60
+
61
+ #ifndef RB_SERIAL_EXPORT
62
+ #define RB_SERIAL_EXPORT __declspec(dllexport)
63
+ #endif
64
+ #else
65
+ #define SPACE 0
66
+ #define MARK 0
67
+ #define EVEN 1
68
+ #define ODD 2
69
+
70
+ #define RB_SERIAL_EXPORT
71
+ #endif
72
+
73
+ extern VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
74
+ extern VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
75
+
76
+ /* Implementation specific functions. */
77
+ VALUE RB_SERIAL_EXPORT sp_create_impl(VALUE class, VALUE _port);
78
+ VALUE RB_SERIAL_EXPORT sp_set_modem_params_impl(int argc, VALUE *argv, VALUE self);
79
+ void RB_SERIAL_EXPORT get_modem_params_impl(VALUE self, struct modem_params *mp);
80
+ VALUE RB_SERIAL_EXPORT sp_set_flow_control_impl(VALUE self, VALUE val);
81
+ VALUE RB_SERIAL_EXPORT sp_get_flow_control_impl(VALUE self);
82
+ VALUE RB_SERIAL_EXPORT sp_set_read_timeout_impl(VALUE self, VALUE val);
83
+ VALUE RB_SERIAL_EXPORT sp_get_read_timeout_impl(VALUE self);
84
+ VALUE RB_SERIAL_EXPORT sp_set_write_timeout_impl(VALUE self, VALUE val);
85
+ VALUE RB_SERIAL_EXPORT sp_get_write_timeout_impl(VALUE self);
86
+ VALUE RB_SERIAL_EXPORT sp_break_impl(VALUE self, VALUE time);
87
+ void RB_SERIAL_EXPORT get_line_signals_helper_impl(VALUE obj, struct line_signals *ls);
88
+ VALUE RB_SERIAL_EXPORT set_signal_impl(VALUE obj, VALUE val, int sig);
89
+ VALUE RB_SERIAL_EXPORT sp_set_rts_impl(VALUE self, VALUE val);
90
+ VALUE RB_SERIAL_EXPORT sp_set_dtr_impl(VALUE self, VALUE val);
91
+ VALUE RB_SERIAL_EXPORT sp_get_rts_impl(VALUE self);
92
+ VALUE RB_SERIAL_EXPORT sp_get_dtr_impl(VALUE self);
93
+
94
+ #endif