Write byte to Arduino from Ruby over serial

Hey guys,

Im trying to send an integer over the serial port to my Ardunio, the chip is then going to display the number in binary on the LED's, however im having lots of trouble trying to send the data as a byte over the serial port, as far as I can debug the following code sends it as the ASC char values.

Can anyone point me in the right direction or spot the mistake? Id really appreciate it Ive been pulling my hair out over this for a long time.

Thanks

Ruby

require 'rubygems'  
require 'serialport' # use Kernel::require on windows, works better.  

#params for serial port  
port_str = "/dev/tty.usbserial-A700dZt3"  #may be different for you  
baud_rate = 9600  
data_bits = 8  
stop_bits = 1  
parity = SerialPort::NONE  

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)  

#just write forever  
while true do  
  sp.write 11.to_s(2)
  sleep 10
end

Arduino

int ledPin = 10;
int ledPin1 = 11;
int ledPin2 = 12;
int ledPin3 = 13;

byte incomingByte;  // for incoming serial data

void setup() {
  pinMode(ledPin, OUTPUT);  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT); // initialize the LED pin as an output:
  pinMode(ledPin2, OUTPUT); // initialize the LED pin as an output:
  pinMode(ledPin3, OUTPUT); // initialize the LED pin as an output:
  Serial.begin(9600);   
  Serial.println("I am online"); 
}

void loop() {
 // send data only when you receive data:
if (Serial.available() > 0) {
    incomingByte = Serial.read();
            Serial.println(incomingByte, DEC);

   int value = (incomingByte, DEC) % 16;
   digitalWrite(ledPin, (value >> 0) % 2);
   digitalWrite(ledPin1, (value >> 1) % 2);
   digitalWrite(ledPin2, (value >> 2) % 2);
   digitalWrite(ledPin3, (value >> 3) % 2); // MSB

}

}

Thanks :)

Can you explain what the statement is doing?

  sp.write 11.to_s(2)

To a non-Ruby cognizant person, this looks like you are doing an explicit conversion to string of the value that you want to write.

Hi PaulS thanks for the reply, I assumed that my Ardunio code was functioning correctly and thought maybe I should ask on a Ruby forum about how I should get it working.

Well, 9.to_s(2) is not a binary representation of 9. It is a String representation. You'd have to use something like "\x9" or "\x0009" or [9].pack('c'). There's a difference between visually looking like the binary representation and being the binary

Anyway, I found this article and am now using PHP to get the job done. http://www.mydarkmaterials.co.uk/2008/11/30/interfacing-php-with-the-arduino/

However if anyone does know how to send a byte from Ruby on the serial port id very much like to know how. :)

Now that ive got it working I just need to scrape information from the local station and hopefully I wont miss my morning train again :P

However if anyone does know how to send a byte from Ruby on the serial port id very much like to know how.

Not being familiar with Ruby, the obvious thing that comes to mind is to simply drop the .to_s() call:

sp.write 11

That will write is as the char value for 1 and 1, I can go down that road but I have to add logic to the Arduino to deal with the values being passed in one at a time.

I could also directly query the terminal from Ruby but I cant find any examples of sending a byte through the terminal on OS X.