shellixyz-serialport 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.travis.yml +12 -0
- data/CHANGELOG +59 -0
- data/CHECKLIST +12 -0
- data/Gemfile +3 -0
- data/LICENSE +339 -0
- data/MANIFEST +12 -0
- data/README.md +43 -0
- data/Rakefile +27 -0
- data/ext/native/extconf.rb +13 -0
- data/ext/native/posix_serialport_impl.c +789 -0
- data/ext/native/serialport.c +613 -0
- data/ext/native/serialport.h +100 -0
- data/ext/native/win_serialport_impl.c +657 -0
- data/lib/serialport/version.rb +3 -0
- data/lib/serialport.rb +60 -0
- data/serialport.gemspec +23 -0
- data/test/miniterm.rb +25 -0
- data/test/set_readtimeout.rb +7 -0
- metadata +113 -0
data/MANIFEST
ADDED
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Ruby/SerialPort
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/hparra/ruby-serialport.png?branch=v1.2.2)](https://travis-ci.org/hparra/ruby-serialport)
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Ruby/SerialPort is a Ruby library that provides a class for using RS-232 serial ports. This class also contains low-level functions to check and set the current state of the signals on the line.
|
8
|
+
|
9
|
+
The native Windows version of this library supports the Microsoft Visual C++, Borland C++, and MinGW compilers.
|
10
|
+
|
11
|
+
There is an alternative gem with MRI, JRuby, and Rubinius support. See below.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install the gem as normal: `sudo gem install serialport`
|
16
|
+
|
17
|
+
## Testing
|
18
|
+
|
19
|
+
Use Ruby's version of miniterm: `ruby test/miniterm.rb`
|
20
|
+
|
21
|
+
## API
|
22
|
+
|
23
|
+
See <http://rubydoc.info/gems/serialport/SerialPort> or run `yard`
|
24
|
+
|
25
|
+
## Issues
|
26
|
+
|
27
|
+
See <https://github.com/hparra/ruby-serialport/issues>
|
28
|
+
|
29
|
+
## Alternatives
|
30
|
+
|
31
|
+
The [Hybrid Group](http://hybridgroup.com/)'s MRI, JRuby, & Rubinius compatible [rubyserial](https://github.com/hybridgroup/rubyserial) gem may provide improved compatibilty in modern environments.
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
GPL
|
36
|
+
|
37
|
+
## Authors
|
38
|
+
|
39
|
+
* Guillaume Pierronnet <moumar@netcourrier.com>
|
40
|
+
* Alan Stern <stern@rowland.harvard.edu>
|
41
|
+
* Tobin Richard <tobin.richard@gmail.com>
|
42
|
+
* Hector Parra <hector@hectorparra.com>
|
43
|
+
* Ryan C. Payne <rpayne-oss@bullittsystems.com>
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
# :build
|
3
|
+
# :install
|
4
|
+
# :release
|
5
|
+
require "bundler/gem_tasks"
|
6
|
+
|
7
|
+
# :clean
|
8
|
+
# :clobber
|
9
|
+
# :compile
|
10
|
+
# :compile:serialport
|
11
|
+
require "rake/extensiontask"
|
12
|
+
|
13
|
+
CLOBBER << FileList["doc"]
|
14
|
+
CLOBBER << FileList["pkg"]
|
15
|
+
|
16
|
+
GEMSPEC = eval File.read File.expand_path("../serialport.gemspec", __FILE__)
|
17
|
+
|
18
|
+
Rake::ExtensionTask.new "serialport", GEMSPEC do |ext|
|
19
|
+
ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
|
20
|
+
ext.ext_dir = "ext/native"
|
21
|
+
end
|
22
|
+
|
23
|
+
# add your default gem packing task
|
24
|
+
Gem::PackageTask.new(GEMSPEC) do |pkg|
|
25
|
+
end
|
26
|
+
|
27
|
+
task :default => [:clean, :clobber, :compile, :test]
|
@@ -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' or os == 'mingw')
|
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,789 @@
|
|
1
|
+
/* Ruby/SerialPort
|
2
|
+
* Guillaume Pierronnet <moumar@netcourrier.com>
|
3
|
+
* Alan Stern <stern@rowland.harvard.edu>
|
4
|
+
* Daniel E. Shipton <dshipton@redshiptechnologies.com>
|
5
|
+
* Ryan C. Payne <rpayne-oss@bullittsystems.com>
|
6
|
+
* Manuel "MaG" A. Güílamo <maguilamo.c@gmail.com>
|
7
|
+
*
|
8
|
+
* This code is hereby licensed for public consumption under either the
|
9
|
+
* GNU GPL v2 or greater.
|
10
|
+
*
|
11
|
+
* You should have received a copy of the GNU General Public License
|
12
|
+
* along with this program; if not, write to the Free Software
|
13
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
14
|
+
*
|
15
|
+
* For documentation on serial programming, see the excellent:
|
16
|
+
* "Serial Programming Guide for POSIX Operating Systems"
|
17
|
+
* written Michael R. Sweet.
|
18
|
+
* http://www.easysw.com/~mike/serial/
|
19
|
+
*/
|
20
|
+
|
21
|
+
#include "serialport.h"
|
22
|
+
|
23
|
+
/* Check if we are on a posix compliant system. */
|
24
|
+
#if !defined(OS_MSWIN) && !defined(OS_BCCWIN) && !defined(OS_MINGW)
|
25
|
+
|
26
|
+
#include <stdio.h> /* Standard input/output definitions */
|
27
|
+
#include <unistd.h> /* UNIX standard function definitions */
|
28
|
+
#include <fcntl.h> /* File control definitions */
|
29
|
+
#include <errno.h> /* Error number definitions */
|
30
|
+
#include <termios.h> /* POSIX terminal control definitions */
|
31
|
+
#include <sys/ioctl.h>
|
32
|
+
|
33
|
+
#ifdef CRTSCTS
|
34
|
+
#define HAVE_FLOWCONTROL_HARD 1
|
35
|
+
#else
|
36
|
+
#undef HAVE_FLOWCONTROL_HARD
|
37
|
+
#endif
|
38
|
+
|
39
|
+
/* on mac os x, not all baud rates are defined in termios.h but
|
40
|
+
they are mapped to the numeric value anyway, so we define them here */
|
41
|
+
#ifdef __APPLE__
|
42
|
+
#ifndef B460800
|
43
|
+
#define B460800 460800
|
44
|
+
#endif
|
45
|
+
#ifndef B500000
|
46
|
+
#define B500000 500000
|
47
|
+
#endif
|
48
|
+
#ifndef B576000
|
49
|
+
#define B576000 576000
|
50
|
+
#endif
|
51
|
+
#ifndef B921600
|
52
|
+
#define B921600 921600
|
53
|
+
#endif
|
54
|
+
#ifndef B1000000
|
55
|
+
#define B1000000 1000000
|
56
|
+
#endif
|
57
|
+
#endif
|
58
|
+
|
59
|
+
static char sTcgetattr[] = "tcgetattr";
|
60
|
+
static char sTcsetattr[] = "tcsetattr";
|
61
|
+
static char sIoctl[] = "ioctl";
|
62
|
+
|
63
|
+
|
64
|
+
int get_fd_helper(obj)
|
65
|
+
VALUE obj;
|
66
|
+
{
|
67
|
+
#ifdef HAVE_RUBY_IO_H
|
68
|
+
rb_io_t *fptr;
|
69
|
+
#else
|
70
|
+
OpenFile *fptr;
|
71
|
+
#endif
|
72
|
+
GetOpenFile(obj, fptr);
|
73
|
+
#ifdef HAVE_RUBY_IO_H
|
74
|
+
return (fptr->fd);
|
75
|
+
#else
|
76
|
+
return (fileno(fptr->f));
|
77
|
+
#endif
|
78
|
+
}
|
79
|
+
|
80
|
+
VALUE sp_create_impl(class, _port)
|
81
|
+
VALUE class, _port;
|
82
|
+
{
|
83
|
+
#ifdef HAVE_RUBY_IO_H
|
84
|
+
rb_io_t *fp;
|
85
|
+
#else
|
86
|
+
OpenFile *fp;
|
87
|
+
#endif
|
88
|
+
|
89
|
+
int fd;
|
90
|
+
int num_port;
|
91
|
+
char *port;
|
92
|
+
char *ports[] = {
|
93
|
+
#if defined(OS_LINUX) || defined(OS_CYGWIN)
|
94
|
+
"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
|
95
|
+
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6", "/dev/ttyS7"
|
96
|
+
#elif defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DARWIN)
|
97
|
+
"/dev/cuaa0", "/dev/cuaa1", "/dev/cuaa2", "/dev/cuaa3",
|
98
|
+
"/dev/cuaa4", "/dev/cuaa5", "/dev/cuaa6", "/dev/cuaa7"
|
99
|
+
#elif defined(OS_SOLARIS)
|
100
|
+
"/dev/ttya", "/dev/ttyb", "/dev/ttyc", "/dev/ttyd",
|
101
|
+
"/dev/ttye", "/dev/ttyf", "/dev/ttyg", "/dev/ttyh"
|
102
|
+
#elif defined(OS_AIX)
|
103
|
+
"/dev/tty0", "/dev/tty1", "/dev/tty2", "/dev/tty3",
|
104
|
+
"/dev/tty4", "/dev/tty5", "/dev/tty6", "/dev/tty7"
|
105
|
+
#elif defined(OS_IRIX)
|
106
|
+
"/dev/ttyf1", "/dev/ttyf2", "/dev/ttyf3", "/dev/ttyf4",
|
107
|
+
"/dev/ttyf5", "/dev/ttyf6", "/dev/ttyf7", "/dev/ttyf8"
|
108
|
+
#endif
|
109
|
+
};
|
110
|
+
struct termios params;
|
111
|
+
|
112
|
+
NEWOBJ(sp, struct RFile);
|
113
|
+
OBJSETUP(sp, class, T_FILE);
|
114
|
+
MakeOpenFile((VALUE) sp, fp);
|
115
|
+
|
116
|
+
switch(TYPE(_port))
|
117
|
+
{
|
118
|
+
case T_FIXNUM:
|
119
|
+
num_port = FIX2INT(_port);
|
120
|
+
if (num_port < 0 || num_port > sizeof(ports) / sizeof(ports[0]))
|
121
|
+
{
|
122
|
+
rb_raise(rb_eArgError, "illegal port number");
|
123
|
+
}
|
124
|
+
port = ports[num_port];
|
125
|
+
break;
|
126
|
+
|
127
|
+
case T_STRING:
|
128
|
+
port = StringValueCStr(_port);
|
129
|
+
break;
|
130
|
+
|
131
|
+
default:
|
132
|
+
rb_raise(rb_eTypeError, "wrong argument type");
|
133
|
+
break;
|
134
|
+
}
|
135
|
+
|
136
|
+
fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
|
137
|
+
if (fd == -1)
|
138
|
+
{
|
139
|
+
rb_sys_fail(port);
|
140
|
+
}
|
141
|
+
|
142
|
+
if (!isatty(fd))
|
143
|
+
{
|
144
|
+
close(fd);
|
145
|
+
rb_raise(rb_eArgError, "not a serial port");
|
146
|
+
}
|
147
|
+
|
148
|
+
/* enable blocking read */
|
149
|
+
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
|
150
|
+
|
151
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
152
|
+
{
|
153
|
+
close(fd);
|
154
|
+
rb_sys_fail(sTcgetattr);
|
155
|
+
}
|
156
|
+
|
157
|
+
params.c_oflag = 0;
|
158
|
+
params.c_lflag = 0;
|
159
|
+
params.c_iflag &= (IXON | IXOFF | IXANY);
|
160
|
+
params.c_cflag |= CLOCAL | CREAD;
|
161
|
+
params.c_cflag &= ~HUPCL;
|
162
|
+
|
163
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
164
|
+
{
|
165
|
+
close(fd);
|
166
|
+
rb_sys_fail(sTcsetattr);
|
167
|
+
}
|
168
|
+
|
169
|
+
#ifdef HAVE_RUBY_IO_H
|
170
|
+
fp->fd = fd;
|
171
|
+
#else
|
172
|
+
fp->f = rb_fdopen(fd, "r+");
|
173
|
+
#endif
|
174
|
+
fp->mode = FMODE_READWRITE | FMODE_SYNC;
|
175
|
+
|
176
|
+
return (VALUE) sp;
|
177
|
+
}
|
178
|
+
|
179
|
+
VALUE sp_set_modem_params_impl(argc, argv, self)
|
180
|
+
int argc;
|
181
|
+
VALUE *argv, self;
|
182
|
+
{
|
183
|
+
int fd;
|
184
|
+
struct termios params;
|
185
|
+
VALUE _data_rate, _data_bits, _parity, _stop_bits;
|
186
|
+
int use_hash = 0;
|
187
|
+
int data_rate, data_bits;
|
188
|
+
_data_rate = _data_bits = _parity = _stop_bits = Qnil;
|
189
|
+
|
190
|
+
if (argc == 0)
|
191
|
+
{
|
192
|
+
return self;
|
193
|
+
}
|
194
|
+
|
195
|
+
if (argc == 1 && T_HASH == TYPE(argv[0]))
|
196
|
+
{
|
197
|
+
use_hash = 1;
|
198
|
+
_data_rate = rb_hash_aref(argv[0], sBaud);
|
199
|
+
_data_bits = rb_hash_aref(argv[0], sDataBits);
|
200
|
+
_stop_bits = rb_hash_aref(argv[0], sStopBits);
|
201
|
+
_parity = rb_hash_aref(argv[0], sParity);
|
202
|
+
}
|
203
|
+
|
204
|
+
fd = get_fd_helper(self);
|
205
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
206
|
+
{
|
207
|
+
rb_sys_fail(sTcgetattr);
|
208
|
+
}
|
209
|
+
|
210
|
+
if (!use_hash)
|
211
|
+
{
|
212
|
+
_data_rate = argv[0];
|
213
|
+
}
|
214
|
+
|
215
|
+
if (NIL_P(_data_rate))
|
216
|
+
{
|
217
|
+
goto SkipDataRate;
|
218
|
+
}
|
219
|
+
Check_Type(_data_rate, T_FIXNUM);
|
220
|
+
|
221
|
+
switch(FIX2INT(_data_rate))
|
222
|
+
{
|
223
|
+
case 50: data_rate = B50; break;
|
224
|
+
case 75: data_rate = B75; break;
|
225
|
+
case 110: data_rate = B110; break;
|
226
|
+
case 134: data_rate = B134; break;
|
227
|
+
case 150: data_rate = B150; break;
|
228
|
+
case 200: data_rate = B200; break;
|
229
|
+
case 300: data_rate = B300; break;
|
230
|
+
case 600: data_rate = B600; break;
|
231
|
+
case 1200: data_rate = B1200; break;
|
232
|
+
case 1800: data_rate = B1800; break;
|
233
|
+
case 2400: data_rate = B2400; break;
|
234
|
+
case 4800: data_rate = B4800; break;
|
235
|
+
case 9600: data_rate = B9600; break;
|
236
|
+
case 19200: data_rate = B19200; break;
|
237
|
+
case 38400: data_rate = B38400; break;
|
238
|
+
#ifdef B57600
|
239
|
+
case 57600: data_rate = B57600; break;
|
240
|
+
#endif
|
241
|
+
#ifdef B76800
|
242
|
+
case 76800: data_rate = B76800; break;
|
243
|
+
#endif
|
244
|
+
#ifdef B115200
|
245
|
+
case 115200: data_rate = B115200; break;
|
246
|
+
#endif
|
247
|
+
#ifdef B230400
|
248
|
+
case 230400: data_rate = B230400; break;
|
249
|
+
#endif
|
250
|
+
#ifdef B460800
|
251
|
+
case 460800: data_rate = B460800; break;
|
252
|
+
#endif
|
253
|
+
#ifdef B500000
|
254
|
+
case 500000: data_rate = B500000; break;
|
255
|
+
#endif
|
256
|
+
#ifdef B576000
|
257
|
+
case 576000: data_rate = B576000; break;
|
258
|
+
#endif
|
259
|
+
#ifdef B921600
|
260
|
+
case 921600: data_rate = B921600; break;
|
261
|
+
#endif
|
262
|
+
#ifdef B1000000
|
263
|
+
case 1000000: data_rate = B1000000; break;
|
264
|
+
#endif
|
265
|
+
|
266
|
+
default:
|
267
|
+
rb_raise(rb_eArgError, "unknown baud rate");
|
268
|
+
break;
|
269
|
+
}
|
270
|
+
cfsetispeed(¶ms, data_rate);
|
271
|
+
cfsetospeed(¶ms, data_rate);
|
272
|
+
|
273
|
+
SkipDataRate:
|
274
|
+
|
275
|
+
if (!use_hash)
|
276
|
+
{
|
277
|
+
_data_bits = (argc >= 2 ? argv[1] : INT2FIX(8));
|
278
|
+
}
|
279
|
+
|
280
|
+
if (NIL_P(_data_bits))
|
281
|
+
{
|
282
|
+
goto SkipDataBits;
|
283
|
+
}
|
284
|
+
Check_Type(_data_bits, T_FIXNUM);
|
285
|
+
|
286
|
+
switch(FIX2INT(_data_bits))
|
287
|
+
{
|
288
|
+
case 5:
|
289
|
+
data_bits = CS5;
|
290
|
+
break;
|
291
|
+
case 6:
|
292
|
+
data_bits = CS6;
|
293
|
+
break;
|
294
|
+
case 7:
|
295
|
+
data_bits = CS7;
|
296
|
+
break;
|
297
|
+
case 8:
|
298
|
+
data_bits = CS8;
|
299
|
+
break;
|
300
|
+
default:
|
301
|
+
rb_raise(rb_eArgError, "unknown character size");
|
302
|
+
break;
|
303
|
+
}
|
304
|
+
params.c_cflag &= ~CSIZE;
|
305
|
+
params.c_cflag |= data_bits;
|
306
|
+
|
307
|
+
SkipDataBits:
|
308
|
+
|
309
|
+
if (!use_hash)
|
310
|
+
{
|
311
|
+
_stop_bits = (argc >= 3 ? argv[2] : INT2FIX(1));
|
312
|
+
}
|
313
|
+
|
314
|
+
if (NIL_P(_stop_bits))
|
315
|
+
{
|
316
|
+
goto SkipStopBits;
|
317
|
+
}
|
318
|
+
|
319
|
+
Check_Type(_stop_bits, T_FIXNUM);
|
320
|
+
|
321
|
+
switch(FIX2INT(_stop_bits))
|
322
|
+
{
|
323
|
+
case 1:
|
324
|
+
params.c_cflag &= ~CSTOPB;
|
325
|
+
break;
|
326
|
+
case 2:
|
327
|
+
params.c_cflag |= CSTOPB;
|
328
|
+
break;
|
329
|
+
default:
|
330
|
+
rb_raise(rb_eArgError, "unknown number of stop bits");
|
331
|
+
break;
|
332
|
+
}
|
333
|
+
|
334
|
+
SkipStopBits:
|
335
|
+
|
336
|
+
if (!use_hash)
|
337
|
+
{
|
338
|
+
_parity = (argc >= 4 ? argv[3] : ((params.c_cflag & CSIZE) == CS8 ?
|
339
|
+
INT2FIX(NONE) : INT2FIX(EVEN)));
|
340
|
+
}
|
341
|
+
|
342
|
+
if (NIL_P(_parity))
|
343
|
+
{
|
344
|
+
goto SkipParity;
|
345
|
+
}
|
346
|
+
|
347
|
+
Check_Type(_parity, T_FIXNUM);
|
348
|
+
|
349
|
+
switch(FIX2INT(_parity))
|
350
|
+
{
|
351
|
+
case EVEN:
|
352
|
+
params.c_cflag |= PARENB;
|
353
|
+
params.c_cflag &= ~PARODD;
|
354
|
+
#ifdef CMSPAR
|
355
|
+
params.c_cflag &= ~CMSPAR;
|
356
|
+
#endif
|
357
|
+
break;
|
358
|
+
|
359
|
+
case ODD:
|
360
|
+
params.c_cflag |= PARENB;
|
361
|
+
params.c_cflag |= PARODD;
|
362
|
+
#ifdef CMSPAR
|
363
|
+
params.c_cflag &= ~CMSPAR;
|
364
|
+
#endif
|
365
|
+
break;
|
366
|
+
|
367
|
+
#ifdef CMSPAR
|
368
|
+
case SPACE:
|
369
|
+
params.c_cflag |= PARENB;
|
370
|
+
params.c_cflag &= ~PARODD;
|
371
|
+
params.c_cflag |= CMSPAR;
|
372
|
+
break;
|
373
|
+
|
374
|
+
case MARK:
|
375
|
+
params.c_cflag |= PARENB;
|
376
|
+
params.c_cflag |= PARODD;
|
377
|
+
params.c_cflag |= CMSPAR;
|
378
|
+
break;
|
379
|
+
#endif
|
380
|
+
|
381
|
+
case NONE:
|
382
|
+
params.c_cflag &= ~PARENB;
|
383
|
+
#ifdef CMSPAR
|
384
|
+
params.c_cflag &= ~CMSPAR;
|
385
|
+
#endif
|
386
|
+
break;
|
387
|
+
|
388
|
+
default:
|
389
|
+
rb_raise(rb_eArgError, "unknown parity");
|
390
|
+
break;
|
391
|
+
}
|
392
|
+
|
393
|
+
SkipParity:
|
394
|
+
|
395
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
396
|
+
{
|
397
|
+
rb_sys_fail(sTcsetattr);
|
398
|
+
}
|
399
|
+
return argv[0];
|
400
|
+
}
|
401
|
+
|
402
|
+
void get_modem_params_impl(self, mp)
|
403
|
+
VALUE self;
|
404
|
+
struct modem_params *mp;
|
405
|
+
{
|
406
|
+
int fd;
|
407
|
+
struct termios params;
|
408
|
+
|
409
|
+
fd = get_fd_helper(self);
|
410
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
411
|
+
{
|
412
|
+
rb_sys_fail(sTcgetattr);
|
413
|
+
}
|
414
|
+
|
415
|
+
switch (cfgetospeed(¶ms))
|
416
|
+
{
|
417
|
+
case B50: mp->data_rate = 50; break;
|
418
|
+
case B75: mp->data_rate = 75; break;
|
419
|
+
case B110: mp->data_rate = 110; break;
|
420
|
+
case B134: mp->data_rate = 134; break;
|
421
|
+
case B150: mp->data_rate = 150; break;
|
422
|
+
case B200: mp->data_rate = 200; break;
|
423
|
+
case B300: mp->data_rate = 300; break;
|
424
|
+
case B600: mp->data_rate = 600; break;
|
425
|
+
case B1200: mp->data_rate = 1200; break;
|
426
|
+
case B1800: mp->data_rate = 1800; break;
|
427
|
+
case B2400: mp->data_rate = 2400; break;
|
428
|
+
case B4800: mp->data_rate = 4800; break;
|
429
|
+
case B9600: mp->data_rate = 9600; break;
|
430
|
+
case B19200: mp->data_rate = 19200; break;
|
431
|
+
case B38400: mp->data_rate = 38400; break;
|
432
|
+
#ifdef B57600
|
433
|
+
case B57600: mp->data_rate = 57600; break;
|
434
|
+
#endif
|
435
|
+
#ifdef B76800
|
436
|
+
case B76800: mp->data_rate = 76800; break;
|
437
|
+
#endif
|
438
|
+
#ifdef B115200
|
439
|
+
case B115200: mp->data_rate = 115200; break;
|
440
|
+
#endif
|
441
|
+
#ifdef B230400
|
442
|
+
case B230400: mp->data_rate = 230400; break;
|
443
|
+
#endif
|
444
|
+
#ifdef B460800
|
445
|
+
case B460800: mp->data_rate = 460800; break;
|
446
|
+
#endif
|
447
|
+
#ifdef B500000
|
448
|
+
case B500000: mp->data_rate = 500000; break;
|
449
|
+
#endif
|
450
|
+
#ifdef B576000
|
451
|
+
case B576000: mp->data_rate = 576000; break;
|
452
|
+
#endif
|
453
|
+
#ifdef B921600
|
454
|
+
case B921600: mp->data_rate = 921600; break;
|
455
|
+
#endif
|
456
|
+
#ifdef B1000000
|
457
|
+
case B1000000: mp->data_rate = 1000000; break;
|
458
|
+
#endif
|
459
|
+
}
|
460
|
+
|
461
|
+
switch(params.c_cflag & CSIZE)
|
462
|
+
{
|
463
|
+
case CS5:
|
464
|
+
mp->data_bits = 5;
|
465
|
+
break;
|
466
|
+
case CS6:
|
467
|
+
mp->data_bits = 6;
|
468
|
+
break;
|
469
|
+
case CS7:
|
470
|
+
mp->data_bits = 7;
|
471
|
+
break;
|
472
|
+
case CS8:
|
473
|
+
mp->data_bits = 8;
|
474
|
+
break;
|
475
|
+
default:
|
476
|
+
mp->data_bits = 0;
|
477
|
+
break;
|
478
|
+
}
|
479
|
+
|
480
|
+
mp->stop_bits = (params.c_cflag & CSTOPB ? 2 : 1);
|
481
|
+
|
482
|
+
if (!(params.c_cflag & PARENB))
|
483
|
+
{
|
484
|
+
mp->parity = NONE;
|
485
|
+
}
|
486
|
+
else if (params.c_cflag & PARODD)
|
487
|
+
{
|
488
|
+
mp->parity = ODD;
|
489
|
+
}
|
490
|
+
else
|
491
|
+
{
|
492
|
+
mp->parity = EVEN;
|
493
|
+
}
|
494
|
+
}
|
495
|
+
|
496
|
+
VALUE sp_set_flow_control_impl(self, val)
|
497
|
+
VALUE self, val;
|
498
|
+
{
|
499
|
+
int fd;
|
500
|
+
int flowc;
|
501
|
+
struct termios params;
|
502
|
+
|
503
|
+
Check_Type(val, T_FIXNUM);
|
504
|
+
|
505
|
+
fd = get_fd_helper(self);
|
506
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
507
|
+
{
|
508
|
+
rb_sys_fail(sTcgetattr);
|
509
|
+
}
|
510
|
+
|
511
|
+
flowc = FIX2INT(val);
|
512
|
+
if (flowc & HARD)
|
513
|
+
{
|
514
|
+
#ifdef HAVE_FLOWCONTROL_HARD
|
515
|
+
params.c_cflag |= CRTSCTS;
|
516
|
+
}
|
517
|
+
else
|
518
|
+
{
|
519
|
+
params.c_cflag &= ~CRTSCTS;
|
520
|
+
}
|
521
|
+
#else
|
522
|
+
rb_raise(rb_eIOError, "Hardware flow control not supported");
|
523
|
+
}
|
524
|
+
#endif
|
525
|
+
|
526
|
+
if (flowc & SOFT)
|
527
|
+
{
|
528
|
+
params.c_iflag |= (IXON | IXOFF | IXANY);
|
529
|
+
}
|
530
|
+
else
|
531
|
+
{
|
532
|
+
params.c_iflag &= ~(IXON | IXOFF | IXANY);
|
533
|
+
}
|
534
|
+
|
535
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
536
|
+
{
|
537
|
+
rb_sys_fail(sTcsetattr);
|
538
|
+
}
|
539
|
+
|
540
|
+
return val;
|
541
|
+
}
|
542
|
+
|
543
|
+
VALUE sp_get_flow_control_impl(self)
|
544
|
+
VALUE self;
|
545
|
+
{
|
546
|
+
int ret;
|
547
|
+
int fd;
|
548
|
+
struct termios params;
|
549
|
+
|
550
|
+
fd = get_fd_helper(self);
|
551
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
552
|
+
{
|
553
|
+
rb_sys_fail(sTcgetattr);
|
554
|
+
}
|
555
|
+
|
556
|
+
ret = 0;
|
557
|
+
|
558
|
+
#ifdef HAVE_FLOWCONTROL_HARD
|
559
|
+
if (params.c_cflag & CRTSCTS)
|
560
|
+
{
|
561
|
+
ret += HARD;
|
562
|
+
}
|
563
|
+
#endif
|
564
|
+
|
565
|
+
if (params.c_iflag & (IXON | IXOFF | IXANY))
|
566
|
+
{
|
567
|
+
ret += SOFT;
|
568
|
+
}
|
569
|
+
|
570
|
+
return INT2FIX(ret);
|
571
|
+
}
|
572
|
+
|
573
|
+
VALUE sp_set_read_timeout_impl(self, val)
|
574
|
+
VALUE self, val;
|
575
|
+
{
|
576
|
+
int timeout;
|
577
|
+
int fd;
|
578
|
+
struct termios params;
|
579
|
+
|
580
|
+
Check_Type(val, T_FIXNUM);
|
581
|
+
timeout = FIX2INT(val);
|
582
|
+
|
583
|
+
fd = get_fd_helper(self);
|
584
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
585
|
+
{
|
586
|
+
rb_sys_fail(sTcgetattr);
|
587
|
+
}
|
588
|
+
|
589
|
+
if (timeout < 0)
|
590
|
+
{
|
591
|
+
params.c_cc[VTIME] = 0;
|
592
|
+
params.c_cc[VMIN] = 0;
|
593
|
+
}
|
594
|
+
else if (timeout == 0)
|
595
|
+
{
|
596
|
+
params.c_cc[VTIME] = 0;
|
597
|
+
params.c_cc[VMIN] = 1;
|
598
|
+
}
|
599
|
+
else
|
600
|
+
{
|
601
|
+
params.c_cc[VTIME] = (timeout + 50) / 100;
|
602
|
+
params.c_cc[VMIN] = 0;
|
603
|
+
}
|
604
|
+
|
605
|
+
if (tcsetattr(fd, TCSANOW, ¶ms) == -1)
|
606
|
+
{
|
607
|
+
rb_sys_fail(sTcsetattr);
|
608
|
+
}
|
609
|
+
|
610
|
+
return val;
|
611
|
+
}
|
612
|
+
|
613
|
+
VALUE sp_get_read_timeout_impl(self)
|
614
|
+
VALUE self;
|
615
|
+
{
|
616
|
+
int fd;
|
617
|
+
struct termios params;
|
618
|
+
|
619
|
+
fd = get_fd_helper(self);
|
620
|
+
if (tcgetattr(fd, ¶ms) == -1)
|
621
|
+
{
|
622
|
+
rb_sys_fail(sTcgetattr);
|
623
|
+
}
|
624
|
+
|
625
|
+
if (params.c_cc[VTIME] == 0 && params.c_cc[VMIN] == 0)
|
626
|
+
{
|
627
|
+
return INT2FIX(-1);
|
628
|
+
}
|
629
|
+
|
630
|
+
return INT2FIX(params.c_cc[VTIME] * 100);
|
631
|
+
}
|
632
|
+
|
633
|
+
VALUE sp_set_write_timeout_impl(self, val)
|
634
|
+
VALUE self, val;
|
635
|
+
{
|
636
|
+
rb_notimplement();
|
637
|
+
return self;
|
638
|
+
}
|
639
|
+
|
640
|
+
VALUE sp_get_write_timeout_impl(self)
|
641
|
+
VALUE self;
|
642
|
+
{
|
643
|
+
rb_notimplement();
|
644
|
+
return self;
|
645
|
+
}
|
646
|
+
|
647
|
+
VALUE sp_break_impl(self, time)
|
648
|
+
VALUE self, time;
|
649
|
+
{
|
650
|
+
int fd;
|
651
|
+
|
652
|
+
Check_Type(time, T_FIXNUM);
|
653
|
+
|
654
|
+
fd = get_fd_helper(self);
|
655
|
+
|
656
|
+
if (tcsendbreak(fd, FIX2INT(time) / 3) == -1)
|
657
|
+
{
|
658
|
+
rb_sys_fail("tcsendbreak");
|
659
|
+
}
|
660
|
+
|
661
|
+
return Qnil;
|
662
|
+
}
|
663
|
+
|
664
|
+
void get_line_signals_helper_impl(obj, ls)
|
665
|
+
VALUE obj;
|
666
|
+
struct line_signals *ls;
|
667
|
+
{
|
668
|
+
int fd, status;
|
669
|
+
|
670
|
+
fd = get_fd_helper(obj);
|
671
|
+
|
672
|
+
if (ioctl(fd, TIOCMGET, &status) == -1)
|
673
|
+
{
|
674
|
+
rb_sys_fail(sIoctl);
|
675
|
+
}
|
676
|
+
|
677
|
+
ls->rts = (status & TIOCM_RTS ? 1 : 0);
|
678
|
+
ls->dtr = (status & TIOCM_DTR ? 1 : 0);
|
679
|
+
ls->cts = (status & TIOCM_CTS ? 1 : 0);
|
680
|
+
ls->dsr = (status & TIOCM_DSR ? 1 : 0);
|
681
|
+
ls->dcd = (status & TIOCM_CD ? 1 : 0);
|
682
|
+
ls->ri = (status & TIOCM_RI ? 1 : 0);
|
683
|
+
}
|
684
|
+
|
685
|
+
VALUE set_signal_impl(obj, val, sig)
|
686
|
+
VALUE obj,val;
|
687
|
+
int sig;
|
688
|
+
{
|
689
|
+
int status;
|
690
|
+
int fd;
|
691
|
+
int set;
|
692
|
+
|
693
|
+
Check_Type(val, T_FIXNUM);
|
694
|
+
fd = get_fd_helper(obj);
|
695
|
+
|
696
|
+
if (ioctl(fd, TIOCMGET, &status) == -1)
|
697
|
+
{
|
698
|
+
rb_sys_fail(sIoctl);
|
699
|
+
}
|
700
|
+
|
701
|
+
set = FIX2INT(val);
|
702
|
+
|
703
|
+
if (set == 0)
|
704
|
+
{
|
705
|
+
status &= ~sig;
|
706
|
+
}
|
707
|
+
else if (set == 1)
|
708
|
+
{
|
709
|
+
status |= sig;
|
710
|
+
}
|
711
|
+
else
|
712
|
+
{
|
713
|
+
rb_raise(rb_eArgError, "invalid value");
|
714
|
+
}
|
715
|
+
|
716
|
+
if (ioctl(fd, TIOCMSET, &status) == -1)
|
717
|
+
{
|
718
|
+
rb_sys_fail(sIoctl);
|
719
|
+
}
|
720
|
+
|
721
|
+
return val;
|
722
|
+
}
|
723
|
+
|
724
|
+
VALUE sp_set_rts_impl(self, val)
|
725
|
+
VALUE self, val;
|
726
|
+
{
|
727
|
+
return set_signal_impl(self, val, TIOCM_RTS);
|
728
|
+
}
|
729
|
+
|
730
|
+
VALUE sp_set_dtr_impl(self, val)
|
731
|
+
VALUE self, val;
|
732
|
+
{
|
733
|
+
return set_signal_impl(self, val, TIOCM_DTR);
|
734
|
+
}
|
735
|
+
|
736
|
+
VALUE sp_get_rts_impl(self)
|
737
|
+
VALUE self;
|
738
|
+
{
|
739
|
+
struct line_signals ls;
|
740
|
+
|
741
|
+
get_line_signals_helper_impl(self, &ls);
|
742
|
+
return INT2FIX(ls.rts);
|
743
|
+
}
|
744
|
+
|
745
|
+
VALUE sp_get_dtr_impl(self)
|
746
|
+
VALUE self;
|
747
|
+
{
|
748
|
+
struct line_signals ls;
|
749
|
+
|
750
|
+
get_line_signals_helper_impl(self, &ls);
|
751
|
+
|
752
|
+
return INT2FIX(ls.dtr);
|
753
|
+
}
|
754
|
+
|
755
|
+
VALUE sp_flush_input_data_impl(self)
|
756
|
+
VALUE self;
|
757
|
+
{
|
758
|
+
int fd;
|
759
|
+
int ret;
|
760
|
+
|
761
|
+
fd = get_fd_helper(self);
|
762
|
+
|
763
|
+
ret = tcflush(fd, TCIFLUSH);
|
764
|
+
if(ret<0) {
|
765
|
+
return Qfalse;
|
766
|
+
}
|
767
|
+
|
768
|
+
return Qtrue;
|
769
|
+
}
|
770
|
+
|
771
|
+
VALUE sp_flush_output_data_impl(self)
|
772
|
+
VALUE self;
|
773
|
+
{
|
774
|
+
int fd;
|
775
|
+
int ret;
|
776
|
+
|
777
|
+
fd = get_fd_helper(self);
|
778
|
+
|
779
|
+
ret = tcflush(fd, TCOFLUSH);
|
780
|
+
if(ret<0) {
|
781
|
+
return Qfalse;
|
782
|
+
}
|
783
|
+
|
784
|
+
return Qtrue;
|
785
|
+
}
|
786
|
+
|
787
|
+
|
788
|
+
|
789
|
+
#endif /* !defined(OS_MSWIN) && !defined(OS_BCCWIN) && !defined(OS_MINGW) */
|