#!/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}) # 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| data = activity.readpartial(16+1+5) line = data.unpack("C*").collect { |b| sprintf("%02X ",b) }.join if activity==pc_serial then puts "> "+line begin telegram = Telegram.parse(data) if telegram then puts "> "+telegram.to_s[2..-1] else raise "could not parse message" end rescue Exception => e puts e.to_s end else raise "unknown source" end end end