Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

serial port - Hardware flow control bits not working for ruby-serialport gem

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to http://www.rubydoc.info/gems/serialport/SerialPort:flow_control=

Note: SerialPort::HARD uses RTS/CTS handshaking. DSR/DTR is not supported.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...