Skip to content

Instantly share code, notes, and snippets.

@halfd
Created March 3, 2015 13:03
Show Gist options
  • Save halfd/6985350fe0721e6d42ef to your computer and use it in GitHub Desktop.
Save halfd/6985350fe0721e6d42ef to your computer and use it in GitHub Desktop.
Python Serial connection to 3D printer
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()
@simcup
Copy link

simcup commented Mar 8, 2022

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