Floppy-rb232 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +8 -7
- data/lib/rb232/text_protocol.rb +18 -0
- data/src/port.c +40 -12
- data/src/port.h +39 -8
- data/src/rb232.c +10 -11
- data/src/utility.c +10 -0
- metadata +2 -2
data/README
CHANGED
@@ -45,10 +45,11 @@ You can provide alternative settings when you create a new port:
|
|
45
45
|
:parity => true,
|
46
46
|
:stop_bits => 2)
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
48
|
+
See http://github.com/Floppy/rb232/tree/master/spec/port_spec.rb or RB232::Port
|
49
|
+
documentation for more details.
|
50
|
+
|
51
|
+
If you are using a simple text protocol over RS232, you can use the
|
52
|
+
RB232::TextProtocol class to help you out. It automatically monitors the port
|
53
|
+
and splits messages up by detecting separator characters. See
|
54
|
+
http://github.com/Floppy/rb232/tree/master/examples/listen.rb for an example of
|
55
|
+
how to use this class.
|
data/lib/rb232/text_protocol.rb
CHANGED
@@ -3,18 +3,35 @@ require 'observer'
|
|
3
3
|
|
4
4
|
module RB232
|
5
5
|
|
6
|
+
# A helper class for RB232::Port which implements a simple text-based
|
7
|
+
# protocol on top of a serial port. Data is read from the serial port and split
|
8
|
+
# into individual messages based on a separator character.
|
9
|
+
#
|
10
|
+
# This class is Observable. Client code should implement an update(string) function
|
11
|
+
# add call TextProtocol#add_observer(self). When a complete message is received,
|
12
|
+
# the update() function will be called with the message string.
|
6
13
|
class TextProtocol
|
7
14
|
|
8
15
|
include Observable
|
9
16
|
|
17
|
+
# Create a protocol object. _port_ should be a RB232::Port object.
|
18
|
+
# _separator_ is the character which separates messages in the text protocol,
|
19
|
+
# "\n" by default.
|
10
20
|
def initialize(port, separator = "\n")
|
11
21
|
@port = port
|
12
22
|
@separator = separator
|
13
23
|
end
|
14
24
|
|
25
|
+
# Separator character, as specified in TextProtocol#new
|
15
26
|
attr_reader :separator
|
27
|
+
|
28
|
+
# Port object, as specified in TextProtocol#new
|
16
29
|
attr_reader :port
|
17
30
|
|
31
|
+
# Begin processing incoming data from the serial port.
|
32
|
+
# A thread is started which monitors the port for data and detects
|
33
|
+
# complete messages.
|
34
|
+
# Call TextProtocol#stop to halt this process.
|
18
35
|
def start
|
19
36
|
@stop = false
|
20
37
|
@reader_thread = Thread.new {
|
@@ -31,6 +48,7 @@ module RB232
|
|
31
48
|
}
|
32
49
|
end
|
33
50
|
|
51
|
+
# Stop processing incoming data from the serial port.
|
34
52
|
def stop
|
35
53
|
@stop = true
|
36
54
|
@reader_thread.join
|
data/src/port.c
CHANGED
@@ -169,10 +169,14 @@ VALUE rb232_port_initialize_with_options(VALUE self, VALUE port, VALUE options)
|
|
169
169
|
}
|
170
170
|
|
171
171
|
/*
|
172
|
-
*
|
173
|
-
*
|
174
|
-
*
|
175
|
-
*
|
172
|
+
* Create a Port object, using the port filename specified in _port_ (e.g. '/dev/ttyS0' or 'COM1').
|
173
|
+
*
|
174
|
+
* Valid options are :baud_rate (integer), :data_bits (integer), :parity
|
175
|
+
* (boolean), and :stop_bits (integer). Default values are 9600, 8, false, and 1 respectively.
|
176
|
+
*
|
177
|
+
* call-seq:
|
178
|
+
* new(port, options = {})
|
179
|
+
*
|
176
180
|
*/
|
177
181
|
VALUE rb232_port_initialize(int argc, VALUE* argv, VALUE self) {
|
178
182
|
/* Only allow 1 or 2 arguments */
|
@@ -193,25 +197,33 @@ VALUE rb232_port_initialize(int argc, VALUE* argv, VALUE self) {
|
|
193
197
|
return rb232_port_initialize_with_options(self, port, options);
|
194
198
|
}
|
195
199
|
|
196
|
-
/*
|
200
|
+
/*
|
201
|
+
* Get the port name (for instance, '/dev/ttyS0' or 'COM1'), as set in Port#new.
|
202
|
+
*/
|
197
203
|
VALUE rb232_port_get_port_name(VALUE self) {
|
198
204
|
/* Return baud rate */
|
199
205
|
return rb_str_new2(get_port_data(self)->port_name);
|
200
206
|
}
|
201
207
|
|
202
|
-
/*
|
208
|
+
/*
|
209
|
+
* Get the baud rate, as set in the _options_ argument to Port#new.
|
210
|
+
*/
|
203
211
|
VALUE rb232_port_get_baud_rate(VALUE self) {
|
204
212
|
/* Return baud rate */
|
205
213
|
return rb_uint_new(get_port_data(self)->baud_rate);
|
206
214
|
}
|
207
215
|
|
208
|
-
/*
|
216
|
+
/*
|
217
|
+
* Get the number of data bits, as set in the _options_ argument to Port#new.
|
218
|
+
*/
|
209
219
|
VALUE rb232_port_get_data_bits(VALUE self) {
|
210
220
|
/* Return baud rate */
|
211
221
|
return rb_uint_new(get_port_data(self)->data_bits);
|
212
222
|
}
|
213
223
|
|
214
|
-
/*
|
224
|
+
/*
|
225
|
+
* Get the parity setting, as set in the _options_ argument to Port#new.
|
226
|
+
*/
|
215
227
|
VALUE rb232_port_get_parity(VALUE self) {
|
216
228
|
/* Return baud rate */
|
217
229
|
if (get_port_data(self)->parity == TRUE)
|
@@ -220,13 +232,17 @@ VALUE rb232_port_get_parity(VALUE self) {
|
|
220
232
|
return Qfalse;
|
221
233
|
}
|
222
234
|
|
223
|
-
/*
|
235
|
+
/*
|
236
|
+
* Get the number of stop bits, as set in the _options_ argument to Port#new.
|
237
|
+
*/
|
224
238
|
VALUE rb232_port_get_stop_bits(VALUE self) {
|
225
239
|
/* Return baud rate */
|
226
240
|
return rb_uint_new(get_port_data(self)->stop_bits);
|
227
241
|
}
|
228
242
|
|
229
|
-
/*
|
243
|
+
/*
|
244
|
+
* Read raw data from port
|
245
|
+
*/
|
230
246
|
int rb232_port_read(VALUE self, char* buffer, VALUE count) {
|
231
247
|
int bytes_to_read = NUM2INT(count);
|
232
248
|
if (bytes_to_read > 255)
|
@@ -234,7 +250,13 @@ int rb232_port_read(VALUE self, char* buffer, VALUE count) {
|
|
234
250
|
return read(get_port_data(self)->port_handle, buffer, bytes_to_read);
|
235
251
|
}
|
236
252
|
|
237
|
-
/*
|
253
|
+
/*
|
254
|
+
* Read _count_ raw byte values from the port.
|
255
|
+
* Returns an array of values. Useful for binary protocols.
|
256
|
+
* call-seq:
|
257
|
+
* read_bytes(count)
|
258
|
+
*
|
259
|
+
*/
|
238
260
|
VALUE rb232_port_read_bytes(VALUE self, VALUE count) {
|
239
261
|
char buffer[256];
|
240
262
|
int bytes_read = rb232_port_read(self, buffer, count);
|
@@ -245,7 +267,13 @@ VALUE rb232_port_read_bytes(VALUE self, VALUE count) {
|
|
245
267
|
}
|
246
268
|
}
|
247
269
|
|
248
|
-
/*
|
270
|
+
/*
|
271
|
+
* Read _count_ characters from the port.
|
272
|
+
* Returns a string. Useful for text-based protocols.
|
273
|
+
* call-seq:
|
274
|
+
* read_string(count)
|
275
|
+
*
|
276
|
+
*/
|
249
277
|
VALUE rb232_port_read_string(VALUE self, VALUE count) {
|
250
278
|
char buffer[256];
|
251
279
|
int bytes_read = rb232_port_read(self, buffer, count);
|
data/src/port.h
CHANGED
@@ -9,26 +9,57 @@ extern VALUE RB232_Port;
|
|
9
9
|
/* Allocator for Port class */
|
10
10
|
VALUE rb232_port_alloc(VALUE klass);
|
11
11
|
|
12
|
-
/*
|
12
|
+
/*
|
13
|
+
* Create a Port object, using the port filename specified in _port_ (e.g. '/dev/ttyS0' or 'COM1').
|
14
|
+
*
|
15
|
+
* Valid options are :baud_rate (integer), :data_bits (integer), :parity
|
16
|
+
* (boolean), and :stop_bits (integer). Default values are 9600, 8, false, and 1 respectively.
|
17
|
+
*
|
18
|
+
* call-seq:
|
19
|
+
* new(port, options = {})
|
20
|
+
*
|
21
|
+
*/
|
13
22
|
VALUE rb232_port_initialize(int argc, VALUE* argv, VALUE self);
|
14
23
|
|
15
|
-
/*
|
24
|
+
/*
|
25
|
+
* Get the port name (for instance, '/dev/ttyS0' or 'COM1'), as set in Port#new.
|
26
|
+
*/
|
16
27
|
VALUE rb232_port_get_port_name(VALUE self);
|
17
28
|
|
18
|
-
/*
|
29
|
+
/*
|
30
|
+
* Get the baud rate, as set in the _options_ argument to Port#new.
|
31
|
+
*/
|
19
32
|
VALUE rb232_port_get_baud_rate(VALUE self);
|
20
33
|
|
21
|
-
/*
|
34
|
+
/*
|
35
|
+
* Get the number of data bits, as set in the _options_ argument to Port#new.
|
36
|
+
*/
|
22
37
|
VALUE rb232_port_get_data_bits(VALUE self);
|
23
38
|
|
24
|
-
/*
|
39
|
+
/*
|
40
|
+
* Get the parity setting, as set in the _options_ argument to Port#new.
|
41
|
+
*/
|
25
42
|
VALUE rb232_port_get_parity(VALUE self);
|
26
43
|
|
27
|
-
/*
|
44
|
+
/*
|
45
|
+
* Get the number of stop bits, as set in the _options_ argument to Port#new.
|
46
|
+
*/
|
28
47
|
VALUE rb232_port_get_stop_bits(VALUE self);
|
29
48
|
|
30
|
-
/*
|
49
|
+
/*
|
50
|
+
* Read _count_ raw byte values from the port.
|
51
|
+
* Returns an array of values. Useful for binary protocols.
|
52
|
+
* call-seq:
|
53
|
+
* read_bytes(count)
|
54
|
+
*
|
55
|
+
*/
|
31
56
|
VALUE rb232_port_read_bytes(VALUE self, VALUE count);
|
32
57
|
|
33
|
-
/*
|
58
|
+
/*
|
59
|
+
* Read _count_ characters from the port.
|
60
|
+
* Returns a string. Useful for text-based protocols.
|
61
|
+
* call-seq:
|
62
|
+
* read_string(count)
|
63
|
+
*
|
64
|
+
*/
|
34
65
|
VALUE rb232_port_read_string(VALUE self, VALUE count);
|
data/src/rb232.c
CHANGED
@@ -2,20 +2,19 @@
|
|
2
2
|
#include "port.h"
|
3
3
|
|
4
4
|
/*
|
5
|
-
*
|
6
|
-
*
|
7
|
-
* Called automatically on require 'rb232'.
|
5
|
+
* Serial port communications. The class RB232::Port provides access to a hardware
|
6
|
+
* port on the local machine. Currently only Linux systems are supported.
|
8
7
|
*/
|
9
8
|
void Init_rb232() {
|
10
9
|
RB232 = rb_define_module("RB232");
|
11
10
|
RB232_Port = rb_define_class_under(RB232, "Port", rb_cObject);
|
12
11
|
rb_define_alloc_func(RB232_Port, rb232_port_alloc);
|
13
|
-
rb_define_method(RB232_Port, "initialize", rb232_port_initialize, -1);
|
14
|
-
rb_define_method(RB232_Port, "port_name", rb232_port_get_port_name, 0);
|
15
|
-
rb_define_method(RB232_Port, "baud_rate", rb232_port_get_baud_rate, 0);
|
16
|
-
rb_define_method(RB232_Port, "data_bits", rb232_port_get_data_bits, 0);
|
17
|
-
rb_define_method(RB232_Port, "parity", rb232_port_get_parity, 0);
|
18
|
-
rb_define_method(RB232_Port, "stop_bits", rb232_port_get_stop_bits, 0);
|
19
|
-
rb_define_method(RB232_Port, "read_bytes", rb232_port_read_bytes, 1);
|
20
|
-
rb_define_method(RB232_Port, "read_string", rb232_port_read_string, 1);
|
12
|
+
rb_define_method(RB232_Port, "initialize", rb232_port_initialize, -1); /* in port.c */
|
13
|
+
rb_define_method(RB232_Port, "port_name", rb232_port_get_port_name, 0); /* in port.c */
|
14
|
+
rb_define_method(RB232_Port, "baud_rate", rb232_port_get_baud_rate, 0); /* in port.c */
|
15
|
+
rb_define_method(RB232_Port, "data_bits", rb232_port_get_data_bits, 0); /* in port.c */
|
16
|
+
rb_define_method(RB232_Port, "parity", rb232_port_get_parity, 0); /* in port.c */
|
17
|
+
rb_define_method(RB232_Port, "stop_bits", rb232_port_get_stop_bits, 0); /* in port.c */
|
18
|
+
rb_define_method(RB232_Port, "read_bytes", rb232_port_read_bytes, 1); /* in port.c */
|
19
|
+
rb_define_method(RB232_Port, "read_string", rb232_port_read_string, 1); /* in port.c */
|
21
20
|
}
|
data/src/utility.c
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
#include "utility.h"
|
2
2
|
|
3
|
+
/*
|
4
|
+
* Get a key from a hash, or if it's not there, use the default.
|
5
|
+
* A bit like doing hash[key] || default_val in Ruby.
|
6
|
+
* Integer version.
|
7
|
+
*/
|
3
8
|
int rbx_int_from_hash_or_default(VALUE hash, VALUE key, int default_val) {
|
4
9
|
VALUE data = (rb_hash_aref(hash, key));
|
5
10
|
if (data == Qnil)
|
@@ -8,6 +13,11 @@ int rbx_int_from_hash_or_default(VALUE hash, VALUE key, int default_val) {
|
|
8
13
|
return NUM2INT(data);
|
9
14
|
}
|
10
15
|
|
16
|
+
/*
|
17
|
+
* Get a key from a hash, or if it's not there, use the default.
|
18
|
+
* A bit like doing hash[key] || default_val in Ruby.
|
19
|
+
* Boolean version.
|
20
|
+
*/
|
11
21
|
BOOL rbx_bool_from_hash_or_default(VALUE hash, VALUE key, BOOL default_val) {
|
12
22
|
VALUE data = (rb_hash_aref(hash, key));
|
13
23
|
if (data == Qnil)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Floppy-rb232
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Smith
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|