add autoswitching baudrate (used by EasyPS2000)
This commit is contained in:
parent
73b6dc6c53
commit
91b317ca79
|
@ -9,10 +9,6 @@ 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})
|
||||
|
||||
# ensure serial settings (weird bug)
|
||||
pc_serial.baud = 57600
|
||||
pc_serial.parity = SerialPort::ODD
|
||||
|
||||
while true
|
||||
activities = IO.select([pc_serial])
|
||||
activities[0].each do |activity|
|
||||
|
@ -24,26 +20,29 @@ while true
|
|||
telegram_in = Telegram.parse(data)
|
||||
if telegram_in then
|
||||
puts "> "+telegram_in.to_s[2..-1]
|
||||
telegram_out = case telegram_in.object
|
||||
when 0 # device type
|
||||
Telegram.new(telegram_in.object,"simulator".bytes.to_a+[0x00],false)
|
||||
when 8 # manufacturer
|
||||
Telegram.new(telegram_in.object,"EA".bytes.to_a+[0x00],false)
|
||||
when 1, 6, 9 # article no., serial no., software version
|
||||
Telegram.new(telegram_in.object,"42".bytes.to_a+[0x00],false)
|
||||
when 2,3,4 # nominal voltage, current, power
|
||||
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)
|
||||
else # error: object not defined
|
||||
Telegram.new(0xff,[0x07],false)
|
||||
end
|
||||
if telegram_out then
|
||||
puts "< "+telegram_out.to_s[2..-1] # display message
|
||||
pc_serial.write(telegram_out.pack) # send message
|
||||
end
|
||||
else
|
||||
raise "could not parse message"
|
||||
pc_serial.baud = (pc_serial.baud==57600 ? 115200 : 57600)
|
||||
pc_serial.parity = SerialPort::ODD
|
||||
puts "could not parse message: switching baudrate to #{pc_serial.baud} bps"
|
||||
end
|
||||
telegram_out = case telegram_in.object
|
||||
when 0 # device type
|
||||
# Telegram.new(telegram_in.object,[0x50,0x53,0x20,0x32,0x30,0x38,0x34,0x2D,0x30,0x33,0x42,0x00])
|
||||
Telegram.new(telegram_in.object,"simulator".bytes.to_a+[0x00],false)
|
||||
when 1, 6, 9 # article no., serial no., software version
|
||||
Telegram.new(telegram_in.object,"42".bytes.to_a+[0x00],false)
|
||||
when 2,3,4 # nominal voltage, current, power
|
||||
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)
|
||||
else # error: object not defined
|
||||
Telegram.new(0xff,[0x07],false)
|
||||
end
|
||||
if telegram_out then
|
||||
puts "< "+telegram_out.to_s[2..-1] # display message
|
||||
pc_serial.write(telegram_out.pack) # send message
|
||||
end
|
||||
rescue Exception => e
|
||||
puts e.to_s
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue