Skip to content

Instantly share code, notes, and snippets.

@olleolleolle
Forked from qzio/serial_writer.py
Created March 23, 2010 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olleolleolle/341617 to your computer and use it in GitHub Desktop.
Save olleolleolle/341617 to your computer and use it in GitHub Desktop.
Message writer for a... cool thing
#!/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