Skip to content

Instantly share code, notes, and snippets.

@kaveenr
Created May 13, 2014 03:44
Show Gist options
  • Save kaveenr/96ffe2d498da9b08cb48 to your computer and use it in GitHub Desktop.
Save kaveenr/96ffe2d498da9b08cb48 to your computer and use it in GitHub Desktop.
PySerial Helper Class
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# pysbase.py
#
# Copyright 2013 Unknown <kaveenr@kaveenlabs>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import serial
import thread
import time
class EzSerial:
def __init__(self,port,baud):
self.port=serial.Serial(port,baud)
thread.start_new_thread( self.Scanner, (True,True) )
def Scanner(self,a,b):
while a:
data=self.port.readline()
if data=="":
continue
else:
self.processor(data)
def processor(self,data):
print str.strip(data)
time.sleep(1)
self.Writeln("#")
def Writeln(self,command):
self.port.writelines(command)
def close(self):
self.port.close()
#---------------
test=EzSerial("COM3",9600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment