embedded RTOS class project.
Dependencies: C12832_lcd USBDevice mbed-rtos mbed mmSPI_RTOS watchdog_RTOS
mmPython/mmUSBserial.txt
- Committer:
- gatedClock
- Date:
- 2013-09-17
- Revision:
- 3:1b3329242491
- Parent:
- 0:8e898e1270d6
File content as of revision 3:1b3329242491:
#!/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 = 115200 # was 9600 self.__serialPort.timeout = 1 # one-second timeout. 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.") self.__SER_BYTES = 18 # same as mbed program's SER_BYTES. #-----------------------------------------------#------------------------------- def __del__(self): # destructor. self.__serialPort.close() print "object destroyed." #-----------------------------------------------#------------------------------- # the number of bytes written and read must both be SER_BYTES. # the '$' must be in bit<7> of what's written. def write(self,toWrite): # write a string. nowWrite = toWrite[:7] nowWrite = nowWrite + "$" # in <7>. if (self.__SER_BYTES > 8): # flush out with 0's. for loopIndex in range (0, self.__SER_BYTES - 8): nowWrite = nowWrite + "0" # print("nowWrite {}").format(nowWrite) self.__serialPort.write(nowWrite) return(nowWrite) #-----------------------------------------------#------------------------------- def read(self): # read a string. gotRead = self.__serialPort.read(self.__SER_BYTES) # print("gotRead {}").format(gotRead) return(gotRead) #-----------------------------------------------#------------------------------- #===============================================#===============================