cehoffman-ruby-serialport 0.7.0
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.
- data/CHANGELOG +26 -0
- data/LICENSE +280 -0
- data/README.rdoc +145 -0
- data/ext/extconf.rb +13 -0
- data/ext/posix_serialport_impl.c +655 -0
- data/ext/serialport.c +263 -0
- data/ext/serialport.h +83 -0
- data/ext/win_serialport_impl.c +616 -0
- data/lib/serialport.rb +60 -0
- data/test/miniterm.rb +25 -0
- metadata +68 -0
data/ext/extconf.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
printf("checking for OS... ")
|
4
|
+
STDOUT.flush
|
5
|
+
os = /-([a-z]+)/.match(RUBY_PLATFORM)[1]
|
6
|
+
puts(os)
|
7
|
+
$CFLAGS += " -DOS_#{os.upcase}"
|
8
|
+
|
9
|
+
if !(os == 'mswin' or os == 'bccwin')
|
10
|
+
exit(1) if not have_header("termios.h") or not have_header("unistd.h")
|
11
|
+
end
|
12
|
+
|
13
|
+
create_makefile('serialport')
|
@@ -0,0 +1,655 @@
|
|
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
|
+
* Chris Hoffman <cehoffman@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
|
+
#include "serialport.h"
|
21
|
+
|
22
|
+
/* Check if we are on a posix compliant system. */
|
23
|
+
#if !defined(OS_MSWIN) && !defined(OS_BCCWIN)
|
24
|
+
|
25
|
+
#include <stdio.h> /* Standard input/output definitions */
|
26
|
+
#include <unistd.h> /* UNIX standard function definitions */
|
27
|
+
#include <fcntl.h> /* File control definitions */
|
28
|
+
#include <errno.h> /* Error number definitions */
|
29
|
+
#include <termios.h> /* POSIX terminal control definitions */
|
30
|
+
#include <sys/ioctl.h>
|
31
|
+
|
32
|
+
#ifdef CRTSCTS
|
33
|
+
#define HAVE_FLOWCONTROL_HARD 1
|
34
|
+
#else
|
35
|
+
#undef HAVE_FLOWCONTROL_HARD
|
36
|
+
#endif
|
37
|
+
|
38
|
+
|
39
|
+
static char sTcgetattr[] = "tcgetattr";
|
40
|
+
static char sTcsetattr[] = "tcsetattr";
|
41
|
+
static char sIoctl[] = "ioctl";
|
42
|
+
|
43
|
+
|
44
|
+
static int get_fd_helper(obj)
|
45
|
+
VALUE obj;
|
46
|
+
{
|
47
|
+
OpenFile *fptr;
|
48
|
+
|
49
|
+
GetOpenFile(obj, fptr);
|
50
|
+
return (fileno(fptr->f));
|
51
|
+
}
|
52
|
+
|
53
|
+
VALUE sp_create(VALUE class, VALUE _port)
|
54
|
+
{
|
55
|
+
OpenFile *fp;
|
56
|
+
int fd;
|
57
|
+
int num_port;
|
58
|
+
char *port;
|
59
|
+
char *ports[] = {
|
60
|
+
#if defined(OS_LINUX) || defined(OS_CYGWIN)
|
61
|
+
"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
|
62
|
+
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6", "/dev/ttyS7"
|
63
|
+
#elif defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD)
|
64
|
+
"/dev/cuaa0", "/dev/cuaa1", "/dev/cuaa2", "/dev/cuaa3",
|
65
|
+
"/dev/cuaa4", "/dev/cuaa5", "/dev/cuaa6", "/dev/cuaa7"
|
66
|
+
#elif defined(OS_SOLARIS)
|
67
|
+
"/dev/ttya", "/dev/ttyb", "/dev/ttyc", "/dev/ttyd",
|
68
|
+
"/dev/ttye", "/dev/ttyf", "/dev/ttyg", "/dev/ttyh"
|
69
|
+
#elif defined(OS_AIX)
|
70
|
+
"/dev/tty0", "/dev/tty1", "/dev/tty2", "/dev/tty3",
|
71
|
+
"/dev/tty4", "/dev/tty5", "/dev/tty6", "/dev/tty7"
|
72
|
+
#elif defined(OS_IRIX)
|
73
|
+
"/dev/ttyf1", "/dev/ttyf2", "/dev/ttyf3", "/dev/ttyf4",
|
74
|
+
"/dev/ttyf5", "/dev/ttyf6", "/dev/ttyf7", "/dev/ttyf8"
|
75
|
+
#endif
|
76
|
+
};
|
77
|
+
struct termios params;
|
78
|
+
|
79
|
+
NEWOBJ(sp, struct RFile);
|
80
|
+
rb_secure(4);
|
81
|
+
OBJSETUP(sp, class, T_FILE);
|
82
|
+
MakeOpenFile((VALUE) sp, fp);
|
83
|
+
|
84
|
+
switch(TYPE(_port))
|
85
|
+
{
|
86
|
+
case T_FIXNUM:
|
87
|
+
num_port = FIX2INT(_port);
|
88
|
+
if (num_port < 0 || num_port > sizeof(ports) / sizeof(ports[0]))
|
89
|
+
{
|
90
|
+
rb_raise(rb_eArgError, "illegal port number");
|
91
|
+
}
|
92
|
+
port = ports[num_port];
|
93
|
+
break;
|
94
|
+
|
95
|
+
case T_STRING:
|
96
|
+
Check_SafeStr(_port);
|
97
|
+
port = RSTRING(_port)->ptr;
|
98
|
+
break;
|
99
|
+
|
100
|
+
default:
|
101
|
+
rb_raise(rb_eTypeError, "wrong argument type");
|
102
|
+
break;
|
103
|
+
}
|
104
|
+
|
105
|
+
fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
|
106
|
+
if (fd == -1)
|
107
|
+
{
|
108
|
+
rb_sys_fail(port);
|
109
|
+
}
|
110
|
+
|
111
|
+
if (!isatty(fd))
|
112
|
+
{
|
113
|
+
close(fd);
|
114
|
+
rb_raise(rb_eArgError, "not a serial port");
|
115
|
+
}
|
116
|
+
|
117
|
+
/* enable blocking read */
|
118
|
+
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
|
119
|
+
|
120
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
121
|
+
{
|
122
|
+
close(fd);
|
123
|
+
rb_sys_fail(sTcgetattr);
|
124
|
+
}
|
125
|
+
|
126
|
+
params.c_oflag = 0;
|
127
|
+
params.c_lflag = 0;
|
128
|
+
params.c_iflag &= (IXON | IXOFF | IXANY);
|
129
|
+
params.c_cflag |= CLOCAL | CREAD;
|
130
|
+
params.c_cflag &= ~HUPCL;
|
131
|
+
|
132
|
+
/* Cause a read of the serial port wait for either the number
|
133
|
+
* of requested bytes to be read, or for .1*c_cc[TIME] sec to
|
134
|
+
* elpase since last received byte. */
|
135
|
+
// TODO: Look to see if this makes sense for intended use of library
|
136
|
+
//params.c_cc[MIN] = 0;
|
137
|
+
//params.c_cc[TIME] = 1;
|
138
|
+
|
139
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
140
|
+
{
|
141
|
+
close(fd);
|
142
|
+
rb_sys_fail(sTcsetattr);
|
143
|
+
}
|
144
|
+
|
145
|
+
fp->f = rb_fdopen(fd, "r+");
|
146
|
+
fp->mode = FMODE_READWRITE | FMODE_SYNC;
|
147
|
+
|
148
|
+
return (VALUE) sp;
|
149
|
+
}
|
150
|
+
|
151
|
+
VALUE sp_set_modem_params(int argc, VALUE *argv, VALUE self)
|
152
|
+
{
|
153
|
+
int fd;
|
154
|
+
struct termios params;
|
155
|
+
VALUE _data_rate, _data_bits, _parity, _stop_bits;
|
156
|
+
int use_hash = 0;
|
157
|
+
int data_rate, data_bits;
|
158
|
+
_data_rate = _data_bits = _parity = _stop_bits = Qnil;
|
159
|
+
|
160
|
+
if (argc == 0)
|
161
|
+
{
|
162
|
+
return self;
|
163
|
+
}
|
164
|
+
|
165
|
+
if (argc == 1 && T_HASH == TYPE(argv[0]))
|
166
|
+
{
|
167
|
+
use_hash = 1;
|
168
|
+
_data_rate = rb_hash_aref(argv[0], sBaud);
|
169
|
+
_data_bits = rb_hash_aref(argv[0], sDataBits);
|
170
|
+
_stop_bits = rb_hash_aref(argv[0], sStopBits);
|
171
|
+
_parity = rb_hash_aref(argv[0], sParity);
|
172
|
+
}
|
173
|
+
|
174
|
+
fd = get_fd_helper(self);
|
175
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
176
|
+
{
|
177
|
+
rb_sys_fail(sTcgetattr);
|
178
|
+
}
|
179
|
+
|
180
|
+
if (!use_hash)
|
181
|
+
{
|
182
|
+
_data_rate = argv[0];
|
183
|
+
}
|
184
|
+
|
185
|
+
if (NIL_P(_data_rate))
|
186
|
+
{
|
187
|
+
goto SkipDataRate;
|
188
|
+
}
|
189
|
+
Check_Type(_data_rate, T_FIXNUM);
|
190
|
+
|
191
|
+
switch(FIX2INT(_data_rate))
|
192
|
+
{
|
193
|
+
case 50: data_rate = B50; break;
|
194
|
+
case 75: data_rate = B75; break;
|
195
|
+
case 110: data_rate = B110; break;
|
196
|
+
case 134: data_rate = B134; break;
|
197
|
+
case 150: data_rate = B150; break;
|
198
|
+
case 200: data_rate = B200; break;
|
199
|
+
case 300: data_rate = B300; break;
|
200
|
+
case 600: data_rate = B600; break;
|
201
|
+
case 1200: data_rate = B1200; break;
|
202
|
+
case 1800: data_rate = B1800; break;
|
203
|
+
case 2400: data_rate = B2400; break;
|
204
|
+
case 4800: data_rate = B4800; break;
|
205
|
+
case 9600: data_rate = B9600; break;
|
206
|
+
case 19200: data_rate = B19200; break;
|
207
|
+
case 38400: data_rate = B38400; break;
|
208
|
+
#ifdef B57600
|
209
|
+
case 57600: data_rate = B57600; break;
|
210
|
+
#endif
|
211
|
+
#ifdef B76800
|
212
|
+
case 76800: data_rate = B76800; break;
|
213
|
+
#endif
|
214
|
+
#ifdef B115200
|
215
|
+
case 115200: data_rate = B115200; break;
|
216
|
+
#endif
|
217
|
+
#ifdef B230400
|
218
|
+
case 230400: data_rate = B230400; break;
|
219
|
+
#endif
|
220
|
+
|
221
|
+
default:
|
222
|
+
rb_raise(rb_eArgError, "unknown baud rate");
|
223
|
+
break;
|
224
|
+
}
|
225
|
+
cfsetispeed(¶ms, data_rate);
|
226
|
+
cfsetospeed(¶ms, data_rate);
|
227
|
+
|
228
|
+
SkipDataRate:
|
229
|
+
|
230
|
+
if (!use_hash)
|
231
|
+
{
|
232
|
+
_data_bits = (argc >= 2 ? argv[1] : INT2FIX(8));
|
233
|
+
}
|
234
|
+
|
235
|
+
if (NIL_P(_data_bits))
|
236
|
+
{
|
237
|
+
goto SkipDataBits;
|
238
|
+
}
|
239
|
+
Check_Type(_data_bits, T_FIXNUM);
|
240
|
+
|
241
|
+
switch(FIX2INT(_data_bits))
|
242
|
+
{
|
243
|
+
case 5:
|
244
|
+
data_bits = CS5;
|
245
|
+
break;
|
246
|
+
case 6:
|
247
|
+
data_bits = CS6;
|
248
|
+
break;
|
249
|
+
case 7:
|
250
|
+
data_bits = CS7;
|
251
|
+
break;
|
252
|
+
case 8:
|
253
|
+
data_bits = CS8;
|
254
|
+
break;
|
255
|
+
default:
|
256
|
+
rb_raise(rb_eArgError, "unknown character size");
|
257
|
+
break;
|
258
|
+
}
|
259
|
+
params.c_cflag &= ~CSIZE;
|
260
|
+
params.c_cflag |= data_bits;
|
261
|
+
|
262
|
+
SkipDataBits:
|
263
|
+
|
264
|
+
if (!use_hash)
|
265
|
+
{
|
266
|
+
_stop_bits = (argc >= 3 ? argv[2] : INT2FIX(1));
|
267
|
+
}
|
268
|
+
|
269
|
+
if (NIL_P(_stop_bits))
|
270
|
+
{
|
271
|
+
goto SkipStopBits;
|
272
|
+
}
|
273
|
+
|
274
|
+
Check_Type(_stop_bits, T_FIXNUM);
|
275
|
+
|
276
|
+
switch(FIX2INT(_stop_bits))
|
277
|
+
{
|
278
|
+
case 1:
|
279
|
+
params.c_cflag &= ~CSTOPB;
|
280
|
+
break;
|
281
|
+
case 2:
|
282
|
+
params.c_cflag |= CSTOPB;
|
283
|
+
break;
|
284
|
+
default:
|
285
|
+
rb_raise(rb_eArgError, "unknown number of stop bits");
|
286
|
+
break;
|
287
|
+
}
|
288
|
+
|
289
|
+
SkipStopBits:
|
290
|
+
|
291
|
+
if (!use_hash)
|
292
|
+
{
|
293
|
+
_parity = (argc >= 4 ? argv[3] : ((params.c_cflag & CSIZE) == CS8 ?
|
294
|
+
INT2FIX(NONE) : INT2FIX(EVEN)));
|
295
|
+
}
|
296
|
+
|
297
|
+
if (NIL_P(_parity))
|
298
|
+
{
|
299
|
+
goto SkipParity;
|
300
|
+
}
|
301
|
+
|
302
|
+
Check_Type(_parity, T_FIXNUM);
|
303
|
+
|
304
|
+
switch(FIX2INT(_parity))
|
305
|
+
{
|
306
|
+
case EVEN:
|
307
|
+
params.c_cflag |= PARENB;
|
308
|
+
params.c_cflag &= ~PARODD;
|
309
|
+
break;
|
310
|
+
|
311
|
+
case ODD:
|
312
|
+
params.c_cflag |= PARENB;
|
313
|
+
params.c_cflag |= PARODD;
|
314
|
+
break;
|
315
|
+
|
316
|
+
case NONE:
|
317
|
+
params.c_cflag &= ~PARENB;
|
318
|
+
break;
|
319
|
+
|
320
|
+
default:
|
321
|
+
rb_raise(rb_eArgError, "unknown parity");
|
322
|
+
break;
|
323
|
+
}
|
324
|
+
|
325
|
+
SkipParity:
|
326
|
+
|
327
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
328
|
+
{
|
329
|
+
rb_sys_fail(sTcsetattr);
|
330
|
+
}
|
331
|
+
return argv[0];
|
332
|
+
}
|
333
|
+
|
334
|
+
void get_modem_params(VALUE self, struct modem_params *mp)
|
335
|
+
{
|
336
|
+
int fd;
|
337
|
+
struct termios params;
|
338
|
+
|
339
|
+
fd = get_fd_helper(self);
|
340
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
341
|
+
{
|
342
|
+
rb_sys_fail(sTcgetattr);
|
343
|
+
}
|
344
|
+
|
345
|
+
switch (cfgetospeed(¶ms))
|
346
|
+
{
|
347
|
+
case B50: mp->data_rate = 50; break;
|
348
|
+
case B75: mp->data_rate = 75; break;
|
349
|
+
case B110: mp->data_rate = 110; break;
|
350
|
+
case B134: mp->data_rate = 134; break;
|
351
|
+
case B150: mp->data_rate = 150; break;
|
352
|
+
case B200: mp->data_rate = 200; break;
|
353
|
+
case B300: mp->data_rate = 300; break;
|
354
|
+
case B600: mp->data_rate = 600; break;
|
355
|
+
case B1200: mp->data_rate = 1200; break;
|
356
|
+
case B1800: mp->data_rate = 1800; break;
|
357
|
+
case B2400: mp->data_rate = 2400; break;
|
358
|
+
case B4800: mp->data_rate = 4800; break;
|
359
|
+
case B9600: mp->data_rate = 9600; break;
|
360
|
+
case B19200: mp->data_rate = 19200; break;
|
361
|
+
case B38400: mp->data_rate = 38400; break;
|
362
|
+
#ifdef B57600
|
363
|
+
case B57600: mp->data_rate = 57600; break;
|
364
|
+
#endif
|
365
|
+
#ifdef B76800
|
366
|
+
case B76800: mp->data_rate = 76800; break;
|
367
|
+
#endif
|
368
|
+
#ifdef B115200
|
369
|
+
case B115200: mp->data_rate = 115200; break;
|
370
|
+
#endif
|
371
|
+
#ifdef B230400
|
372
|
+
case B230400: mp->data_rate = 230400; break;
|
373
|
+
#endif
|
374
|
+
}
|
375
|
+
|
376
|
+
switch(params.c_cflag & CSIZE)
|
377
|
+
{
|
378
|
+
case CS5:
|
379
|
+
mp->data_bits = 5;
|
380
|
+
break;
|
381
|
+
case CS6:
|
382
|
+
mp->data_bits = 6;
|
383
|
+
break;
|
384
|
+
case CS7:
|
385
|
+
mp->data_bits = 7;
|
386
|
+
break;
|
387
|
+
case CS8:
|
388
|
+
mp->data_bits = 8;
|
389
|
+
break;
|
390
|
+
default:
|
391
|
+
mp->data_bits = 0;
|
392
|
+
break;
|
393
|
+
}
|
394
|
+
|
395
|
+
mp->stop_bits = (params.c_cflag & CSTOPB ? 2 : 1);
|
396
|
+
|
397
|
+
if (!(params.c_cflag & PARENB))
|
398
|
+
{
|
399
|
+
mp->parity = NONE;
|
400
|
+
}
|
401
|
+
else if (params.c_cflag & PARODD)
|
402
|
+
{
|
403
|
+
mp->parity = ODD;
|
404
|
+
}
|
405
|
+
else
|
406
|
+
{
|
407
|
+
mp->parity = EVEN;
|
408
|
+
}
|
409
|
+
}
|
410
|
+
|
411
|
+
VALUE sp_set_flow_control(VALUE self, VALUE val)
|
412
|
+
{
|
413
|
+
int fd;
|
414
|
+
int flowc;
|
415
|
+
struct termios params;
|
416
|
+
|
417
|
+
Check_Type(val, T_FIXNUM);
|
418
|
+
|
419
|
+
fd = get_fd_helper(self);
|
420
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
421
|
+
{
|
422
|
+
rb_sys_fail(sTcgetattr);
|
423
|
+
}
|
424
|
+
|
425
|
+
flowc = FIX2INT(val);
|
426
|
+
if (flowc & HARD)
|
427
|
+
{
|
428
|
+
#ifdef HAVE_FLOWCONTROL_HARD
|
429
|
+
params.c_cflag |= CRTSCTS;
|
430
|
+
}
|
431
|
+
else
|
432
|
+
{
|
433
|
+
params.c_cflag &= ~CRTSCTS;
|
434
|
+
}
|
435
|
+
#else
|
436
|
+
rb_raise(rb_eIOError, "Hardware flow control not supported");
|
437
|
+
}
|
438
|
+
#endif
|
439
|
+
|
440
|
+
if (flowc & SOFT)
|
441
|
+
{
|
442
|
+
params.c_iflag |= (IXON | IXOFF | IXANY);
|
443
|
+
}
|
444
|
+
else
|
445
|
+
{
|
446
|
+
params.c_iflag &= ~(IXON | IXOFF | IXANY);
|
447
|
+
}
|
448
|
+
|
449
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
450
|
+
{
|
451
|
+
rb_sys_fail(sTcsetattr);
|
452
|
+
}
|
453
|
+
|
454
|
+
return val;
|
455
|
+
}
|
456
|
+
|
457
|
+
VALUE sp_get_flow_control(VALUE self)
|
458
|
+
{
|
459
|
+
int ret;
|
460
|
+
int fd;
|
461
|
+
struct termios params;
|
462
|
+
|
463
|
+
fd = get_fd_helper(self);
|
464
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
465
|
+
{
|
466
|
+
rb_sys_fail(sTcgetattr);
|
467
|
+
}
|
468
|
+
|
469
|
+
ret = 0;
|
470
|
+
|
471
|
+
#ifdef HAVE_FLOWCONTROL_HARD
|
472
|
+
if (params.c_cflag & CRTSCTS)
|
473
|
+
{
|
474
|
+
ret += HARD;
|
475
|
+
}
|
476
|
+
#endif
|
477
|
+
|
478
|
+
if (params.c_iflag & (IXON | IXOFF | IXANY))
|
479
|
+
{
|
480
|
+
ret += SOFT;
|
481
|
+
}
|
482
|
+
|
483
|
+
return INT2FIX(ret);
|
484
|
+
}
|
485
|
+
|
486
|
+
VALUE sp_set_read_timeout(VALUE self, VALUE val)
|
487
|
+
{
|
488
|
+
int timeout;
|
489
|
+
int fd;
|
490
|
+
struct termios params;
|
491
|
+
|
492
|
+
Check_Type(val, T_FIXNUM);
|
493
|
+
timeout = FIX2INT(val);
|
494
|
+
|
495
|
+
fd = get_fd_helper(self);
|
496
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
497
|
+
{
|
498
|
+
rb_sys_fail(sTcgetattr);
|
499
|
+
}
|
500
|
+
|
501
|
+
if (timeout < 0)
|
502
|
+
{
|
503
|
+
params.c_cc[VTIME] = 0;
|
504
|
+
params.c_cc[VMIN] = 0;
|
505
|
+
}
|
506
|
+
else if (timeout == 0)
|
507
|
+
{
|
508
|
+
params.c_cc[VTIME] = 0;
|
509
|
+
params.c_cc[VMIN] = 1;
|
510
|
+
}
|
511
|
+
else
|
512
|
+
{
|
513
|
+
params.c_cc[VTIME] = (timeout + 50) / 100;
|
514
|
+
params.c_cc[VMIN] = 0;
|
515
|
+
}
|
516
|
+
|
517
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
518
|
+
{
|
519
|
+
rb_sys_fail(sTcsetattr);
|
520
|
+
}
|
521
|
+
|
522
|
+
return val;
|
523
|
+
}
|
524
|
+
|
525
|
+
VALUE sp_get_read_timeout(VALUE self)
|
526
|
+
{
|
527
|
+
int fd;
|
528
|
+
struct termios params;
|
529
|
+
|
530
|
+
fd = get_fd_helper(self);
|
531
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
532
|
+
{
|
533
|
+
rb_sys_fail(sTcgetattr);
|
534
|
+
}
|
535
|
+
|
536
|
+
if (params.c_cc[VTIME] == 0 && params.c_cc[VMIN] == 0)
|
537
|
+
{
|
538
|
+
return INT2FIX(-1);
|
539
|
+
}
|
540
|
+
|
541
|
+
return INT2FIX(params.c_cc[VTIME] * 100);
|
542
|
+
}
|
543
|
+
|
544
|
+
VALUE sp_set_write_timeout(VALUE self, VALUE val)
|
545
|
+
{
|
546
|
+
rb_notimplement();
|
547
|
+
return self;
|
548
|
+
}
|
549
|
+
|
550
|
+
VALUE sp_get_write_timeout(VALUE self)
|
551
|
+
{
|
552
|
+
rb_notimplement();
|
553
|
+
return self;
|
554
|
+
}
|
555
|
+
|
556
|
+
VALUE sp_break(VALUE self, VALUE time)
|
557
|
+
{
|
558
|
+
int fd;
|
559
|
+
|
560
|
+
Check_Type(time, T_FIXNUM);
|
561
|
+
|
562
|
+
fd = get_fd_helper(self);
|
563
|
+
|
564
|
+
if (tcsendbreak(fd, FIX2INT(time) / 3) == -1)
|
565
|
+
{
|
566
|
+
rb_sys_fail("tcsendbreak");
|
567
|
+
}
|
568
|
+
|
569
|
+
return Qnil;
|
570
|
+
}
|
571
|
+
|
572
|
+
void get_line_signals_helper(VALUE obj, struct line_signals *ls)
|
573
|
+
{
|
574
|
+
int fd, status;
|
575
|
+
|
576
|
+
fd = get_fd_helper(obj);
|
577
|
+
|
578
|
+
if (ioctl(fd, TIOCMGET, &status) == -1)
|
579
|
+
{
|
580
|
+
rb_sys_fail(sIoctl);
|
581
|
+
}
|
582
|
+
|
583
|
+
ls->rts = (status & TIOCM_RTS ? 1 : 0);
|
584
|
+
ls->dtr = (status & TIOCM_DTR ? 1 : 0);
|
585
|
+
ls->cts = (status & TIOCM_CTS ? 1 : 0);
|
586
|
+
ls->dsr = (status & TIOCM_DSR ? 1 : 0);
|
587
|
+
ls->dcd = (status & TIOCM_CD ? 1 : 0);
|
588
|
+
ls->ri = (status & TIOCM_RI ? 1 : 0);
|
589
|
+
}
|
590
|
+
|
591
|
+
VALUE set_signal(VALUE obj, VALUE val, int sig)
|
592
|
+
{
|
593
|
+
int status;
|
594
|
+
int fd;
|
595
|
+
int set;
|
596
|
+
|
597
|
+
Check_Type(val, T_FIXNUM);
|
598
|
+
fd = get_fd_helper(obj);
|
599
|
+
|
600
|
+
if (ioctl(fd, TIOCMGET, &status) == -1)
|
601
|
+
{
|
602
|
+
rb_sys_fail(sIoctl);
|
603
|
+
}
|
604
|
+
|
605
|
+
set = FIX2INT(val);
|
606
|
+
|
607
|
+
if (set == 0)
|
608
|
+
{
|
609
|
+
status &= ~sig;
|
610
|
+
}
|
611
|
+
else if (set == 1)
|
612
|
+
{
|
613
|
+
status |= sig;
|
614
|
+
}
|
615
|
+
else
|
616
|
+
{
|
617
|
+
rb_raise(rb_eArgError, "invalid value");
|
618
|
+
}
|
619
|
+
|
620
|
+
if (ioctl(fd, TIOCMSET, &status) == -1)
|
621
|
+
{
|
622
|
+
rb_sys_fail(sIoctl);
|
623
|
+
}
|
624
|
+
|
625
|
+
return val;
|
626
|
+
}
|
627
|
+
|
628
|
+
VALUE sp_set_rts(VALUE self, VALUE val)
|
629
|
+
{
|
630
|
+
return set_signal(self, val, TIOCM_RTS);
|
631
|
+
}
|
632
|
+
|
633
|
+
VALUE sp_set_dtr(VALUE self, VALUE val)
|
634
|
+
{
|
635
|
+
return set_signal(self, val, TIOCM_DTR);
|
636
|
+
}
|
637
|
+
|
638
|
+
VALUE sp_get_rts(VALUE self)
|
639
|
+
{
|
640
|
+
struct line_signals ls;
|
641
|
+
|
642
|
+
get_line_signals_helper(self, &ls);
|
643
|
+
return INT2FIX(ls.rts);
|
644
|
+
}
|
645
|
+
|
646
|
+
VALUE sp_get_dtr(VALUE self)
|
647
|
+
{
|
648
|
+
struct line_signals ls;
|
649
|
+
|
650
|
+
get_line_signals_helper(self, &ls);
|
651
|
+
|
652
|
+
return INT2FIX(ls.dtr);
|
653
|
+
}
|
654
|
+
|
655
|
+
#endif /* !defined(OS_MSWIN) || !defined(OS_BCCWIN) */
|