I have a strange bit of functionality going on when executing the following code on a Mac and on an Ubuntu installation.
My code makes a connection to my Arduino, which then checks for whether the client is connected (it connects successfully) and I then use Ruby code to send a command to my Arduino via t.puts using the socket gem, which then outputs whatever I send it to the serial output using the Arduino software. That’s the easy part.
When the following is executed on a Mac my serial outputs the following, which is correct:
{power, tv} # t.puts "{power, tv}"
However, when the same code is executed on Ubuntu I get the following in the serial output, as if it’s trying to connect again. It doesn’t give the above serial output that it does on the Mac:
Connecting... # t.puts "{power, tv}" # Connecting... (text is actually coming from Arduino not the below code).
I have double checked that my Arduino, IP, code and port are correct (which is what it’s connecting to). I just don’t see the reason why the output would be different on Mac and Ubuntu.
Any reasons why this would be happening and whether the following code can be modified in such a way that it’s sending it properly on all environments?
#!/usr/bin/ruby
require "socket"
#Thread.new {
begin
puts "Connecting to 10.1.1.45..."
t = TCPSocket.new("10.1.1.45", 80)
rescue
puts "error : #{$!}"
else
t.print "{power,tv}"
t.close
puts "Sent command..."
end
#}
Keep in mind I can connect to 10.1.1.45 just fine (I can PING, etc) so the actual Arduino code is not at fault here as far as I know, because I’m testing this by plugging it into the Mac and the Ubuntu and executing the exact same code above. Feel free to ask me any further questions and I’ll be happy to answer them in as much detail as possible to help me on my way.
Thanks in advance.
In ruby
putsis just a method on an IOStream object and the object can be changed without you knowing it.You might want to try this, to see if it solves your problem:
It’s possible that
putswas being called on your new socket.