Skip to content

Instantly share code, notes, and snippets.

@andresv
Created August 8, 2012 08:37
Show Gist options
  • Save andresv/3293455 to your computer and use it in GitHub Desktop.
Save andresv/3293455 to your computer and use it in GitHub Desktop.
LED display test
import serial
import struct
import time
s = serial.Serial('/dev/tty.KeySerial1', 9600, stopbits=serial.STOPBITS_TWO, timeout=1.)
class Display(object):
def __init__(self, serial, receiver='00'):
self.s = serial
self.sender_addr = 'FF'
self.receiver_addr = receiver
#def wipe(self):
# pass
def write(self, command='A', settings='AA227F000024000001', text='test'):
preamble = [0x00, 0x00, 0x00, 0x00, 0x00]
soh = 0x01
sender = self.sender_addr
receiver = self.receiver_addr
stx = 0x02
cmd = command
settings = settings
data = text
etx = 0x03
checksum = self._checksum(chr(stx) + cmd + settings + data + chr(etx))
eot = 0x04
#message = struct.pack('%ssB2s2sBs%ss%ssB4sB' %
# (len(preamble), len(data), len(settings)),
# ''.join(map(chr, preamble)), soh, sender, receiver, stx, cmd, settings, data, etx, checksum, eot)
data = '000000'.decode('hex')
message = struct.pack('%ss' % (len(data)), data)
s.write(message)
#time.sleep(0.1)
#s.write(chr(eot))
rx = s.read(2)
print 'from display: ', rx.encode('hex')
def _checksum(self, data):
"""
return checksum
data contains: etx, cmd, data, etx
"""
print data.encode('hex')
checksum = hex(sum([ord(c) for c in data])).upper()
checksum = checksum[2:]
if len(checksum) == 3:
checksum = '0' + checksum
elif len(checksum) == 2:
checksum = '00' + checksum
elif len(checksum) == 1:
checksum = '000' + checksum
print 'checksum: ', checksum
return checksum
d = Display(s, receiver='00')
#d.write(command='A', settings='AA427F000023590001', text='hello')
d.write(command='A', settings='AA227F000024000001', text='HELLO')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment