monoxit-serialport 1.2.1.1

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.
@@ -0,0 +1,665 @@
1
+ /* Ruby/SerialPort
2
+ * Guillaume Pierronnet <moumar@netcourrier.com>
3
+ * Alan Stern <stern@rowland.harvard.edu>
4
+ * Daniel E. Shipton <dshipton@redshiptechnologies.com>
5
+ * Hector G. Parra <hector@hectorparra.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
+ #include "serialport.h"
21
+
22
+ #if defined(OS_MSWIN) || defined(OS_BCCWIN) || defined(OS_MINGW)
23
+
24
+ #include <stdio.h> /* Standard input/output definitions */
25
+ #include <io.h> /* Low-level I/O definitions */
26
+ #include <fcntl.h> /* File control definitions */
27
+ #include <windows.h> /* Windows standard function definitions */
28
+
29
+
30
+ static char sGetCommState[] = "GetCommState";
31
+ static char sSetCommState[] = "SetCommState";
32
+ static char sGetCommTimeouts[] = "GetCommTimeouts";
33
+ static char sSetCommTimeouts[] = "SetCommTimeouts";
34
+
35
+
36
+ static HANDLE get_handle_helper(obj)
37
+ VALUE obj;
38
+ {
39
+ HANDLE fh;
40
+ Data_Get_Struct(rb_iv_get(obj,"@fh"), HANDLE, fh);
41
+ return fh;
42
+ }
43
+
44
+ /* hack to work around the fact that Ruby doesn't use GetLastError? */
45
+ static void _rb_win32_fail(const char *function_call) {
46
+ rb_raise(
47
+ rb_eRuntimeError,
48
+ "%s failed: GetLastError returns %d",
49
+ function_call, GetLastError( )
50
+ );
51
+ }
52
+
53
+ VALUE RB_SERIAL_EXPORT sp_create_impl(class, _port)
54
+ VALUE class, _port;
55
+ {
56
+ int fd;
57
+ HANDLE fh;
58
+ VALUE vfh;
59
+ int num_port;
60
+ char *str_port;
61
+ char port[260]; /* Windows XP MAX_PATH. See http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx */
62
+
63
+ DCB dcb;
64
+
65
+ NEWOBJ(sp, struct RObject);
66
+ rb_secure(4);
67
+ OBJSETUP(sp, class, T_OBJECT);
68
+
69
+ switch(TYPE(_port))
70
+ {
71
+ case T_FIXNUM:
72
+ num_port = FIX2INT(_port);
73
+ if (num_port < 0)
74
+ {
75
+ rb_raise(rb_eArgError, "illegal port number");
76
+ }
77
+ snprintf(port, sizeof(port) - 1, "\\\\.\\COM%d", num_port + 1); /* '0' is actually COM1, etc. */
78
+ port[sizeof(port) - 1] = 0;
79
+ break;
80
+
81
+ case T_STRING:
82
+ SafeStringValue(_port);
83
+ str_port = RSTRING_PTR(_port);
84
+ if (str_port[0] != '\\') /* Check for Win32 Device Namespace prefix "\\.\" */
85
+ {
86
+ snprintf(port, sizeof(port) - 1, "\\\\.\\%s", str_port);
87
+ port[sizeof(port) - 1] = 0;
88
+ }
89
+ else
90
+ {
91
+ snprintf(port, sizeof(port) - 1, "%s", str_port);
92
+ port[sizeof(port) - 1] = 0;
93
+ }
94
+ break;
95
+
96
+ default:
97
+ rb_raise(rb_eTypeError, "wrong argument type");
98
+ break;
99
+ }
100
+
101
+ fh = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
102
+ vfh = Data_Wrap_Struct(class, 0, CloseHandle, fh);
103
+ rb_iv_set((VALUE) sp,"@fh",vfh);
104
+
105
+ if (fh == INVALID_HANDLE_VALUE){
106
+ CloseHandle(fh);
107
+ rb_sys_fail(port);
108
+ }
109
+
110
+ if (SetupComm(fh, 1024, 1024) == 0)
111
+ {
112
+ CloseHandle(fh);
113
+ rb_raise(rb_eArgError, "not a serial port");
114
+ }
115
+
116
+ dcb.DCBlength = sizeof(dcb);
117
+ if (GetCommState(fh, &dcb) == 0)
118
+ {
119
+ CloseHandle(fh);
120
+ _rb_win32_fail(sGetCommState);
121
+ }
122
+ dcb.fBinary = TRUE;
123
+ dcb.fParity = FALSE;
124
+ dcb.fOutxDsrFlow = FALSE;
125
+ dcb.fDtrControl = DTR_CONTROL_ENABLE;
126
+ dcb.fDsrSensitivity = FALSE;
127
+ dcb.fTXContinueOnXoff = FALSE;
128
+ dcb.fErrorChar = FALSE;
129
+ dcb.fNull = FALSE;
130
+ dcb.fAbortOnError = FALSE;
131
+ dcb.XonChar = 17;
132
+ dcb.XoffChar = 19;
133
+ if (SetCommState(fh, &dcb) == 0)
134
+ {
135
+ CloseHandle(fh);
136
+ _rb_win32_fail(sSetCommState);
137
+ }
138
+
139
+ errno = 0;
140
+ return (VALUE) sp;
141
+ }
142
+
143
+ VALUE RB_SERIAL_EXPORT sp_set_modem_params_impl(argc, argv, self)
144
+ int argc;
145
+ VALUE *argv, self;
146
+ {
147
+ HANDLE fh;
148
+ DCB dcb;
149
+ VALUE _data_rate, _data_bits, _parity, _stop_bits;
150
+ int use_hash = 0;
151
+ int data_rate, data_bits, parity;
152
+
153
+ if (argc == 0)
154
+ {
155
+ return self;
156
+ }
157
+ if (argc == 1 && T_HASH == TYPE(argv[0]))
158
+ {
159
+ use_hash = 1;
160
+ _data_rate = rb_hash_aref(argv[0], sBaud);
161
+ _data_bits = rb_hash_aref(argv[0], sDataBits);
162
+ _stop_bits = rb_hash_aref(argv[0], sStopBits);
163
+ _parity = rb_hash_aref(argv[0], sParity);
164
+ }
165
+
166
+ fh = get_handle_helper(self);
167
+ dcb.DCBlength = sizeof(dcb);
168
+ if (GetCommState(fh, &dcb) == 0)
169
+ {
170
+ _rb_win32_fail(sGetCommState);
171
+ }
172
+
173
+ if (!use_hash)
174
+ {
175
+ _data_rate = argv[0];
176
+ }
177
+
178
+ if (NIL_P(_data_rate))
179
+ {
180
+ goto SkipDataRate;
181
+ }
182
+
183
+ Check_Type(_data_rate, T_FIXNUM);
184
+
185
+ data_rate = FIX2INT(_data_rate);
186
+ dcb.BaudRate = data_rate;
187
+
188
+ SkipDataRate:
189
+
190
+ if (!use_hash)
191
+ {
192
+ _data_bits = (argc >= 2 ? argv[1] : INT2FIX(8));
193
+ }
194
+
195
+ if (NIL_P(_data_bits))
196
+ {
197
+ goto SkipDataBits;
198
+ }
199
+
200
+ Check_Type(_data_bits, T_FIXNUM);
201
+
202
+ data_bits = FIX2INT(_data_bits);
203
+ if (4 <= data_bits && data_bits <= 8)
204
+ {
205
+ dcb.ByteSize = data_bits;
206
+ }
207
+ else
208
+ {
209
+ rb_raise(rb_eArgError, "unknown character size");
210
+ }
211
+
212
+ SkipDataBits:
213
+
214
+ if (!use_hash)
215
+ {
216
+ _stop_bits = (argc >= 3 ? argv[2] : INT2FIX(1));
217
+ }
218
+
219
+ if (NIL_P(_stop_bits))
220
+ {
221
+ goto SkipStopBits;
222
+ }
223
+
224
+ Check_Type(_stop_bits, T_FIXNUM);
225
+
226
+ switch (FIX2INT(_stop_bits))
227
+ {
228
+ case 1:
229
+ dcb.StopBits = ONESTOPBIT;
230
+ break;
231
+ case 2:
232
+ dcb.StopBits = TWOSTOPBITS;
233
+ break;
234
+ default:
235
+ rb_raise(rb_eArgError, "unknown number of stop bits");
236
+ break;
237
+ }
238
+
239
+ SkipStopBits:
240
+
241
+ if (!use_hash)
242
+ {
243
+ _parity = (argc >= 4 ? argv[3] : (dcb.ByteSize == 8 ?
244
+ INT2FIX(NOPARITY) : INT2FIX(EVENPARITY)));
245
+ }
246
+
247
+ if (NIL_P(_parity))
248
+ {
249
+ goto SkipParity;
250
+ }
251
+
252
+ Check_Type(_parity, T_FIXNUM);
253
+
254
+ parity = FIX2INT(_parity);
255
+ switch (parity)
256
+ {
257
+ case EVENPARITY:
258
+ case ODDPARITY:
259
+ case MARKPARITY:
260
+ case SPACEPARITY:
261
+ case NOPARITY:
262
+ dcb.Parity = parity;
263
+ break;
264
+
265
+ default:
266
+ rb_raise(rb_eArgError, "unknown parity");
267
+ break;
268
+ }
269
+
270
+ SkipParity:
271
+
272
+ if (SetCommState(fh, &dcb) == 0)
273
+ {
274
+ _rb_win32_fail(sSetCommState);
275
+ }
276
+
277
+ return argv[0];
278
+ }
279
+
280
+ void RB_SERIAL_EXPORT get_modem_params_impl(self, mp)
281
+ VALUE self;
282
+ struct modem_params *mp;
283
+ {
284
+ HANDLE fh;
285
+ DCB dcb;
286
+
287
+ fh = get_handle_helper(self);
288
+ dcb.DCBlength = sizeof(dcb);
289
+ if (GetCommState(fh, &dcb) == 0)
290
+ {
291
+ _rb_win32_fail(sGetCommState);
292
+ }
293
+
294
+ mp->data_rate = dcb.BaudRate;
295
+ mp->data_bits = dcb.ByteSize;
296
+ mp->stop_bits = (dcb.StopBits == ONESTOPBIT ? 1 : 2);
297
+ mp->parity = dcb.Parity;
298
+ }
299
+
300
+ VALUE RB_SERIAL_EXPORT sp_set_flow_control_impl(self, val)
301
+ VALUE self, val;
302
+ {
303
+ HANDLE fh;
304
+ int flowc;
305
+ DCB dcb;
306
+
307
+ Check_Type(val, T_FIXNUM);
308
+
309
+ fh = get_handle_helper(self);
310
+ dcb.DCBlength = sizeof(dcb);
311
+ if (GetCommState(fh, &dcb) == 0)
312
+ {
313
+ _rb_win32_fail(sGetCommState);
314
+ }
315
+
316
+ flowc = FIX2INT(val);
317
+ if (flowc & HARD)
318
+ {
319
+ dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
320
+ dcb.fOutxCtsFlow = TRUE;
321
+ }
322
+ else
323
+ {
324
+ dcb.fRtsControl = RTS_CONTROL_ENABLE;
325
+ dcb.fOutxCtsFlow = FALSE;
326
+ }
327
+
328
+ if (flowc & SOFT)
329
+ {
330
+ dcb.fOutX = dcb.fInX = TRUE;
331
+ }
332
+ else
333
+ {
334
+ dcb.fOutX = dcb.fInX = FALSE;
335
+ }
336
+
337
+ if (SetCommState(fh, &dcb) == 0)
338
+ {
339
+ _rb_win32_fail(sSetCommState);
340
+ }
341
+
342
+ return val;
343
+ }
344
+
345
+ VALUE RB_SERIAL_EXPORT sp_get_flow_control_impl(self)
346
+ VALUE self;
347
+ {
348
+ HANDLE fh;
349
+ int ret;
350
+ DCB dcb;
351
+
352
+ fh = get_handle_helper(self);
353
+ dcb.DCBlength = sizeof(dcb);
354
+ if (GetCommState(fh, &dcb) == 0)
355
+ {
356
+ _rb_win32_fail(sGetCommState);
357
+ }
358
+
359
+ ret = 0;
360
+ if (dcb.fOutxCtsFlow)
361
+ {
362
+ ret += HARD;
363
+ }
364
+
365
+ if (dcb.fOutX)
366
+ {
367
+ ret += SOFT;
368
+ }
369
+
370
+ return INT2FIX(ret);
371
+ }
372
+
373
+ VALUE RB_SERIAL_EXPORT sp_set_read_timeout_impl(self, val)
374
+ VALUE self, val;
375
+ {
376
+ int timeout;
377
+ HANDLE fh;
378
+ COMMTIMEOUTS ctout;
379
+
380
+ Check_Type(val, T_FIXNUM);
381
+ timeout = FIX2INT(val);
382
+
383
+ fh = get_handle_helper(self);
384
+ if (GetCommTimeouts(fh, &ctout) == 0)
385
+ {
386
+ _rb_win32_fail(sGetCommTimeouts);
387
+ }
388
+
389
+ if (timeout < 0)
390
+ {
391
+ ctout.ReadIntervalTimeout = MAXDWORD;
392
+ ctout.ReadTotalTimeoutMultiplier = 0;
393
+ ctout.ReadTotalTimeoutConstant = 0;
394
+ }
395
+ else if (timeout == 0)
396
+ {
397
+ ctout.ReadIntervalTimeout = MAXDWORD;
398
+ ctout.ReadTotalTimeoutMultiplier = MAXDWORD;
399
+ ctout.ReadTotalTimeoutConstant = MAXDWORD - 1;
400
+ }
401
+ else
402
+ {
403
+ ctout.ReadIntervalTimeout = timeout;
404
+ ctout.ReadTotalTimeoutMultiplier = 0;
405
+ ctout.ReadTotalTimeoutConstant = timeout;
406
+ }
407
+
408
+ if (SetCommTimeouts(fh, &ctout) == 0)
409
+ {
410
+ _rb_win32_fail(sSetCommTimeouts);
411
+ }
412
+
413
+ return val;
414
+ }
415
+
416
+ VALUE RB_SERIAL_EXPORT sp_get_read_timeout_impl(self)
417
+ VALUE self;
418
+ {
419
+ HANDLE fh;
420
+ COMMTIMEOUTS ctout;
421
+
422
+ fh = get_handle_helper(self);
423
+ if (GetCommTimeouts(fh, &ctout) == 0)
424
+ {
425
+ _rb_win32_fail(sGetCommTimeouts);
426
+ }
427
+
428
+ switch (ctout.ReadTotalTimeoutConstant)
429
+ {
430
+ case 0:
431
+ return INT2FIX(-1);
432
+ case MAXDWORD:
433
+ return INT2FIX(0);
434
+ }
435
+
436
+ return INT2FIX(ctout.ReadTotalTimeoutConstant);
437
+ }
438
+
439
+ VALUE RB_SERIAL_EXPORT sp_set_write_timeout_impl(self, val)
440
+ VALUE self, val;
441
+ {
442
+ int timeout;
443
+ HANDLE fh;
444
+ COMMTIMEOUTS ctout;
445
+
446
+ Check_Type(val, T_FIXNUM);
447
+ timeout = FIX2INT(val);
448
+
449
+ fh = get_handle_helper(self);
450
+ if (GetCommTimeouts(fh, &ctout) == 0)
451
+ {
452
+ _rb_win32_fail(sGetCommTimeouts);
453
+ }
454
+
455
+ if (timeout <= 0)
456
+ {
457
+ ctout.WriteTotalTimeoutMultiplier = 0;
458
+ ctout.WriteTotalTimeoutConstant = 0;
459
+ }
460
+ else
461
+ {
462
+ ctout.WriteTotalTimeoutMultiplier = timeout;
463
+ ctout.WriteTotalTimeoutConstant = 0;
464
+ }
465
+
466
+ if (SetCommTimeouts(fh, &ctout) == 0)
467
+ {
468
+ _rb_win32_fail(sSetCommTimeouts);
469
+ }
470
+
471
+ return val;
472
+ }
473
+
474
+ VALUE RB_SERIAL_EXPORT sp_get_write_timeout_impl(self)
475
+ VALUE self;
476
+ {
477
+ HANDLE fh;
478
+ COMMTIMEOUTS ctout;
479
+
480
+ fh = get_handle_helper(self);
481
+ if (GetCommTimeouts(fh, &ctout) == 0)
482
+ {
483
+ _rb_win32_fail(sGetCommTimeouts);
484
+ }
485
+
486
+ return INT2FIX(ctout.WriteTotalTimeoutMultiplier);
487
+ }
488
+
489
+ static void delay_ms(time)
490
+ int time;
491
+ {
492
+ HANDLE ev;
493
+
494
+ ev = CreateEvent(NULL, FALSE, FALSE, NULL);
495
+ if (!ev)
496
+ {
497
+ _rb_win32_fail("CreateEvent");
498
+ }
499
+
500
+ if (WaitForSingleObject(ev, time) == WAIT_FAILED)
501
+ {
502
+ _rb_win32_fail("WaitForSingleObject");
503
+ }
504
+
505
+ CloseHandle(ev);
506
+ }
507
+
508
+ VALUE RB_SERIAL_EXPORT sp_break_impl(self, time)
509
+ VALUE self, time;
510
+ {
511
+ HANDLE fh;
512
+
513
+ Check_Type(time, T_FIXNUM);
514
+
515
+ fh = get_handle_helper(self);
516
+ if (SetCommBreak(fh) == 0)
517
+ {
518
+ _rb_win32_fail("SetCommBreak");
519
+ }
520
+
521
+ delay_ms(FIX2INT(time) * 100);
522
+ ClearCommBreak(fh);
523
+
524
+ return Qnil;
525
+ }
526
+
527
+ void RB_SERIAL_EXPORT get_line_signals_helper_impl(obj, ls)
528
+ VALUE obj;
529
+ struct line_signals *ls;
530
+ {
531
+ HANDLE fh;
532
+ unsigned long status; /* DWORD */
533
+
534
+ fh = get_handle_helper(obj);
535
+ if (GetCommModemStatus(fh, &status) == 0)
536
+ {
537
+ _rb_win32_fail("GetCommModemStatus");
538
+ }
539
+
540
+ ls->cts = (status & MS_CTS_ON ? 1 : 0);
541
+ ls->dsr = (status & MS_DSR_ON ? 1 : 0);
542
+ ls->dcd = (status & MS_RLSD_ON ? 1 : 0);
543
+ ls->ri = (status & MS_RING_ON ? 1 : 0);
544
+ }
545
+
546
+ static VALUE set_signal(obj, val, sigoff, sigon)
547
+ VALUE obj,val;
548
+ int sigoff, sigon;
549
+ {
550
+ HANDLE fh;
551
+ int set, sig;
552
+
553
+ Check_Type(val, T_FIXNUM);
554
+ fh = get_handle_helper(obj);
555
+
556
+ set = FIX2INT(val);
557
+ if (set == 0)
558
+ {
559
+ sig = sigoff;
560
+ }
561
+ else if (set == 1)
562
+ {
563
+ sig = sigon;
564
+ }
565
+ else
566
+ {
567
+ rb_raise(rb_eArgError, "invalid value");
568
+ }
569
+
570
+ if (EscapeCommFunction(fh, sig) == 0)
571
+ {
572
+ _rb_win32_fail("EscapeCommFunction");
573
+ }
574
+
575
+ return val;
576
+ }
577
+
578
+ VALUE RB_SERIAL_EXPORT sp_set_rts_impl(self, val)
579
+ VALUE self, val;
580
+ {
581
+ return set_signal(self, val, CLRRTS, SETRTS);
582
+ }
583
+
584
+ VALUE RB_SERIAL_EXPORT sp_set_dtr_impl(self, val)
585
+ VALUE self, val;
586
+ {
587
+ return set_signal(self, val, CLRDTR, SETDTR);
588
+ }
589
+
590
+ VALUE RB_SERIAL_EXPORT sp_get_rts_impl(self)
591
+ VALUE self;
592
+ {
593
+ rb_notimplement();
594
+ return self;
595
+ }
596
+
597
+ VALUE RB_SERIAL_EXPORT sp_get_dtr_impl(self)
598
+ VALUE self;
599
+ {
600
+ rb_notimplement();
601
+ return self;
602
+ }
603
+
604
+ VALUE RB_SERIAL_EXPORT sp_write_impl(self, str)
605
+ VALUE self, str;
606
+ {
607
+ char *c_str = RSTRING_PTR(str);
608
+ int len = RSTRING_LEN(str);
609
+
610
+ HANDLE fh;
611
+ DWORD n = 0;
612
+ Data_Get_Struct(rb_iv_get(self,"@fh"), HANDLE, fh);
613
+ if(FALSE == WriteFile(fh, c_str, len, &n, NULL)){
614
+ _rb_win32_fail("WriteFile");
615
+ }
616
+
617
+ //rb_iv_set(self,"@byte_offset", rb_iv_get(self,"@initial_byte_offset"));
618
+ return INT2FIX(n);
619
+ }
620
+
621
+ VALUE RB_SERIAL_EXPORT sp_read_impl(argc, argv, self)
622
+ int argc;
623
+ VALUE *argv, self;
624
+ {
625
+ long bytes = 1024;
626
+
627
+ if (argc > 1){
628
+ rb_raise(rb_eArgError, "Wrong number of arguments in read()");
629
+ }
630
+ else if (argc == 1){
631
+ bytes = FIX2LONG(argv[0]);
632
+ }
633
+
634
+ char ReadBuffer[bytes];
635
+ DWORD n = 0;
636
+ DWORD w;
637
+ HANDLE fh;
638
+ Data_Get_Struct(rb_iv_get(self,"@fh"), HANDLE, fh);
639
+ /*
640
+ w = SetFilePointer(fh, FIX2LONG(rb_iv_get(self,"@byte_offset")), NULL, FILE_BEGIN);
641
+ if (w == INVALID_SET_FILE_POINTER){
642
+ _rb_win32_fail("SetFilePointer");
643
+ }*/
644
+ if (FALSE == ReadFile(fh, ReadBuffer, bytes, &n, NULL)){
645
+ _rb_win32_fail("ReadFile");
646
+ }
647
+ //rb_iv_set(self,"@byte_offset", FIX2LONG(rb_iv_get(self, "@byte_offset")) + bytes);
648
+ return rb_str_new(ReadBuffer, n);
649
+ }
650
+
651
+ void RB_SERIAL_EXPORT sp_close_impl(self)
652
+ VALUE self;
653
+ {
654
+ HANDLE fh;
655
+ Data_Get_Struct(rb_iv_get(self,"@fh"), HANDLE, fh);
656
+ CloseHandle(fh);
657
+ }
658
+
659
+ void RB_SERIAL_EXPORT sp_set_initial_offset_impl(self, offset)
660
+ VALUE self, offset;
661
+ {
662
+ //rb_iv_set(self,"@initial_byte_offset", offset);
663
+ }
664
+
665
+ #endif /* defined(OS_MSWIN) || defined(OS_BCCWIN) || defined(OS_MINGW) */
data/lib/oscheck.rb ADDED
@@ -0,0 +1,5 @@
1
+ class OsCheck
2
+ def self.is_osx?
3
+ return (RUBY_PLATFORM.split("-")[1] == ('darwin')) ? true : false
4
+ end
5
+ end
data/lib/serialport.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'oscheck'
2
+
3
+ if OsCheck.is_osx?
4
+ require 'serialport.bundle'
5
+ else
6
+ require 'serialport.so'
7
+ end
8
+
9
+ class SerialPort
10
+ private_class_method(:create)
11
+
12
+ # Creates a serial port object.
13
+ #
14
+ # <tt>port</tt> may be a port number
15
+ # or the file name of a defice.
16
+ # The number is portable; so 0 is mapped to "COM1" on Windows,
17
+ # "/dev/ttyS0" on Linux, "/dev/cuaa0" on Mac OS X, etc.
18
+ #
19
+ # <tt>params</tt> can be used to configure the serial port.
20
+ # See SerialPort#set_modem_params for details
21
+ def SerialPort::new(port, *params)
22
+ sp = create(port)
23
+ begin
24
+ sp.set_modem_params(*params)
25
+ rescue
26
+ sp.close
27
+ raise
28
+ end
29
+ return sp
30
+ end
31
+
32
+ # This behaves like SerialPort#new, except that you can pass a block
33
+ # to which the new serial port object will be passed. In this case
34
+ # the connection is automaticaly closed when the block has finished.
35
+ def SerialPort::open(port, *params)
36
+ sp = create(port)
37
+ begin
38
+ sp.set_modem_params(*params)
39
+ rescue
40
+ sp.close
41
+ raise
42
+ end
43
+ if (block_given?)
44
+ begin
45
+ yield sp
46
+ ensure
47
+ sp.close
48
+ end
49
+ return nil
50
+ end
51
+ return sp
52
+ end
53
+ end