Last active
January 17, 2016 23:10
-
-
Save cho45/4326cf94e22f8e921d0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "serialport" | |
SerialPort.class_eval do | |
TCGETS2 = 0x802c542a | |
TCSETS2 = 0x402c542b | |
CBAUD = 0x100f | |
BOTHER = 0x1000 | |
# struct termios2 { | |
Termios2 = Struct.new(*[ | |
:c_iflag, | |
:c_oflag, | |
:c_cflag, | |
:c_lflag, | |
:c_line, | |
(0...19).map {|n| # c_cc[NCCS] NCCS = 19 | |
"c_cc_#{n}".to_sym | |
}, | |
:c_ispeed, | |
:c_ospeed | |
].flatten); | |
Termios2::FORMAT = "I!I!I!I!CC19I!I!" | |
Termios2::FORMAT_POINTER = "P44" | |
# } | |
def set_custom_baudrate(baud) | |
tio = Termios2.new | |
tio.each_pair {|m,_| tio[m] = 0 } | |
# read | |
v = tio.values.flatten.pack(Termios2::FORMAT) | |
self.ioctl(TCGETS2, v) | |
tio = Termios2.new(*v.unpack(Termios2::FORMAT)) | |
# write | |
tio.c_cflag &= ~CBAUD | |
tio.c_cflag |= BOTHER | |
tio.c_ispeed = baud | |
tio.c_ospeed = baud | |
v = tio.values.flatten.pack(Termios2::FORMAT) | |
self.ioctl(TCSETS2, v) | |
# read | |
v = tio.values.flatten.pack(Termios2::FORMAT) | |
self.ioctl(TCGETS2, v) | |
tio = Termios2.new(*v.unpack(Termios2::FORMAT)) | |
if tio.c_ispeed == baud && tio.c_ospeed == baud | |
true | |
else | |
raise "failed to set baudrate expected:#{baud} but set:#{tio.c_ispeed}/#{tio.c_ospeed}" | |
end | |
end | |
end | |
@port = SerialPort.new( | |
"/dev/ttyUSB0", | |
230400, | |
8, | |
1, | |
0 | |
) | |
@port.set_encoding(Encoding::BINARY) | |
@port.set_custom_baudrate(250000) | |
sleep 0.1 | |
# @port << "\xA5" | |
loop do | |
data = @port.sysread(4096) | |
p [data, data.size] | |
@port << "\x5A" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment