Parse Texecom ответ на тревогу - PullRequest
0 голосов
/ 27 мая 2019

У меня есть сигнал тревоги Texecom, подключенный к последовательному порту, и я пытаюсь прочитать страницу состояния зон 2 данного руководства. (https://drive.google.com/file/d/1Z3caY_y9wKAch7-tiZgAElWBY8uiZRxr/view?usp=sharing) Вы также можете найти там описание ответа на запрос. Я получаю некоторый двоичный ответ, но не могу разобрать его.

Не могли бы вы помочь мне разобрать это правильно? Вот что я уже пробовал:

require 'bundler/setup'
require 'serialport'
require 'active_support/all'
require 'json'
​
$LOAD_PATH << './lib'
STDOUT.sync = true
​
class Texecom
​
  BUTTONS = {
    '1' => 0x01, '2' => 0x02,
    '3' => 0x03, '4' => 0x04,
    '5' => 0x05, '6' => 0x06,
    '7' => 0x07, '8' => 0x08,
    '9' => 0x09, '0' => 0x0A,
    'Omit' => 0x0B, 'Menu' => 0x0C,
    'Yes' => 0x0D, 'No' => 0x0F,
    'Part' => 0x0E, 'Area' => 0x10,
    'Fire' => 0x11, 'PA' => 0x12,
    'Medical' => 0x13, 'Chime' => 0x14,
    'Reset' => 0x15, 'Up' => 0x16,
    'Down' => 0x17,
  }
​
  def initialize(tty="/dev/ttyUSB0", user_code = 1234)
    @user_code = user_code
    @tty_device_path = tty
  end
​
  def device
    @device ||= SerialPort.new @tty_device_path, 19200, 8, 2, SerialPort::NONE
    @device.flow_control = SerialPort::NONE
    @device
  end
​
  def read
    device.readline.chomp
  end
​
  def write(string)
    device.write("\\#{string}/")
  end
​
  def log_in
    write("W#{@user_code}")
    read.include?("OK")
  end
​
  def press_button(button)
    write("K#{BUTTONS[button]}")
    read.include?("OK")
  end
​
end
​
if __FILE__ == $0
  texecom = Texecom.new
  texecom.log_in
end
​
​
irb(main):037:0> texecom.write('Z01')
=> 5
irb(main):038:0> texecom.read
=> "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002\u0002\u0002\u0002\u0002\u0002\u0002\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
​
​
rb(main):087:0> ['5c5a08082f'].pack('H*')
=> "\\Z\b\b/"
​
irb(main):088:0> texecom.device.write(['5c5a08082f'].pack('H*'))
=> 5
irb(main):089:0> texecom.read
=> "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
irb(main):090:0> "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000".unpack('c*')
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
​
irb(main):099:0> texecom.device.write(['5c5a07082f'].pack('H*'))
=> 5
irb(main):100:0> texecom.read
=> "\u0012\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
irb(main):102:0> "\u0012\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000".unpack('c*')
=> [18, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
irb(main):103:0> File.binwrite('test.bin', "\u0012\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000")
irb(main):109:0> texecom.log_in
=> true
irb(main):110:0> texecom.device.write(['5c5a00082f'].pack('H*'))
=> 5
irb(main):111:0> texecom.read
=> "\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b"
irb(main):113:0> "\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b".unpack('c*')
=> [18, 8, 18, 8, 18, 8, 18, 8, 18, 8, 18, 8, 18, 8, 18, 8]
​
str = "\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b\u0012\b"
irb> str.each_byte.map{|c| "%X" % c}
=> ["12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8"]
irb> str.each_byte.map{|c| "%X" % c}.size
=> 16
irb> str.each_byte.map{|c| "%X" % c}
=> ["12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8"]
irb> str.each_codepoint.map{|c| "%X" % c}
=> ["12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8", "12", "8"]
irb> str.unpack('H*')
=> ["12081208120812081208120812081208"]
irb> str.valid_encoding?
=> true
irb> str.encode('ASCII')
=> "\x12\b\x12\b\x12\b\x12\b\x12\b\x12\b\x12\b\x12\b"
irb> str.unpack('H*')
=> ["12081208120812081208120812081208"]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...