add serial baudrate adaptation

This commit is contained in:
King Kévin 2015-08-24 16:12:30 +02:00
parent 9e22fa618f
commit dacfbcef5d
1 changed files with 15 additions and 5 deletions

20
mitm.rb
View File

@ -8,23 +8,33 @@ pc_serial = SerialPort.open("/dev/ttyUSB0",{ baud: 57600, databits: 8, parity: S
ps_serial = SerialPort.open("/dev/ttyACM0",{ baud: 115200, databits: 8, parity: SerialPort::ODD, stop_bit: 1, flow_control: SerialPort::NONE})
# ensure serial settings (weird bug)
pc_serial.baud = 57600
pc_serial.parity = SerialPort::ODD
# the different baudrates used
baudrates = []
baudrates << {baud: 57600, parity: SerialPort::ODD}
baudrates << {baud: 115200, parity: SerialPort::ODD}
baudrate_i = 0
while true
activities = IO.select([pc_serial,ps_serial])
activities[0].each do |activity|
data = activity.readpartial(16+1+5)
data = activity.readpartial(1024)
line = data.unpack("C*").collect { |b| sprintf("%02X ",b) }.join
if activity==pc_serial then
puts "pc->ps: "+line
begin
telegram = Telegram.parse(data)
puts "pc->ps: "+telegram.to_s if telegram
if telegram then
puts "pc->ps: "+telegram.to_s if telegram
ps_serial.write(data)
else
baudrate_i = (baudrate_i+1)%baudrates.size
pc_serial.baud = baudrates[baudrate_i][:baud]
pc_serial.parity = baudrates[baudrate_i][:parity]
puts "could not parse message: switching baudrate to #{pc_serial.baud} bps"
end
rescue Exception => e
puts e.to_s
end
ps_serial.write(data)
elsif activity==ps_serial then
puts "ps->pc: "+line
begin