Created
May 30, 2017 21:25
-
-
Save peisenhower/7d2eecddf3e72cd0154c0554c2762c59 to your computer and use it in GitHub Desktop.
motor commands over serial
This file contains 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 | |
# python 3 | |
from serial import Serial | |
import time | |
import argparse | |
from threading import Thread | |
class Command(): | |
"""docstring for Command""" | |
def __init__(self, delay, cmd): | |
self.cmd = cmd | |
self.delay = delay | |
def has_callback(self): | |
if b'setpoint' in self.cmd: | |
return True | |
else: | |
return False | |
command_set = [ | |
Command(3.0, b'filter set h\r\n'), | |
Command(3.0, b'filter set a\r\n'), | |
Command(3.0, b'filter set a\r\n'), | |
] | |
def readline(com): | |
eol = b'\r\n' | |
leneol = len(eol) | |
line = bytearray() | |
while True: | |
c = com.read(1) | |
if c: | |
line += c | |
if line[-leneol:] == eol: | |
break | |
else: | |
break | |
if len(line) > 0: | |
print(line.decode()) | |
return bytes(line) | |
def drainInput(com): | |
length = len(readline(com)) | |
while length > 0: | |
length = len(readline(com)) | |
def runner(): | |
com = Serial() | |
com.port = 'COM7' | |
com.baudrate = 115200 | |
com.timeout = 0 | |
com.open() | |
count = 0 | |
while count < 1000: | |
print('\r\nLoop {}'.format(count)) | |
count += 1 | |
for c in command_set: | |
com.write(c.cmd) | |
print(c.cmd) | |
drainInput(com) | |
time.sleep(c.delay) | |
com.close() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='loop serial commands') | |
runner() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment