Skip to content

Instantly share code, notes, and snippets.

@c2h2
Created October 11, 2011 10:01
Show Gist options
  • Save c2h2/1277742 to your computer and use it in GitHub Desktop.
Save c2h2/1277742 to your computer and use it in GitHub Desktop.
a working serial program.
#!/usr/bin/ruby
#encoding: utf-8
require "serialport"
ENABLE_RS485_RELAY = true
DEV=0
BAUD=57600
MSG_SLEEP_TIME = 0.1
trap("INT"){exit 0}
trap("TERM"){exit 0}
class String
def to_bin
[self].pack("H*")
end
def to_hex
str=""
self.each_byte{|b| str << b.to_s(16).add_zero }
str
end
def add_zero
self.length == 1 ? "0" + self : self
end
end
def rs485_send(msg, baud, dev, parmas={})
#sp = SerialPort.open(dev, baud, 8, 1, SerialPort::MARK)
sp = SerialPort.open(dev, baud, 8, 1, SerialPort::MARK)
sp.write "01fdfd".to_bin
sp.close
sleep MSG_SLEEP_TIME
#sp = SerialPort.open(dev, baud, 8, 1, SerialPort::SPACE)
sp = SerialPort.open(dev, baud, 8, 1, SerialPort::EVEN)
sp.write msg.to_bin
puts msg.length
sp.close
end
rs485_data = "01fd000002a5010002007f1110091210090000000023595901000000000000140010080801010112010202ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbffffffffffffffffffffffffffffffffffffffffdffffffffffffffffffffffffffffffffffffffbfffffffe7ffe7ffffffffffffffffffffffe7fffdfff7fff7ffffffffffffffffffffffffffffffbffffffff7fff7fffffffffffffffffffffff7fffdfff7fff7ffffffffffffffffffffffffffffffa3ffc3fff7fff7ffc3ffbeffffffc3ffa3fff7ffc5fff7fff7ffffffffffffffffffffffffffffff9dffbdfff7fff7ffbdffb6ffffffbdff9dfff7ffb9fff7fff7ffffffffffffffffffffffffffffffbdffbdfff7fff7ffbdffb6ffffffbdffbdfff7ffbdfff7fff7ffffffffffffffffffffffffffffffbdff81fff7fff7ffbdffb6ffffffbdffbffff7ffbdfff7fff7ffffffffffffffffffffffffffffffbdffbffff7fff7ffbdffb6ffffffbdffbffff7ffbdfff7fff7ffffffffffffffffffffffffffffffbdffbffff7fff7ffbdffb6ffffffbdffbffff7ffbdffffffffffffffffffffffffffffffffffffffbdffbdfff7fff7ffbdffb6ffffffbdffbffff7ffb9fff7fff7ffffffffffffffffffffffffffffffbdffc3ffc1ffc1ffc3ffc9ffffffc3ffbfffc1ffc5fff7fff7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00026d60aa"
puts rs485_data
rs485_send(rs485_data, BAUD, "/dev/ttyS0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment