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/serialport.c
ADDED
@@ -0,0 +1,263 @@
|
|
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
|
+
VALUE cSerialPort; /* serial port class */
|
23
|
+
|
24
|
+
static VALUE sp_set_data_rate(self, data_rate)
|
25
|
+
VALUE self, data_rate;
|
26
|
+
{
|
27
|
+
VALUE argv[4];
|
28
|
+
|
29
|
+
argv[0] = data_rate;
|
30
|
+
argv[1] = argv[2] = argv[3] = Qnil;
|
31
|
+
sp_set_modem_params(4, argv, self);
|
32
|
+
|
33
|
+
return data_rate;
|
34
|
+
}
|
35
|
+
|
36
|
+
static VALUE sp_set_data_bits(self, data_bits)
|
37
|
+
VALUE self, data_bits;
|
38
|
+
{
|
39
|
+
VALUE argv[4];
|
40
|
+
|
41
|
+
argv[1] = data_bits;
|
42
|
+
argv[0] = argv[2] = argv[3] = Qnil;
|
43
|
+
sp_set_modem_params(4, argv, self);
|
44
|
+
|
45
|
+
return data_bits;
|
46
|
+
}
|
47
|
+
|
48
|
+
static VALUE sp_set_stop_bits(self, stop_bits)
|
49
|
+
VALUE self, stop_bits;
|
50
|
+
{
|
51
|
+
VALUE argv[4];
|
52
|
+
|
53
|
+
argv[2] = stop_bits;
|
54
|
+
argv[0] = argv[1] = argv[3] = Qnil;
|
55
|
+
sp_set_modem_params(4, argv, self);
|
56
|
+
|
57
|
+
return stop_bits;
|
58
|
+
}
|
59
|
+
|
60
|
+
static VALUE sp_set_parity(self, parity)
|
61
|
+
VALUE self, parity;
|
62
|
+
{
|
63
|
+
VALUE argv[4];
|
64
|
+
|
65
|
+
argv[3] = parity;
|
66
|
+
argv[0] = argv[1] = argv[2] = Qnil;
|
67
|
+
sp_set_modem_params(4, argv, self);
|
68
|
+
|
69
|
+
return parity;
|
70
|
+
}
|
71
|
+
|
72
|
+
static VALUE sp_get_data_rate(self)
|
73
|
+
VALUE self;
|
74
|
+
{
|
75
|
+
struct modem_params mp;
|
76
|
+
|
77
|
+
get_modem_params(self, &mp);
|
78
|
+
|
79
|
+
return INT2FIX(mp.data_rate);
|
80
|
+
}
|
81
|
+
|
82
|
+
static VALUE sp_get_data_bits(self)
|
83
|
+
VALUE self;
|
84
|
+
{
|
85
|
+
struct modem_params mp;
|
86
|
+
|
87
|
+
get_modem_params(self, &mp);
|
88
|
+
|
89
|
+
return INT2FIX(mp.data_bits);
|
90
|
+
}
|
91
|
+
|
92
|
+
static VALUE sp_get_stop_bits(self)
|
93
|
+
VALUE self;
|
94
|
+
{
|
95
|
+
struct modem_params mp;
|
96
|
+
|
97
|
+
get_modem_params(self, &mp);
|
98
|
+
|
99
|
+
return INT2FIX(mp.stop_bits);
|
100
|
+
}
|
101
|
+
|
102
|
+
static VALUE sp_get_parity(self)
|
103
|
+
VALUE self;
|
104
|
+
{
|
105
|
+
struct modem_params mp;
|
106
|
+
|
107
|
+
get_modem_params(self, &mp);
|
108
|
+
|
109
|
+
return INT2FIX(mp.parity);
|
110
|
+
}
|
111
|
+
|
112
|
+
static VALUE sp_get_modem_params(self)
|
113
|
+
VALUE self;
|
114
|
+
{
|
115
|
+
struct modem_params mp;
|
116
|
+
VALUE hash;
|
117
|
+
|
118
|
+
get_modem_params(self, &mp);
|
119
|
+
|
120
|
+
hash = rb_hash_new();
|
121
|
+
|
122
|
+
rb_hash_aset(hash, sBaud, INT2FIX(mp.data_rate));
|
123
|
+
rb_hash_aset(hash, sDataBits, INT2FIX(mp.data_bits));
|
124
|
+
rb_hash_aset(hash, sStopBits, INT2FIX(mp.stop_bits));
|
125
|
+
rb_hash_aset(hash, sParity, INT2FIX(mp.parity));
|
126
|
+
|
127
|
+
return hash;
|
128
|
+
}
|
129
|
+
|
130
|
+
static VALUE sp_get_cts(self)
|
131
|
+
VALUE self;
|
132
|
+
{
|
133
|
+
struct line_signals ls;
|
134
|
+
|
135
|
+
get_line_signals_helper(self, &ls);
|
136
|
+
|
137
|
+
return INT2FIX(ls.cts);
|
138
|
+
}
|
139
|
+
|
140
|
+
static VALUE sp_get_dsr(self)
|
141
|
+
VALUE self;
|
142
|
+
{
|
143
|
+
struct line_signals ls;
|
144
|
+
|
145
|
+
get_line_signals_helper(self, &ls);
|
146
|
+
|
147
|
+
return INT2FIX(ls.dsr);
|
148
|
+
}
|
149
|
+
|
150
|
+
static VALUE sp_get_dcd(self)
|
151
|
+
VALUE self;
|
152
|
+
{
|
153
|
+
struct line_signals ls;
|
154
|
+
|
155
|
+
get_line_signals_helper(self, &ls);
|
156
|
+
|
157
|
+
return INT2FIX(ls.dcd);
|
158
|
+
}
|
159
|
+
|
160
|
+
static VALUE sp_get_ri(self)
|
161
|
+
VALUE self;
|
162
|
+
{
|
163
|
+
struct line_signals ls;
|
164
|
+
|
165
|
+
get_line_signals_helper(self, &ls);
|
166
|
+
|
167
|
+
return INT2FIX(ls.ri);
|
168
|
+
}
|
169
|
+
|
170
|
+
static VALUE sp_signals(self)
|
171
|
+
VALUE self;
|
172
|
+
{
|
173
|
+
struct line_signals ls;
|
174
|
+
VALUE hash;
|
175
|
+
|
176
|
+
get_line_signals_helper(self, &ls);
|
177
|
+
|
178
|
+
hash = rb_hash_new();
|
179
|
+
|
180
|
+
#if !(defined(OS_MSWIN) || defined(OS_BCCWIN))
|
181
|
+
rb_hash_aset(hash, sRts, INT2FIX(ls.rts));
|
182
|
+
rb_hash_aset(hash, sDtr, INT2FIX(ls.dtr));
|
183
|
+
#endif
|
184
|
+
rb_hash_aset(hash, sCts, INT2FIX(ls.cts));
|
185
|
+
rb_hash_aset(hash, sDsr, INT2FIX(ls.dsr));
|
186
|
+
rb_hash_aset(hash, sDcd, INT2FIX(ls.dcd));
|
187
|
+
rb_hash_aset(hash, sRi, INT2FIX(ls.ri));
|
188
|
+
|
189
|
+
return hash;
|
190
|
+
}
|
191
|
+
|
192
|
+
void Init_serialport()
|
193
|
+
{
|
194
|
+
sBaud = rb_str_new2("baud");
|
195
|
+
sDataBits = rb_str_new2("data_bits");
|
196
|
+
sStopBits = rb_str_new2("stop_bits");
|
197
|
+
sParity = rb_str_new2("parity");
|
198
|
+
sRts = rb_str_new2("rts");
|
199
|
+
sDtr = rb_str_new2("dtr");
|
200
|
+
sCts = rb_str_new2("cts");
|
201
|
+
sDsr = rb_str_new2("dsr");
|
202
|
+
sDcd = rb_str_new2("dcd");
|
203
|
+
sRi = rb_str_new2("ri");
|
204
|
+
|
205
|
+
rb_gc_register_address(&sBaud);
|
206
|
+
rb_gc_register_address(&sDataBits);
|
207
|
+
rb_gc_register_address(&sStopBits);
|
208
|
+
rb_gc_register_address(&sParity);
|
209
|
+
rb_gc_register_address(&sRts);
|
210
|
+
rb_gc_register_address(&sDtr);
|
211
|
+
rb_gc_register_address(&sCts);
|
212
|
+
rb_gc_register_address(&sDsr);
|
213
|
+
rb_gc_register_address(&sDcd);
|
214
|
+
rb_gc_register_address(&sRi);
|
215
|
+
|
216
|
+
cSerialPort = rb_define_class("SerialPort", rb_cIO);
|
217
|
+
rb_define_singleton_method(cSerialPort, "create", sp_create, 1);
|
218
|
+
|
219
|
+
rb_define_method(cSerialPort, "get_modem_params", sp_get_modem_params, 0);
|
220
|
+
rb_define_method(cSerialPort, "set_modem_params", sp_set_modem_params, -1);
|
221
|
+
rb_define_method(cSerialPort, "modem_params", sp_get_modem_params, 0);
|
222
|
+
rb_define_method(cSerialPort, "modem_params=", sp_set_modem_params, -1);
|
223
|
+
rb_define_method(cSerialPort, "baud", sp_get_data_rate, 0);
|
224
|
+
rb_define_method(cSerialPort, "baud=", sp_set_data_rate, 1);
|
225
|
+
rb_define_method(cSerialPort, "data_bits", sp_get_data_bits, 0);
|
226
|
+
rb_define_method(cSerialPort, "data_bits=", sp_set_data_bits, 1);
|
227
|
+
rb_define_method(cSerialPort, "stop_bits", sp_get_stop_bits, 0);
|
228
|
+
rb_define_method(cSerialPort, "stop_bits=", sp_set_stop_bits, 1);
|
229
|
+
rb_define_method(cSerialPort, "parity", sp_get_parity, 0);
|
230
|
+
rb_define_method(cSerialPort, "parity=", sp_set_parity, 1);
|
231
|
+
|
232
|
+
rb_define_method(cSerialPort, "flow_control=", sp_set_flow_control, 1);
|
233
|
+
rb_define_method(cSerialPort, "flow_control", sp_get_flow_control, 0);
|
234
|
+
|
235
|
+
rb_define_method(cSerialPort, "read_timeout", sp_get_read_timeout, 0);
|
236
|
+
rb_define_method(cSerialPort, "read_timeout=", sp_set_read_timeout, 1);
|
237
|
+
rb_define_method(cSerialPort, "write_timeout", sp_get_write_timeout, 0);
|
238
|
+
rb_define_method(cSerialPort, "write_timeout=", sp_set_write_timeout, 1);
|
239
|
+
|
240
|
+
rb_define_method(cSerialPort, "break", sp_break, 1);
|
241
|
+
|
242
|
+
rb_define_method(cSerialPort, "signals", sp_signals, 0);
|
243
|
+
rb_define_method(cSerialPort, "get_signals", sp_signals, 0);
|
244
|
+
rb_define_method(cSerialPort, "rts", sp_get_rts, 0);
|
245
|
+
rb_define_method(cSerialPort, "rts=", sp_set_rts, 1);
|
246
|
+
rb_define_method(cSerialPort, "dtr", sp_get_dtr, 0);
|
247
|
+
rb_define_method(cSerialPort, "dtr=", sp_set_dtr, 1);
|
248
|
+
rb_define_method(cSerialPort, "cts", sp_get_cts, 0);
|
249
|
+
rb_define_method(cSerialPort, "dsr", sp_get_dsr, 0);
|
250
|
+
rb_define_method(cSerialPort, "dcd", sp_get_dcd, 0);
|
251
|
+
rb_define_method(cSerialPort, "ri", sp_get_ri, 0);
|
252
|
+
|
253
|
+
rb_define_const(cSerialPort, "NONE", INT2FIX(NONE));
|
254
|
+
rb_define_const(cSerialPort, "HARD", INT2FIX(HARD));
|
255
|
+
rb_define_const(cSerialPort, "SOFT", INT2FIX(SOFT));
|
256
|
+
|
257
|
+
rb_define_const(cSerialPort, "SPACE", INT2FIX(SPACE));
|
258
|
+
rb_define_const(cSerialPort, "MARK", INT2FIX(MARK));
|
259
|
+
rb_define_const(cSerialPort, "EVEN", INT2FIX(EVEN));
|
260
|
+
rb_define_const(cSerialPort, "ODD", INT2FIX(ODD));
|
261
|
+
|
262
|
+
rb_define_const(cSerialPort, "VERSION", rb_str_new2(RUBY_SERIAL_PORT_VERSION));
|
263
|
+
}
|
data/ext/serialport.h
ADDED
@@ -0,0 +1,83 @@
|
|
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
|
+
#ifndef _RUBY_SERIAL_PORT_H_
|
21
|
+
#define _RUBY_SERIAL_PORT_H_
|
22
|
+
|
23
|
+
#define RUBY_SERIAL_PORT_VERSION "0.7.0"
|
24
|
+
|
25
|
+
#include <ruby.h> /* ruby inclusion */
|
26
|
+
#include <rubyio.h> /* ruby io inclusion */
|
27
|
+
|
28
|
+
struct modem_params
|
29
|
+
{
|
30
|
+
int data_rate;
|
31
|
+
int data_bits;
|
32
|
+
int stop_bits;
|
33
|
+
int parity;
|
34
|
+
};
|
35
|
+
|
36
|
+
struct line_signals
|
37
|
+
{
|
38
|
+
int rts;
|
39
|
+
int dtr;
|
40
|
+
int cts;
|
41
|
+
int dsr;
|
42
|
+
int dcd;
|
43
|
+
int ri;
|
44
|
+
};
|
45
|
+
|
46
|
+
#define NONE 0
|
47
|
+
#define HARD 1
|
48
|
+
#define SOFT 2
|
49
|
+
|
50
|
+
#if defined(OS_MSWIN) || defined(OS_BCCWIN)
|
51
|
+
#define SPACE SPACEPARITY
|
52
|
+
#define MARK MARKPARITY
|
53
|
+
#define EVEN EVENPARITY
|
54
|
+
#define ODD ODDPARITY
|
55
|
+
#else
|
56
|
+
#define SPACE 0
|
57
|
+
#define MARK 0
|
58
|
+
#define EVEN 1
|
59
|
+
#define ODD 2
|
60
|
+
#endif
|
61
|
+
|
62
|
+
static VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
|
63
|
+
static VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
|
64
|
+
|
65
|
+
|
66
|
+
VALUE sp_create(VALUE class, VALUE _port);
|
67
|
+
VALUE sp_set_modem_params(int argc, VALUE *argv, VALUE self);
|
68
|
+
void get_modem_params(VALUE self, struct modem_params *mp);
|
69
|
+
VALUE sp_set_flow_control(VALUE self, VALUE val);
|
70
|
+
VALUE sp_get_flow_control(VALUE self);
|
71
|
+
VALUE sp_set_read_timeout(VALUE self, VALUE val);
|
72
|
+
VALUE sp_get_read_timeout(VALUE self);
|
73
|
+
VALUE sp_set_write_timeout(VALUE self, VALUE val);
|
74
|
+
VALUE sp_get_write_timeout(VALUE self);
|
75
|
+
VALUE sp_break(VALUE self, VALUE time);
|
76
|
+
void get_line_signals_helper(VALUE obj, struct line_signals *ls);
|
77
|
+
VALUE set_signal(VALUE obj, VALUE val, int sig);
|
78
|
+
VALUE sp_set_rts(VALUE self, VALUE val);
|
79
|
+
VALUE sp_set_dtr(VALUE self, VALUE val);
|
80
|
+
VALUE sp_get_rts(VALUE self);
|
81
|
+
VALUE sp_get_dtr(VALUE self);
|
82
|
+
|
83
|
+
#endif
|
@@ -0,0 +1,616 @@
|
|
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
|
+
#if defined(OS_MSWIN) || defined(OS_BCCWIN)
|
22
|
+
|
23
|
+
#include <stdio.h> /* Standard input/output definitions */
|
24
|
+
#include <io.h> /* Low-level I/O definitions */
|
25
|
+
#include <fcntl.h> /* File control definitions */
|
26
|
+
#include <windows.h> /* Windows standard function definitions */
|
27
|
+
|
28
|
+
#ifndef RB_SERIAL_EXPORT
|
29
|
+
#define RB_SERIAL_EXPORT __declspec(dllexport)
|
30
|
+
//#define RB_SERIAL_EXPORT
|
31
|
+
#endif
|
32
|
+
|
33
|
+
static char sGetCommState[] = "GetCommState";
|
34
|
+
static char sSetCommState[] = "SetCommState";
|
35
|
+
static char sGetCommTimeouts[] = "GetCommTimeouts";
|
36
|
+
static char sSetCommTimeouts[] = "SetCommTimeouts";
|
37
|
+
|
38
|
+
|
39
|
+
static HANDLE get_handle_helper(obj)
|
40
|
+
VALUE obj;
|
41
|
+
{
|
42
|
+
OpenFile *fptr;
|
43
|
+
|
44
|
+
GetOpenFile(obj, fptr);
|
45
|
+
return (HANDLE) _get_osfhandle(fileno(fptr->f));
|
46
|
+
}
|
47
|
+
|
48
|
+
VALUE RB_SERIAL_EXPORT sp_create_impl(class, _port)
|
49
|
+
VALUE class, _port;
|
50
|
+
{
|
51
|
+
OpenFile *fp;
|
52
|
+
int fd;
|
53
|
+
HANDLE fh;
|
54
|
+
int num_port;
|
55
|
+
char *port;
|
56
|
+
char *ports[] = {
|
57
|
+
"COM1", "COM2", "COM3", "COM4",
|
58
|
+
"COM5", "COM6", "COM7", "COM8"
|
59
|
+
};
|
60
|
+
//int new_fd;
|
61
|
+
|
62
|
+
DCB dcb;
|
63
|
+
|
64
|
+
NEWOBJ(sp, struct RFile);
|
65
|
+
rb_secure(4);
|
66
|
+
OBJSETUP(sp, class, T_FILE);
|
67
|
+
MakeOpenFile((VALUE) sp, fp);
|
68
|
+
|
69
|
+
switch(TYPE(_port))
|
70
|
+
{
|
71
|
+
case T_FIXNUM:
|
72
|
+
num_port = FIX2INT(_port);
|
73
|
+
if (num_port < 0 || num_port > sizeof(ports) / sizeof(ports[0]))
|
74
|
+
{
|
75
|
+
rb_raise(rb_eArgError, "illegal port number");
|
76
|
+
}
|
77
|
+
port = ports[num_port];
|
78
|
+
break;
|
79
|
+
|
80
|
+
case T_STRING:
|
81
|
+
Check_SafeStr(_port);
|
82
|
+
port = RSTRING(_port)->ptr;
|
83
|
+
break;
|
84
|
+
|
85
|
+
default:
|
86
|
+
rb_raise(rb_eTypeError, "wrong argument type");
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
|
90
|
+
printf("SerialPort => %s\n", port);
|
91
|
+
fd = open(port, O_BINARY | O_RDWR);
|
92
|
+
printf(" fd => %i\n", fd);
|
93
|
+
if (fd == -1)
|
94
|
+
rb_sys_fail(port);
|
95
|
+
fh = (HANDLE) _get_osfhandle(fd);
|
96
|
+
printf(" fh => %i\n", fh);
|
97
|
+
if (SetupComm(fh, 1024, 1024) == 0)
|
98
|
+
{
|
99
|
+
close(fd);
|
100
|
+
rb_raise(rb_eArgError, "not a serial port");
|
101
|
+
}
|
102
|
+
|
103
|
+
dcb.DCBlength = sizeof(dcb);
|
104
|
+
if (GetCommState(fh, &dcb) == 0)
|
105
|
+
{
|
106
|
+
close(fd);
|
107
|
+
rb_sys_fail(sGetCommState);
|
108
|
+
}
|
109
|
+
dcb.fBinary = TRUE;
|
110
|
+
dcb.fParity = FALSE;
|
111
|
+
dcb.fOutxDsrFlow = FALSE;
|
112
|
+
dcb.fDtrControl = DTR_CONTROL_ENABLE;
|
113
|
+
dcb.fDsrSensitivity = FALSE;
|
114
|
+
dcb.fTXContinueOnXoff = FALSE;
|
115
|
+
dcb.fErrorChar = FALSE;
|
116
|
+
dcb.fNull = FALSE;
|
117
|
+
dcb.fAbortOnError = FALSE;
|
118
|
+
dcb.XonChar = 17;
|
119
|
+
dcb.XoffChar = 19;
|
120
|
+
if (SetCommState(fh, &dcb) == 0)
|
121
|
+
{
|
122
|
+
close(fd);
|
123
|
+
rb_sys_fail(sSetCommState);
|
124
|
+
}
|
125
|
+
|
126
|
+
errno = 0;
|
127
|
+
fp->mode = FMODE_READWRITE | FMODE_BINMODE | FMODE_SYNC;
|
128
|
+
fp->f = fdopen(fd, "rb+");
|
129
|
+
return (VALUE) sp;
|
130
|
+
}
|
131
|
+
|
132
|
+
VALUE RB_SERIAL_EXPORT sp_set_modem_params_impl(argc, argv, self)
|
133
|
+
int argc;
|
134
|
+
VALUE *argv, self;
|
135
|
+
{
|
136
|
+
HANDLE fh;
|
137
|
+
DCB dcb;
|
138
|
+
VALUE _data_rate, _data_bits, _parity, _stop_bits;
|
139
|
+
int use_hash = 0;
|
140
|
+
int data_rate, data_bits, parity;
|
141
|
+
|
142
|
+
if (argc == 0)
|
143
|
+
{
|
144
|
+
return self;
|
145
|
+
}
|
146
|
+
if (argc == 1 && T_HASH == TYPE(argv[0]))
|
147
|
+
{
|
148
|
+
use_hash = 1;
|
149
|
+
_data_rate = rb_hash_aref(argv[0], sBaud);
|
150
|
+
_data_bits = rb_hash_aref(argv[0], sDataBits);
|
151
|
+
_stop_bits = rb_hash_aref(argv[0], sStopBits);
|
152
|
+
_parity = rb_hash_aref(argv[0], sParity);
|
153
|
+
}
|
154
|
+
|
155
|
+
fh = get_handle_helper(self);
|
156
|
+
dcb.DCBlength = sizeof(dcb);
|
157
|
+
if (GetCommState(fh, &dcb) == 0)
|
158
|
+
{
|
159
|
+
rb_sys_fail(sGetCommState);
|
160
|
+
}
|
161
|
+
|
162
|
+
if (!use_hash)
|
163
|
+
{
|
164
|
+
_data_rate = argv[0];
|
165
|
+
}
|
166
|
+
|
167
|
+
if (NIL_P(_data_rate))
|
168
|
+
{
|
169
|
+
goto SkipDataRate;
|
170
|
+
}
|
171
|
+
|
172
|
+
Check_Type(_data_rate, T_FIXNUM);
|
173
|
+
|
174
|
+
data_rate = FIX2INT(_data_rate);
|
175
|
+
switch (data_rate)
|
176
|
+
{
|
177
|
+
case 110:
|
178
|
+
case 300:
|
179
|
+
case 600:
|
180
|
+
case 1200:
|
181
|
+
case 2400:
|
182
|
+
case 4800:
|
183
|
+
case 9600:
|
184
|
+
case 14400:
|
185
|
+
case 19200:
|
186
|
+
case 38400:
|
187
|
+
case 56000:
|
188
|
+
case 57600:
|
189
|
+
case 115200:
|
190
|
+
case 128000:
|
191
|
+
case 256000:
|
192
|
+
dcb.BaudRate = data_rate;
|
193
|
+
break;
|
194
|
+
|
195
|
+
default:
|
196
|
+
rb_raise(rb_eArgError, "unknown baud rate");
|
197
|
+
break;
|
198
|
+
}
|
199
|
+
|
200
|
+
SkipDataRate:
|
201
|
+
|
202
|
+
if (!use_hash)
|
203
|
+
{
|
204
|
+
_data_bits = (argc >= 2 ? argv[1] : INT2FIX(8));
|
205
|
+
}
|
206
|
+
|
207
|
+
if (NIL_P(_data_bits))
|
208
|
+
{
|
209
|
+
goto SkipDataBits;
|
210
|
+
}
|
211
|
+
|
212
|
+
Check_Type(_data_bits, T_FIXNUM);
|
213
|
+
|
214
|
+
data_bits = FIX2INT(_data_bits);
|
215
|
+
if (4 <= data_bits && data_bits <= 8)
|
216
|
+
{
|
217
|
+
dcb.ByteSize = data_bits;
|
218
|
+
}
|
219
|
+
else
|
220
|
+
{
|
221
|
+
rb_raise(rb_eArgError, "unknown character size");
|
222
|
+
}
|
223
|
+
|
224
|
+
SkipDataBits:
|
225
|
+
|
226
|
+
if (!use_hash)
|
227
|
+
{
|
228
|
+
_stop_bits = (argc >= 3 ? argv[2] : INT2FIX(1));
|
229
|
+
}
|
230
|
+
|
231
|
+
if (NIL_P(_stop_bits))
|
232
|
+
{
|
233
|
+
goto SkipStopBits;
|
234
|
+
}
|
235
|
+
|
236
|
+
Check_Type(_stop_bits, T_FIXNUM);
|
237
|
+
|
238
|
+
switch (FIX2INT(_stop_bits))
|
239
|
+
{
|
240
|
+
case 1:
|
241
|
+
dcb.StopBits = ONESTOPBIT;
|
242
|
+
break;
|
243
|
+
case 2:
|
244
|
+
dcb.StopBits = TWOSTOPBITS;
|
245
|
+
break;
|
246
|
+
default:
|
247
|
+
rb_raise(rb_eArgError, "unknown number of stop bits");
|
248
|
+
break;
|
249
|
+
}
|
250
|
+
|
251
|
+
SkipStopBits:
|
252
|
+
|
253
|
+
if (!use_hash)
|
254
|
+
{
|
255
|
+
_parity = (argc >= 4 ? argv[3] : (dcb.ByteSize == 8 ?
|
256
|
+
INT2FIX(NOPARITY) : INT2FIX(EVENPARITY)));
|
257
|
+
}
|
258
|
+
|
259
|
+
if (NIL_P(_parity))
|
260
|
+
{
|
261
|
+
goto SkipParity;
|
262
|
+
}
|
263
|
+
|
264
|
+
Check_Type(_parity, T_FIXNUM);
|
265
|
+
|
266
|
+
parity = FIX2INT(_parity);
|
267
|
+
switch (parity)
|
268
|
+
{
|
269
|
+
case EVENPARITY:
|
270
|
+
case ODDPARITY:
|
271
|
+
case MARKPARITY:
|
272
|
+
case SPACEPARITY:
|
273
|
+
case NOPARITY:
|
274
|
+
dcb.Parity = parity;
|
275
|
+
break;
|
276
|
+
|
277
|
+
default:
|
278
|
+
rb_raise(rb_eArgError, "unknown parity");
|
279
|
+
break;
|
280
|
+
}
|
281
|
+
|
282
|
+
SkipParity:
|
283
|
+
|
284
|
+
if (SetCommState(fh, &dcb) == 0)
|
285
|
+
{
|
286
|
+
rb_sys_fail(sSetCommState);
|
287
|
+
}
|
288
|
+
|
289
|
+
return argv[0];
|
290
|
+
}
|
291
|
+
|
292
|
+
void RB_SERIAL_EXPORT get_modem_params_impl(self, mp)
|
293
|
+
VALUE self;
|
294
|
+
struct modem_params *mp;
|
295
|
+
{
|
296
|
+
HANDLE fh;
|
297
|
+
DCB dcb;
|
298
|
+
|
299
|
+
fh = get_handle_helper(self);
|
300
|
+
dcb.DCBlength = sizeof(dcb);
|
301
|
+
if (GetCommState(fh, &dcb) == 0)
|
302
|
+
{
|
303
|
+
rb_sys_fail(sGetCommState);
|
304
|
+
}
|
305
|
+
|
306
|
+
mp->data_rate = dcb.BaudRate;
|
307
|
+
mp->data_bits = dcb.ByteSize;
|
308
|
+
mp->stop_bits = (dcb.StopBits == ONESTOPBIT ? 1 : 2);
|
309
|
+
mp->parity = dcb.Parity;
|
310
|
+
}
|
311
|
+
|
312
|
+
VALUE RB_SERIAL_EXPORT sp_set_flow_control_impl(self, val)
|
313
|
+
VALUE self, val;
|
314
|
+
{
|
315
|
+
HANDLE fh;
|
316
|
+
int flowc;
|
317
|
+
DCB dcb;
|
318
|
+
|
319
|
+
Check_Type(val, T_FIXNUM);
|
320
|
+
|
321
|
+
fh = get_handle_helper(self);
|
322
|
+
dcb.DCBlength = sizeof(dcb);
|
323
|
+
if (GetCommState(fh, &dcb) == 0)
|
324
|
+
{
|
325
|
+
rb_sys_fail(sGetCommState);
|
326
|
+
}
|
327
|
+
|
328
|
+
flowc = FIX2INT(val);
|
329
|
+
if (flowc & HARD)
|
330
|
+
{
|
331
|
+
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
|
332
|
+
dcb.fOutxCtsFlow = TRUE;
|
333
|
+
}
|
334
|
+
else
|
335
|
+
{
|
336
|
+
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
337
|
+
dcb.fOutxCtsFlow = FALSE;
|
338
|
+
}
|
339
|
+
|
340
|
+
if (flowc & SOFT)
|
341
|
+
{
|
342
|
+
dcb.fOutX = dcb.fInX = TRUE;
|
343
|
+
}
|
344
|
+
else
|
345
|
+
{
|
346
|
+
dcb.fOutX = dcb.fInX = FALSE;
|
347
|
+
}
|
348
|
+
|
349
|
+
if (SetCommState(fh, &dcb) == 0)
|
350
|
+
{
|
351
|
+
rb_sys_fail(sSetCommState);
|
352
|
+
}
|
353
|
+
|
354
|
+
return val;
|
355
|
+
}
|
356
|
+
|
357
|
+
VALUE RB_SERIAL_EXPORT sp_get_flow_control_impl(self)
|
358
|
+
VALUE self;
|
359
|
+
{
|
360
|
+
HANDLE fh;
|
361
|
+
int ret;
|
362
|
+
DCB dcb;
|
363
|
+
|
364
|
+
fh = get_handle_helper(self);
|
365
|
+
dcb.DCBlength = sizeof(dcb);
|
366
|
+
if (GetCommState(fh, &dcb) == 0)
|
367
|
+
{
|
368
|
+
rb_sys_fail(sGetCommState);
|
369
|
+
}
|
370
|
+
|
371
|
+
ret = 0;
|
372
|
+
if (dcb.fOutxCtsFlow)
|
373
|
+
{
|
374
|
+
ret += HARD;
|
375
|
+
}
|
376
|
+
|
377
|
+
if (dcb.fOutX)
|
378
|
+
{
|
379
|
+
ret += SOFT;
|
380
|
+
}
|
381
|
+
|
382
|
+
return INT2FIX(ret);
|
383
|
+
}
|
384
|
+
|
385
|
+
VALUE RB_SERIAL_EXPORT sp_set_read_timeout_impl(self, val)
|
386
|
+
VALUE self, val;
|
387
|
+
{
|
388
|
+
int timeout;
|
389
|
+
HANDLE fh;
|
390
|
+
COMMTIMEOUTS ctout;
|
391
|
+
|
392
|
+
Check_Type(val, T_FIXNUM);
|
393
|
+
timeout = FIX2INT(val);
|
394
|
+
|
395
|
+
fh = get_handle_helper(self);
|
396
|
+
if (GetCommTimeouts(fh, &ctout) == 0)
|
397
|
+
{
|
398
|
+
rb_sys_fail(sGetCommTimeouts);
|
399
|
+
}
|
400
|
+
|
401
|
+
if (timeout < 0)
|
402
|
+
{
|
403
|
+
ctout.ReadIntervalTimeout = MAXDWORD;
|
404
|
+
ctout.ReadTotalTimeoutMultiplier = 0;
|
405
|
+
ctout.ReadTotalTimeoutConstant = 0;
|
406
|
+
}
|
407
|
+
else if (timeout == 0)
|
408
|
+
{
|
409
|
+
ctout.ReadIntervalTimeout = MAXDWORD;
|
410
|
+
ctout.ReadTotalTimeoutMultiplier = MAXDWORD;
|
411
|
+
ctout.ReadTotalTimeoutConstant = MAXDWORD - 1;
|
412
|
+
}
|
413
|
+
else
|
414
|
+
{
|
415
|
+
ctout.ReadIntervalTimeout = timeout;
|
416
|
+
ctout.ReadTotalTimeoutMultiplier = 0;
|
417
|
+
ctout.ReadTotalTimeoutConstant = timeout;
|
418
|
+
}
|
419
|
+
|
420
|
+
if (SetCommTimeouts(fh, &ctout) == 0)
|
421
|
+
{
|
422
|
+
rb_sys_fail(sSetCommTimeouts);
|
423
|
+
}
|
424
|
+
|
425
|
+
return val;
|
426
|
+
}
|
427
|
+
|
428
|
+
VALUE RB_SERIAL_EXPORT sp_get_read_timeout_impl(self)
|
429
|
+
VALUE self;
|
430
|
+
{
|
431
|
+
HANDLE fh;
|
432
|
+
COMMTIMEOUTS ctout;
|
433
|
+
|
434
|
+
fh = get_handle_helper(self);
|
435
|
+
if (GetCommTimeouts(fh, &ctout) == 0)
|
436
|
+
{
|
437
|
+
rb_sys_fail(sGetCommTimeouts);
|
438
|
+
}
|
439
|
+
|
440
|
+
switch (ctout.ReadTotalTimeoutConstant)
|
441
|
+
{
|
442
|
+
case 0:
|
443
|
+
return INT2FIX(-1);
|
444
|
+
case MAXDWORD:
|
445
|
+
return INT2FIX(0);
|
446
|
+
}
|
447
|
+
|
448
|
+
return INT2FIX(ctout.ReadTotalTimeoutConstant);
|
449
|
+
}
|
450
|
+
|
451
|
+
VALUE RB_SERIAL_EXPORT sp_set_write_timeout_impl(self, val)
|
452
|
+
VALUE self, val;
|
453
|
+
{
|
454
|
+
int timeout;
|
455
|
+
HANDLE fh;
|
456
|
+
COMMTIMEOUTS ctout;
|
457
|
+
|
458
|
+
Check_Type(val, T_FIXNUM);
|
459
|
+
timeout = FIX2INT(val);
|
460
|
+
|
461
|
+
fh = get_handle_helper(self);
|
462
|
+
if (GetCommTimeouts(fh, &ctout) == 0)
|
463
|
+
{
|
464
|
+
rb_sys_fail(sGetCommTimeouts);
|
465
|
+
}
|
466
|
+
|
467
|
+
if (timeout <= 0)
|
468
|
+
{
|
469
|
+
ctout.WriteTotalTimeoutMultiplier = 0;
|
470
|
+
ctout.WriteTotalTimeoutConstant = 0;
|
471
|
+
}
|
472
|
+
else
|
473
|
+
{
|
474
|
+
ctout.WriteTotalTimeoutMultiplier = timeout;
|
475
|
+
ctout.WriteTotalTimeoutConstant = 0;
|
476
|
+
}
|
477
|
+
|
478
|
+
if (SetCommTimeouts(fh, &ctout) == 0)
|
479
|
+
{
|
480
|
+
rb_sys_fail(sSetCommTimeouts);
|
481
|
+
}
|
482
|
+
|
483
|
+
return val;
|
484
|
+
}
|
485
|
+
|
486
|
+
VALUE RB_SERIAL_EXPORT sp_get_write_timeout_impl(self)
|
487
|
+
VALUE self;
|
488
|
+
{
|
489
|
+
HANDLE fh;
|
490
|
+
COMMTIMEOUTS ctout;
|
491
|
+
|
492
|
+
fh = get_handle_helper(self);
|
493
|
+
if (GetCommTimeouts(fh, &ctout) == 0)
|
494
|
+
{
|
495
|
+
rb_sys_fail(sGetCommTimeouts);
|
496
|
+
}
|
497
|
+
|
498
|
+
return INT2FIX(ctout.WriteTotalTimeoutMultiplier);
|
499
|
+
}
|
500
|
+
|
501
|
+
static void delay_ms(time)
|
502
|
+
int time;
|
503
|
+
{
|
504
|
+
HANDLE ev;
|
505
|
+
|
506
|
+
ev = CreateEvent(NULL, FALSE, FALSE, NULL);
|
507
|
+
if (!ev)
|
508
|
+
{
|
509
|
+
rb_sys_fail("CreateEvent");
|
510
|
+
}
|
511
|
+
|
512
|
+
if (WaitForSingleObject(ev, time) == WAIT_FAILED)
|
513
|
+
{
|
514
|
+
rb_sys_fail("WaitForSingleObject");
|
515
|
+
}
|
516
|
+
|
517
|
+
CloseHandle(ev);
|
518
|
+
}
|
519
|
+
|
520
|
+
VALUE RB_SERIAL_EXPORT sp_break_impl(self, time)
|
521
|
+
VALUE self, time;
|
522
|
+
{
|
523
|
+
HANDLE fh;
|
524
|
+
|
525
|
+
Check_Type(time, T_FIXNUM);
|
526
|
+
|
527
|
+
fh = get_handle_helper(self);
|
528
|
+
if (SetCommBreak(fh) == 0)
|
529
|
+
{
|
530
|
+
rb_sys_fail("SetCommBreak");
|
531
|
+
}
|
532
|
+
|
533
|
+
delay_ms(FIX2INT(time) * 100);
|
534
|
+
ClearCommBreak(fh);
|
535
|
+
|
536
|
+
return Qnil;
|
537
|
+
}
|
538
|
+
|
539
|
+
void RB_SERIAL_EXPORT get_line_signals_helper(obj, ls)
|
540
|
+
VALUE obj;
|
541
|
+
struct line_signals *ls;
|
542
|
+
{
|
543
|
+
HANDLE fh;
|
544
|
+
int status;
|
545
|
+
|
546
|
+
fh = get_handle_helper(obj);
|
547
|
+
if (GetCommModemStatus(fh, &status) == 0)
|
548
|
+
{
|
549
|
+
rb_sys_fail("GetCommModemStatus");
|
550
|
+
}
|
551
|
+
|
552
|
+
ls->cts = (status & MS_CTS_ON ? 1 : 0);
|
553
|
+
ls->dsr = (status & MS_DSR_ON ? 1 : 0);
|
554
|
+
ls->dcd = (status & MS_RLSD_ON ? 1 : 0);
|
555
|
+
ls->ri = (status & MS_RING_ON ? 1 : 0);
|
556
|
+
}
|
557
|
+
|
558
|
+
static VALUE set_signal(obj, val, sigoff, sigon)
|
559
|
+
VALUE obj,val;
|
560
|
+
int sigoff, sigon;
|
561
|
+
{
|
562
|
+
HANDLE fh;
|
563
|
+
int set, sig;
|
564
|
+
|
565
|
+
Check_Type(val, T_FIXNUM);
|
566
|
+
fh = get_handle_helper(obj);
|
567
|
+
|
568
|
+
set = FIX2INT(val);
|
569
|
+
if (set == 0)
|
570
|
+
{
|
571
|
+
sig = sigoff;
|
572
|
+
}
|
573
|
+
else if (set == 1)
|
574
|
+
{
|
575
|
+
sig = sigon;
|
576
|
+
}
|
577
|
+
else
|
578
|
+
{
|
579
|
+
rb_raise(rb_eArgError, "invalid value");
|
580
|
+
}
|
581
|
+
|
582
|
+
if (EscapeCommFunction(fh, sig) == 0)
|
583
|
+
{
|
584
|
+
rb_sys_fail("EscapeCommFunction");
|
585
|
+
}
|
586
|
+
|
587
|
+
return val;
|
588
|
+
}
|
589
|
+
|
590
|
+
VALUE RB_SERIAL_EXPORT sp_set_rts_impl(self, val)
|
591
|
+
VALUE self, val;
|
592
|
+
{
|
593
|
+
return set_signal(self, val, CLRRTS, SETRTS);
|
594
|
+
}
|
595
|
+
|
596
|
+
VALUE RB_SERIAL_EXPORT sp_set_dtr_impl(self, val)
|
597
|
+
VALUE self, val;
|
598
|
+
{
|
599
|
+
return set_signal(self, val, CLRDTR, SETDTR);
|
600
|
+
}
|
601
|
+
|
602
|
+
VALUE RB_SERIAL_EXPORT sp_get_rts_impl(self)
|
603
|
+
VALUE self;
|
604
|
+
{
|
605
|
+
rb_notimplement();
|
606
|
+
return self;
|
607
|
+
}
|
608
|
+
|
609
|
+
VALUE RB_SERIAL_EXPORT sp_get_dtr_impl(self)
|
610
|
+
VALUE self;
|
611
|
+
{
|
612
|
+
rb_notimplement();
|
613
|
+
return self;
|
614
|
+
}
|
615
|
+
|
616
|
+
#endif /* defined(OS_MSWIN) || defined(OS_BCCWIN) */
|