-
-
Save olleolleolle/341617 to your computer and use it in GitHub Desktop.
Message writer for a... cool thing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import serial | |
import struct | |
from optparse import OptionParser | |
parser = OptionParser(usage="hurra") | |
parser.add_option("-p",default="hello world") | |
options,args = parser.parse_args() | |
def mk_msg(txt): | |
s = struct.pack("BBB",255,6,162) | |
crc = 6+162 | |
for t in txt: | |
s += t | |
#s += struct.pack("B",ord(t)) | |
crc += ord(t) | |
#s += struct.pack("B",crc % 256) | |
s += chr(crc % 256) | |
s += "\xff" | |
return s | |
try: | |
ser = serial.Serial("/dev/ttyUSB0", 4800) | |
print "serial device initiated" | |
print ser | |
except Exception, e: | |
ser = None | |
write_string = mk_msg(options.p) | |
if ser and ser.isOpen(): | |
print "will write %r" % write_string | |
ser.write(write_string) | |
else: | |
print "if the serial port would have been open..." | |
print write_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment