ea-ps_2084-03b/mitm.rb

34 lines
1012 B
Ruby
Executable File

#!/usr/bin/env ruby
# encoding: utf-8
# ruby: 1.9
require 'serialport'
require './telegram'
pc_serial = SerialPort.open("/dev/ttyUSB0",{ baud: 57600, databits: 8, parity: SerialPort::ODD, stop_bit: 1, flow_control: SerialPort::NONE})
ps_serial = SerialPort.open("/dev/ttyACM0",{ baud: 115200, databits: 8, parity: SerialPort::ODD, stop_bit: 1, flow_control: SerialPort::NONE})
while true
activities = IO.select([pc_serial,ps_serial])
activities[0].each do |activity|
data = activity.readpartial(16+1+5)
line = data.unpack("C*").collect { |b| sprintf("%02X ",b) }.join
begin
telegram = Telegram.parse(data)
rescue Exception => e
puts e.to_s
end
if activity==pc_serial then
puts "pc->ps: "+line
puts "pc->ps: "+telegram.to_s if telegram
ps_serial.write(data)
elsif activity==ps_serial then
puts "ps->pc: "+line
puts "ps->pc: "+telegram.to_s if telegram
pc_serial.write(data)
else
raise "unknown source"
end
end
end