Floppy-rb232 0.2.2 → 0.2.3

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/extconf.rb CHANGED
@@ -1,2 +1,5 @@
1
1
  require 'mkmf'
2
+
3
+ $CFLAGS = '-Wall ' + $CFLAGS
4
+
2
5
  create_makefile('rb232', 'src')
@@ -37,13 +37,21 @@ module RB232
37
37
  @reader_thread = Thread.new {
38
38
  buffer = ""
39
39
  while (@stop == false)
40
- buffer += @port.read_string(1)
41
- messages = buffer.split(@separator,3)
42
- if messages.size > 1
43
- changed
44
- notify_observers(messages[0])
45
- buffer = ""
46
- end
40
+ new_string = ""
41
+ # Read in as much data as possible
42
+ begin
43
+ new_string = @port.read_string(255)
44
+ if (new_string.size > 0)
45
+ buffer += new_string
46
+ messages = buffer.split(@separator,2)
47
+ if messages.size > 1
48
+ changed
49
+ notify_observers(messages[0])
50
+ buffer = messages[1] || ""
51
+ end
52
+ end
53
+ end while (new_string.size > 0 && @stop == false)
54
+ sleep(0.5)
47
55
  end
48
56
  }
49
57
  end
@@ -56,4 +64,4 @@ module RB232
56
64
 
57
65
  end
58
66
 
59
- end
67
+ end
data/src/port.c CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include <fcntl.h>
5
5
  #include <termios.h>
6
+ #include <unistd.h>
6
7
 
7
8
  /* Module and class handles */
8
9
  VALUE RB232 = Qnil;
@@ -40,6 +41,8 @@ VALUE rb232_port_data_free(void* p) {
40
41
  /* Free memory */
41
42
  free(port_data->port_name);
42
43
  free(port_data);
44
+ /* Done */
45
+ return Qnil;
43
46
  }
44
47
 
45
48
  /*
@@ -67,7 +70,7 @@ VALUE rb232_port_initialize_with_options(VALUE self, VALUE port, VALUE options)
67
70
  port_data->parity = rbx_bool_from_hash_or_default(options, ID2SYM(rb_intern("parity")), FALSE);
68
71
  port_data->stop_bits = rbx_int_from_hash_or_default(options, ID2SYM(rb_intern("stop_bits")), 1);
69
72
  /* Open the serial port */
70
- port_data->port_handle = open(port_data->port_name, O_RDWR | O_NOCTTY);
73
+ port_data->port_handle = open(port_data->port_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
71
74
  if (port_data->port_handle < 0) {
72
75
  rb_raise(rb_eArgError, "couldn't open the specified port");
73
76
  }
@@ -251,8 +254,10 @@ int rb232_port_read(VALUE self, char* buffer, VALUE count) {
251
254
  }
252
255
 
253
256
  /*
254
- * Read _count_ raw byte values from the port.
255
- * Returns an array of values. Useful for binary protocols.
257
+ * Read up to _count_ raw byte values from the port.
258
+ * Returns an array of values. If no data is currently available, a zero-length
259
+ * array will be returned.
260
+ * Useful for binary protocols.
256
261
  * call-seq:
257
262
  * read_bytes(count)
258
263
  *
@@ -265,11 +270,14 @@ VALUE rb232_port_read_bytes(VALUE self, VALUE count) {
265
270
  for (i=0; i<bytes_read; i++) {
266
271
  rb_ary_push(data, INT2NUM(buffer[i]));
267
272
  }
273
+ return data;
268
274
  }
269
275
 
270
276
  /*
271
- * Read _count_ characters from the port.
272
- * Returns a string. Useful for text-based protocols.
277
+ * Read up to _count_ characters from the port.
278
+ * Returns a string. If no data is currently available, an empty string will be
279
+ * returned.
280
+ * Useful for text-based protocols.
273
281
  * call-seq:
274
282
  * read_string(count)
275
283
  *
@@ -277,6 +285,7 @@ VALUE rb232_port_read_bytes(VALUE self, VALUE count) {
277
285
  VALUE rb232_port_read_string(VALUE self, VALUE count) {
278
286
  char buffer[256];
279
287
  int bytes_read = rb232_port_read(self, buffer, count);
288
+ if (bytes_read < 1) bytes_read = 0;
280
289
  buffer[bytes_read] = 0;
281
290
  return rb_str_new2(buffer);
282
291
  }
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.2
4
+ version: 0.2.3
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-21 00:00:00 -07:00
12
+ date: 2008-08-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15