I came across what seems like a very strange problem. I greatly simplified my sketch and Ruby code to isolate the problem.
Ruby version 2.0.0 and ruby-serialport version 2.3.0. I am on Mac OS X 10.9 (Mavericks).
Here is my sketch. As you can see, it just lights up contact 13 for 500 ms if it receives ANY serial data.
serialtest.ino
void setup(){
Serial.begin(115200);
pinMode(13, OUTPUT);
}
void loop(){
while(Serial.available()){
byte x = Serial.read();
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
}
And here is my Ruby code.
serialtest.rb
require 'serialport'
sp = SerialPort.new('/dev/tty.usbmodem411', 115200)
sp.write "a"
sp.flush
This code, if executed by itself, DOES NOT work. There are no warnings and warnings, but the LED on pin 13 does not light up (even if the RX indicator on the Arduino flashes briefly). However, it works serialtest.rbunder the following conditions:
IRB SerialPort.new('/dev/tty.usbmodem411', 115200) , IRB. , IRB, serialtest.rb 13.
Arduino . , 13 serialtest.rb.
IRB. . , 13 ( serialtest.rb).
. , , ( , sp.flush . . sp.sync = true , ). sp.syswrite, . , , , .
, serialport ( SerialPort.new(...) Thread.new ).
.
: , , Arduino reset , Ruby ( IRB , SerialPort.new serialtest.rb ). 10 reset GND Arduino.