Failed to execute Ruby script from file, but IRB working fine?

I have a tiny shell script that writes to a serial device:

#!/usr/bin/ruby

require 'rubygems'
require 'serialport'

@sp = SerialPort.new "/dev/tty.usbserial-A6004cNN", 19200
@sp.write ["\x01\x01\x04\x00", "n", "\xff\xff\xff"]

This is not written to the serial device when I run ./script.shin this directory. However, when I go to IRB and run:

require 'serialport' #=> true
@sp = SerialPort.new "/dev/tty.usbserial-A6004cNN", 19200 #=> #<SerialPort:0x1016bd698>
@sp.write ["\x01\x01\x04\x00", "n", "\xff\xff\xff"] #=> 8

This method works ... I'm puzzled. Could this be the way I am outputting my byte array? This does not seem to be correct, it’s just a ruby ​​ruby, it has no obvious dependencies. In addition, the script does not throw exceptions, it runs just fine, but the device just doesn't respond.

How can I debug this? I'm not sure where to start.

+1
source share
2 answers

SerialPort, , script, IRB , . @sp.flush , .

+4

, script, .rb ruby ​​script,

ruby <name_of_my_script>.rb

.

0

All Articles