Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd USBDevice mbed-rtos mbed mmSPI_RTOS watchdog_RTOS
Diff: mmPython/mmUSBserial.txt
- Revision:
- 0:8e898e1270d6
diff -r 000000000000 -r 8e898e1270d6 mmPython/mmUSBserial.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mmPython/mmUSBserial.txt Tue Sep 17 19:42:49 2013 +0000
@@ -0,0 +1,94 @@
+#!/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)
+#-----------------------------------------------#-------------------------------
+#===============================================#===============================
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+