From 5a810cfd4a6708a79270efff4d7221ed06d6725b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 24 Aug 2015 16:15:39 +0200 Subject: [PATCH] add baudrate adaptation --- ps_simulator.rb | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/ps_simulator.rb b/ps_simulator.rb index cd4e306..cd57646 100755 --- a/ps_simulator.rb +++ b/ps_simulator.rb @@ -9,6 +9,18 @@ DEBUG = true # connect to PC pc_serial = SerialPort.open("/dev/ttyUSB0",{ baud: 57600, databits: 8, parity: SerialPort::ODD, stop_bit: 1, flow_control: SerialPort::NONE}) +# the different baudrates used +baudrates = [] +baudrates << {baud: 57600, parity: SerialPort::ODD} +baudrates << {baud: 115200, parity: SerialPort::ODD} +# only used when everything failed +# but not fast enough for readpartial +#baudrates << {baud: 9600, parity: SerialPort::NONE} +baudrate_i = 0 + +# temporary memeory, in case the PC writes and askes back +tmp_memory = [0x00]*8 +file = nil while true activities = IO.select([pc_serial]) activities[0].each do |activity| @@ -31,8 +43,22 @@ while true Telegram.new(telegram_in.object,[42.0].pack("g").bytes.to_a,false) when 19 # device class Telegram.new(telegram_in.object,[0x00,0x10],false) - when 54,150 # power supply control, unlock code - Telegram.new(0xff,[0x00],false) + when 54,150,151 # power supply control, unlock code 1, unlock code 2 + Telegram.new(0xff,[0x00],false) # no error + when 152 # write memory + if telegram_in.data.empty? then + Telegram.new(telegram_in.object,tmp_memory,false) + else + tmp_memory = telegram_in.data + if telegram_in.data[0]==0x33 then + file.close if file and !file.closed? + file = File.open(telegram_in.data[1].to_s,"w") + elsif telegram_in.data[0]==0x34 then + else + file.write(telegram_in.data[1..-1].pack("C*")) if file + end + Telegram.new(0xff,[0x00],false) # no error + end else # error: object not defined Telegram.new(0xff,[0x07],false) end @@ -41,8 +67,9 @@ while true pc_serial.write(telegram_out.pack) # send message end else - pc_serial.baud = (pc_serial.baud==57600 ? 115200 : 57600) - pc_serial.parity = SerialPort::ODD + 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