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

mmPython/mmUSBserial.txt

Committer:
gatedClock
Date:
2013-09-01
Revision:
8:db29cce17a33
Parent:
4:92539904a4ad

File content as of revision 8:db29cce17a33:

#!/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)
#-----------------------------------------------#-------------------------------
#===============================================#===============================