问题
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.
回答1:
According to http://www.rubydoc.info/gems/serialport/SerialPort:flow_control=
Note: SerialPort::HARD uses RTS/CTS handshaking. DSR/DTR is not supported.
回答2:
I'm answering my own question. It turns out that Apple's supplied FTDI drivers are buggy. They don't give any access to the control signals.
I installed the official FTDI VCP drivers (version 2.3 at this time of writing), rebooted, and now I have full control.
来源:https://stackoverflow.com/questions/28173636/hardware-flow-control-bits-not-working-for-ruby-serialport-gem