Ruby - 代码在 Mac 和 Ubuntu 上执行方式不同
在 Mac 和 Ubuntu 安装上执行以下代码时,出现了一些奇怪的功能。
我的代码连接到我的 Arduino,然后检查客户端是否已连接(连接成功),然后我使用 Ruby 代码通过 t.puts
使用 < code>socket gem,然后使用 Arduino 软件输出我发送到串行输出的任何内容。这是最简单的部分。
当在 Mac 上执行以下命令时,我的串行输出以下内容,这是正确的:
{power, tv} # t.puts "{power, tv}"
但是,当在 Ubuntu 上执行相同的代码时,我在串行输出中得到以下内容,就好像它正在尝试再次连接一样。它不会提供 Mac 上的上述串行输出:
Connecting... # t.puts "{power, tv}" # Connecting... (text is actually coming from Arduino not the below code).
我已经仔细检查了我的 Arduino、IP、代码和端口是否正确(这就是它所连接的端口)。我只是不明白 Mac 和 Ubuntu 上输出不同的原因。
发生这种情况的任何原因以及是否可以修改以下代码以使其在所有环境下正确发送?
#!/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
#}
请记住,我可以很好地连接到 10.1.1.45(我可以 PING 等),因此据我所知,实际的 Arduino 代码在这里没有错误,因为我正在通过将其插入 Mac 和 Ubuntu 来测试它并执行上面完全相同的代码。请随时向我询问任何其他问题,我很乐意尽可能详细地回答这些问题,以帮助我前进。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ruby 中,puts 只是 IOStream 对象上的一个方法,并且该对象可以在您不知情的情况下进行更改。
您可能想尝试一下,看看它是否可以解决您的问题:
有可能在您的新套接字上调用
puts
。In ruby
puts
is 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
puts
was being called on your new socket.