I am trying to use the control bits of a USB serial port adapter as general purpose I/O. This simple example should toggle the DTR line high, then low.
require 'serialport'
DataBits = 8
StopBits = 1
Parity = SerialPort::NONE
Baud = 38400
port = '/dev/tty.usbserial-A100KXWU'
serial = SerialPort.new(port, 'baud' => Baud, 'data_bits' => DataBits, 'stop_bits' => StopBits, 'parity' => Parity)
serial.flow_control = SerialPort::HARD
loop do
p serial.signals
sleep(1)
serial.dtr = (serial.dtr + 1) % 2
end
And the output:
{"rts"=>1, "dtr"=>1, "cts"=>1, "dsr"=>0, "dcd"=>0, "ri"=>0}
{"rts"=>1, "dtr"=>0, "cts"=>1, "dsr"=>0, "dcd"=>0, "ri"=>0}
{"rts"=>1, "dtr"=>1, "cts"=>1, "dsr"=>0, "dcd"=>0, "ri"=>0}
{"rts"=>1, "dtr"=>0, "cts"=>1, "dsr"=>0, "dcd"=>0, "ri"=>0}
As far as Ruby is concerned, serial.dtr
is changing, but there is no change on the output voltage of the DTR pin. It's a constant +7V.
Additionally, the serial
instance is unable to read any changes applied to CTS, DSR, or DCD coming from other hardware devices.
It is being run with sudo
privileges, so it's not a issue with permissions. This is on OS X 10.10 Yosemite.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…