Created
March 3, 2015 13:03
-
-
Save halfd/6985350fe0721e6d42ef to your computer and use it in GitHub Desktop.
Python Serial connection to 3D printer
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
from serial import Serial | |
import time | |
class HDTerm(Serial): | |
def __init__(self, *args, **kwargs): | |
#ensure that a reasonable timeout is set | |
timeout = kwargs.get('timeout',0.1) | |
if timeout < 0.01: timeout = 0.1 | |
kwargs['baudrate'] = 115200 | |
kwargs['port'] = '/dev/ttyUSB0' | |
kwargs['parity'] = Serial.PARITY_ODD | |
#stopbits=serial.STOPBITS_ONE, | |
#bytesize=serial.EIGHTBITS | |
kwargs['timeout'] = timeout | |
Serial.__init__(self, *args, **kwargs) | |
self.buf = '' | |
if self.isOpen(): | |
self.close() | |
self.open() | |
self.isOpen() | |
def rd(self): | |
time.sleep(1) | |
out = '' | |
while self.inWaiting() > 0: | |
out += ser.read(40) | |
print(' <- ', (out)) | |
def wt(self, data): | |
ed = data.encode() | |
print(' -> ', (ed)) | |
self.write(ed) | |
def inp(self): | |
st = input('> ') | |
self.wt(st) | |
self.rd() | |
self.inp() | |
if __name__ == '__main__': | |
hdterm = HDTerm() | |
hdterm.inp() |
by executing the code in a shell 'python3 pyse.py' after that, if there is no connection error, use the stdin for input
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I like your code. But how can I use it? Thank you in advance