This is the Python API for my arduino uno board. this is base on what i posted in Part I .. I didn't spend time to comments it... I'll do it once free soon. Please study it and extend it and share among open source members especially in python... :)
The file name is "cscarduino.py"
if you facing "Pyserial Invalid Handle" issue in your 64bit windows 7 server, Please refer to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #! /usr/bin/env python
#coding=utf-8
#Author: Cheeng Shu Chin
import serial, time
import traceback
import inspect
import threading
class autoprop(type):
def __init__(cls, name, bases, dict):
super(autoprop, cls).__init__(name, bases, dict)
def __new__(cls,name, bases, dict):
sf=super(autoprop, cls).__new__(cls,name, bases, dict)
for key in dict.keys():
if not (len(key)>5 and key.startswith("_")):
continue
nm=key[:5].lower()
if nm in ["_get_","_set_","_del_"]:
fget = getattr(sf, "_get_%s" % key[5:], None)
fset = getattr(sf, "_set_%s" % key[5:], None)
fdel = getattr(sf, "_del_%s" % key[5:], None)
setattr(sf,key[5:],property(fget, fset, fdel))
return sf
def __getattribute__(self,name):
return super(autoprop, self).__getattribute__(name)
class arduino(object):
__metaclass__ = autoprop
def __init__(self, port, baudrate=115200):
self._port=port
self._baudrate=baudrate
self._serial = None
self._debug=0
self._lock=threading.RLock()
self.nop
def _get_lock(self):
self._lock.acquire()
def _get_unlock(self):
self._lock.release()
def _set_debug(self,data):
self._debug=data
self('debug','%s'%data)
def _get_serial(self):
if self._serial==None:
self._serial = serial.Serial(self._port, self._baudrate)
return self._serial
def close(self):
self.nop
self.serial.close()
return True
def _get_nop(self):
self.write="nop"
self.read
def swrite(self,data):
self.lock
self.serial.write(data)
self.unlock
def sread(self):
self.lock
dt=self.serial.readline()
self.unlock
return dt
def _set_write(self,data):
dt=""
while not dt:
while not dt.endswith("?"):
dt+=self.read
if dt in ["!INT0 ?","!INT1 ?"]:
getattr(self,dt[1:5].lower())
dt=""
if self._debug:
print dt
self.swrite(data)
if self._debug:
print data
def _get_int0(self):
print "INT0",chr(7)
def _get_int1(self):
print "INT1",chr(7)
def _get_read(self):
return self.sread().strip()
def _get_frame(self):
frame=inspect.currentframe().f_back.f_back
return frame
def _get_farg(self):
args, varargs, varkw, largs=inspect.getargvalues(self.frame)
if args:
for arg in args[1:]:
self.write=largs[arg]
if varargs:
for arg in varargs:
self.write=largs[arg]
return self.read
def _get_func(self):
code=self.frame.f_code
self.write=code.co_name
return code
def __call__(self,func,*args):
self.write=func
if args:
for arg in args:
self.write=arg
return self.read
class cscarduino(arduino,object):
__metaclass__ = autoprop
def __init__(self,*arg):
arduino.__init__(self,*arg)
def echo(self,data):
self.func
return self.farg
def pinMode(self,pin, mode):
self.func
return self.farg
def digitalWrite(self,pin,value):
self.func
return self.farg
def analogRead(self,pin):
self.func
return self.farg
def analogWrite(self,pin,value):
self.func
return self.farg
if __name__=="__main__":
cscadn=cscarduino(5)
cscadn.debug=0
try:
cscadn.pinMode("13","1")
cscadn.pinMode("9","1")
r=[0,2,4,5,7,9,11,13,15]
for x in range(len(r)):
#cscadn("test","0")
print cscadn.echo(time.asctime())
cscadn.digitalWrite("13","1")
#time.sleep(0.1)
#cscadn.digitalWrite("13","0")
#time.sleep(1)
tmp=[]
for y in range(6):
tmp.append(cscadn.analogRead('%s'%y))
print tmp
#cscadn('pycall','analogWrite',"9","%s"%r[x])
cscadn.analogWrite("9","%s0"%r[x])
cscadn.digitalWrite("13","0")
#cscadn("test","1")
except:
print "Error!"
print traceback.format_exc()
finally:
#cscadn.write="!"
cscadn.close()
|