embedded RTOS class project.
Dependencies: C12832_lcd USBDevice mbed-rtos mbed mmSPI_RTOS watchdog_RTOS
Fork of RTOS_project_fork_01 by
mmPython/mmUSBserial.txt@0:8e898e1270d6, 2013-09-17 (annotated)
- Committer:
- gatedClock
- Date:
- Tue Sep 17 19:42:49 2013 +0000
- Revision:
- 0:8e898e1270d6
title.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gatedClock | 0:8e898e1270d6 | 1 | #!/usr/bin/python -tt # tt: detect mixed space/tab. |
gatedClock | 0:8e898e1270d6 | 2 | #---copyright-----------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 3 | # licensed for personal and academic use. |
gatedClock | 0:8e898e1270d6 | 4 | # commercial use must be approved by the account-holder of |
gatedClock | 0:8e898e1270d6 | 5 | # gated.clock@gmail.com |
gatedClock | 0:8e898e1270d6 | 6 | #-------imports---------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 7 | import serial # serial over USB. |
gatedClock | 0:8e898e1270d6 | 8 | #=======class===================================#=============================== |
gatedClock | 0:8e898e1270d6 | 9 | if (1): |
gatedClock | 0:8e898e1270d6 | 10 | class mmUSBserial(): # USB class. |
gatedClock | 0:8e898e1270d6 | 11 | #-----------------------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 12 | # constructor. |
gatedClock | 0:8e898e1270d6 | 13 | def __init__(self, idVendor, idProduct): |
gatedClock | 0:8e898e1270d6 | 14 | |
gatedClock | 0:8e898e1270d6 | 15 | self.__idVendor = idVendor # promote to object scope. |
gatedClock | 0:8e898e1270d6 | 16 | self.__idProduct = idProduct # promote to object scope. |
gatedClock | 0:8e898e1270d6 | 17 | |
gatedClock | 0:8e898e1270d6 | 18 | openSuccess = 1 |
gatedClock | 0:8e898e1270d6 | 19 | self.__serialPort = serial.Serial() |
gatedClock | 0:8e898e1270d6 | 20 | self.__serialPort.baudrate = 115200 # was 9600 |
gatedClock | 0:8e898e1270d6 | 21 | self.__serialPort.timeout = 1 # one-second timeout. |
gatedClock | 0:8e898e1270d6 | 22 | |
gatedClock | 0:8e898e1270d6 | 23 | for portIndex in range(7, -1, -1): |
gatedClock | 0:8e898e1270d6 | 24 | portString = "/dev/ttyACM" + "{}".format(portIndex) |
gatedClock | 0:8e898e1270d6 | 25 | self.__serialPort.port = portString |
gatedClock | 0:8e898e1270d6 | 26 | try: |
gatedClock | 0:8e898e1270d6 | 27 | self.__serialPort.open() |
gatedClock | 0:8e898e1270d6 | 28 | except: |
gatedClock | 0:8e898e1270d6 | 29 | print("failed to open port {}.").format(portString) |
gatedClock | 0:8e898e1270d6 | 30 | openSuccess = 0 |
gatedClock | 0:8e898e1270d6 | 31 | |
gatedClock | 0:8e898e1270d6 | 32 | if (openSuccess): break |
gatedClock | 0:8e898e1270d6 | 33 | openSuccess = 1 |
gatedClock | 0:8e898e1270d6 | 34 | |
gatedClock | 0:8e898e1270d6 | 35 | if (openSuccess): print("successfully opened port {}.").format(portString) |
gatedClock | 0:8e898e1270d6 | 36 | else: print("could not open any port.") |
gatedClock | 0:8e898e1270d6 | 37 | |
gatedClock | 0:8e898e1270d6 | 38 | self.__SER_BYTES = 18 # same as mbed program's SER_BYTES. |
gatedClock | 0:8e898e1270d6 | 39 | #-----------------------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 40 | def __del__(self): # destructor. |
gatedClock | 0:8e898e1270d6 | 41 | self.__serialPort.close() |
gatedClock | 0:8e898e1270d6 | 42 | print "object destroyed." |
gatedClock | 0:8e898e1270d6 | 43 | #-----------------------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 44 | # the number of bytes written and read must both be SER_BYTES. |
gatedClock | 0:8e898e1270d6 | 45 | # the '$' must be in bit<7> of what's written. |
gatedClock | 0:8e898e1270d6 | 46 | |
gatedClock | 0:8e898e1270d6 | 47 | def write(self,toWrite): # write a string. |
gatedClock | 0:8e898e1270d6 | 48 | nowWrite = toWrite[:7] |
gatedClock | 0:8e898e1270d6 | 49 | nowWrite = nowWrite + "$" # in <7>. |
gatedClock | 0:8e898e1270d6 | 50 | if (self.__SER_BYTES > 8): # flush out with 0's. |
gatedClock | 0:8e898e1270d6 | 51 | for loopIndex in range (0, self.__SER_BYTES - 8): |
gatedClock | 0:8e898e1270d6 | 52 | nowWrite = nowWrite + "0" |
gatedClock | 0:8e898e1270d6 | 53 | # print("nowWrite {}").format(nowWrite) |
gatedClock | 0:8e898e1270d6 | 54 | self.__serialPort.write(nowWrite) |
gatedClock | 0:8e898e1270d6 | 55 | return(nowWrite) |
gatedClock | 0:8e898e1270d6 | 56 | #-----------------------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 57 | def read(self): # read a string. |
gatedClock | 0:8e898e1270d6 | 58 | gotRead = self.__serialPort.read(self.__SER_BYTES) |
gatedClock | 0:8e898e1270d6 | 59 | # print("gotRead {}").format(gotRead) |
gatedClock | 0:8e898e1270d6 | 60 | return(gotRead) |
gatedClock | 0:8e898e1270d6 | 61 | #-----------------------------------------------#------------------------------- |
gatedClock | 0:8e898e1270d6 | 62 | #===============================================#=============================== |
gatedClock | 0:8e898e1270d6 | 63 | |
gatedClock | 0:8e898e1270d6 | 64 | |
gatedClock | 0:8e898e1270d6 | 65 | |
gatedClock | 0:8e898e1270d6 | 66 | |
gatedClock | 0:8e898e1270d6 | 67 | |
gatedClock | 0:8e898e1270d6 | 68 | |
gatedClock | 0:8e898e1270d6 | 69 | |
gatedClock | 0:8e898e1270d6 | 70 | |
gatedClock | 0:8e898e1270d6 | 71 | |
gatedClock | 0:8e898e1270d6 | 72 | |
gatedClock | 0:8e898e1270d6 | 73 | |
gatedClock | 0:8e898e1270d6 | 74 | |
gatedClock | 0:8e898e1270d6 | 75 | |
gatedClock | 0:8e898e1270d6 | 76 | |
gatedClock | 0:8e898e1270d6 | 77 | |
gatedClock | 0:8e898e1270d6 | 78 | |
gatedClock | 0:8e898e1270d6 | 79 | |
gatedClock | 0:8e898e1270d6 | 80 | |
gatedClock | 0:8e898e1270d6 | 81 | |
gatedClock | 0:8e898e1270d6 | 82 | |
gatedClock | 0:8e898e1270d6 | 83 | |
gatedClock | 0:8e898e1270d6 | 84 | |
gatedClock | 0:8e898e1270d6 | 85 | |
gatedClock | 0:8e898e1270d6 | 86 | |
gatedClock | 0:8e898e1270d6 | 87 | |
gatedClock | 0:8e898e1270d6 | 88 | |
gatedClock | 0:8e898e1270d6 | 89 | |
gatedClock | 0:8e898e1270d6 | 90 | |
gatedClock | 0:8e898e1270d6 | 91 | |
gatedClock | 0:8e898e1270d6 | 92 | |
gatedClock | 0:8e898e1270d6 | 93 | |
gatedClock | 0:8e898e1270d6 | 94 |