Go Down

Topic: Write byte to Arduino from Ruby over serial (Read 2859 times) previous topic - next topic

JUo

Nov 06, 2010, 06:43 pm Last Edit: Nov 06, 2010, 06:53 pm by JUo Reason: 1
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
Code: [Select]
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
Code: [Select]
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  :)

PaulS

Can you explain what the statement is doing?
Code: [Select]
 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.
The art of getting good answers lies in asking good questions.

JUo

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.

Quote
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

PaulS

Quote
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:
Code: [Select]
sp.write 11
The art of getting good answers lies in asking good questions.

JUo

#4
Nov 07, 2010, 05:00 pm Last Edit: Nov 07, 2010, 05:03 pm by JUo Reason: 1
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.

Go Up