我有一个连接到串行端口的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"]