#1/usr/bin/env python # coding: utf-8 # copyright Michael Weber avr at xmw.de 2008 # released with GNU GPL import os, tty, select class serial: def __init__(self, linkname, linkspeed): self.linkname = linkname self.linkspeed = linkspeed self.timeout = 1 self.link = os.open(linkname, os.O_RDWR) tty.setraw(self.link) mode = tty.tcgetattr(self.link) mode[tty.ISPEED] = mode[tty.OSPEED] = linkspeed tty.tcsetattr(self.link, tty.TCSAFLUSH, mode) def read(self, n, timeout=None): if not timeout: timeout = self.timeout i = 0 data = [] while len(data) < n: iset, oset, eset = select.select([self.link], [], [], timeout) if iset == []: return data data.append(os.read(self.link, 1)) return data def write(self, data): os.write(self.link, data) def __del__(self): os.close(self.link)