USB Device Programming class project. This project allows a Python/Tk program running on a PC host to monitor/control a test-CPU programmed into an altera development board.
Dependencies: C12832_lcd USBDevice mbed-rtos mbed mmSPI
Diff: mmPython/mmUSBserial.txt
- Revision:
- 4:92539904a4ad
- Child:
- 8:db29cce17a33
diff -r 659ffc90b59e -r 92539904a4ad mmPython/mmUSBserial.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mmPython/mmUSBserial.txt Sun Sep 01 02:58:57 2013 +0000 @@ -0,0 +1,61 @@ +#!/usr/bin/python -tt # tt: detect mixed space/tab. +#---copyright-----------------------------------#------------------------------- +# licensed for personal and academic use. +# commercial use must be approved by the account-holder of +# gated.clock@gmail.com +#-------imports---------------------------------#------------------------------- +import serial # serial over USB. +#=======class===================================#=============================== +if (1): + class mmUSBserial(): # USB class. +#-----------------------------------------------#------------------------------- + # constructor. + def __init__(self, idVendor, idProduct): + + self.__idVendor = idVendor # promote to object scope. + self.__idProduct = idProduct # promote to object scope. + + openSuccess = 1 + self.__serialPort = serial.Serial() + self.__serialPort.baudrate = 9600 + + for portIndex in range(7, -1, -1): + portString = "/dev/ttyACM" + "{}".format(portIndex) + self.__serialPort.port = portString + try: + self.__serialPort.open() + except: + print("failed to open port {}.").format(portString) + openSuccess = 0 + + if (openSuccess): break + openSuccess = 1 + + if (openSuccess): print("successfully opened port {}.").format(portString) + else: print("could not open any port.") +#-----------------------------------------------#------------------------------- + def __del__(self): # destructor. + self.__serialPort.close() + print "object destroyed." +#-----------------------------------------------#------------------------------- + def write(self,toWrite): # write a string. + nowWrite = toWrite[:7] + nowWrite = nowWrite + "$" + self.__serialPort.write(nowWrite) + return(nowWrite) +#-----------------------------------------------#------------------------------- + def read(self): # read a string. + gotRead = self.__serialPort.read(8) + return(gotRead) +#-----------------------------------------------#------------------------------- +#===============================================#=============================== + + + + + + + + + +