📄 dongle.py
字号:
# where cc is a list of the tty special characters.
self.__params=[]
# c_iflag
self.__params.append(IGNPAR)
# c_oflag
self.__params.append(0)
# c_cflag
self.__params.append(CS8|CREAD|CRTSCTS)
# c_lflag
self.__params.append(0)
# c_ispeed
self.__params.append(SerialPortLin.BaudRatesDic[self.__speed])
# c_ospeed
self.__params.append(SerialPortLin.BaudRatesDic[self.__speed])
cc=[0]*NCCS
if self.__timeout==None:
# A reading is only complete when VMIN characters have
# been received (blocking reading)
cc[VMIN]=1
cc[VTIME]=0
elif self.__timeout==0:
cc[VMIN]=0
cc[VTIME]=0
else:
cc[VMIN]=0
cc[VTIME]=self.__timeout #/100
self.__params.append(cc) # c_cc
tcsetattr(self.__handle, TCSANOW, self.__params)
def fileno(self):
return self.__handle
def __read1(self):
tryCnt = 0
byte = ""
while(len(byte)==0 and tryCnt<10):
tryCnt+=1
byte = os.read(self.__handle, 2)
if len(byte)==0 and self.__timeout!=0: # Time-out
print 'Time out cnt was %i'%(tryCnt)
print 'Expected 1 byte but got %i before timeout'%(len(byte))
sys.stdout.flush()
raise SerialPortException('Timeout')
else:
return byte
def read(self, num=1):
s=''
for i in range(num/2):
s=s+SerialPortLin.__read1(self)
return s
def readline(self):
s = ''
while not '\n' in s:
s = s+SerialPortLin.__read1(self)
return s
def write(self, s):
"""Write the string s to the serial port"""
return os.write(self.__handle, s)
def inWaiting(self):
"""Returns the number of bytes waiting to be read"""
data = struct.pack("L", 0)
data=fcntl.ioctl(self.__handle, TIOCINQ, data)
return struct.unpack("L", data)[0]
def outWaiting(self):
"""Returns the number of bytes waiting to be write
mod. by J.Grauheding
result needs some finetunning
"""
rbuf=fcntl.ioctl(self.__handle, TIOCOUTQ, self.buf)
return rbuf
def flush(self):
"""Discards all bytes from the output or input buffer"""
tcflush(self.__handle, TCIOFLUSH)
if sys.platform=='darwin':
class SerialPortOSX:
"""Encapsulate methods for accesing to a serial port."""
BaudRatesDic={
110: B110,
300: B300,
600: B600,
1200: B1200,
2400: B2400,
4800: B4800,
9600: B9600,
19200: B19200,
38400: B38400,
57600: B57600,
115200: B115200,
230400: B230400
}
buf = array.array('h', '\000'*4)
def __init__(self, dev, timeout=None, speed=115200, mode='232', params=None):
self.__devName, self.__timeout, self.__speed=dev, timeout, speed
self.__mode=mode
self.__params=params
self.__speed = 0
self.__reopen = 0
while 1:
try:
self.__handle=os.open(dev, os.O_RDWR)
break
except:
n=0
while (n < 2000000):
n += 1;
self.__reopen = self.__reopen + 1
if self.__reopen > 32:
print "Port does not exist..."
raise SerialPortException('Port does not exist...')
break
self.__configure()
def __del__(self):
if self.__speed:
#tcsetattr(self.__handle, TCSANOW, self.__oldmode)
pass
try:
pass
#os.close(self.__handle)
except IOError:
raise SerialPortException('Unable to close port')
def __configure(self):
if not self.__speed:
self.__speed=115200
# Save the initial port configuration
self.__oldmode=tcgetattr(self.__handle)
if not self.__params:
# print "Create MacOSX params for serialport..."
# self.__params is a list of attributes of the file descriptor
# self.__handle as follows:
# [c_iflag, c_oflag, c_cflag, c_lflag, c_ispeed, c_ospeed, cc]
# where cc is a list of the tty special characters.
self.__params=[]
# c_iflag
self.__params.append(IGNPAR)
# c_oflag
self.__params.append(0)
# c_cflag
self.__params.append(CS8|CREAD|CRTSCTS)
# c_lflag
self.__params.append(0)
# c_ispeed
self.__params.append(SerialPortOSX.BaudRatesDic[self.__speed])
# c_ospeed
self.__params.append(SerialPortOSX.BaudRatesDic[self.__speed])
cc=[0]*NCCS
if self.__timeout==None:
# A reading is only complete when VMIN characters have
# been received (blocking reading)
cc[VMIN]=1
cc[VTIME]=0
elif self.__timeout==0:
cc[VMIN]=0
cc[VTIME]=0
else:
cc[VMIN]=0
cc[VTIME]=self.__timeout #/100
self.__params.append(cc) # c_cc
tcsetattr(self.__handle, TCSANOW, self.__params)
def fileno(self):
return self.__handle
def __read1(self):
tryCnt = 0
byte = ""
while(len(byte)==0 and tryCnt<10):
tryCnt+=1
byte = os.read(self.__handle, 2)
if len(byte)==0 and self.__timeout!=0: # Time-out
print 'Time out cnt was %i'%(tryCnt)
print 'Expected 1 byte but got %i before timeout'%(len(byte))
sys.stdout.flush()
raise SerialPortException('Timeout')
else:
return byte
def read(self, num=1):
s=''
for i in range(num/2):
s=s+SerialPortOSX.__read1(self)
return s
def readline(self):
s = ''
while not '\n' in s:
s = s+SerialPortOSX.__read1(self)
return s
def write(self, s):
"""Write the string s to the serial port"""
return os.write(self.__handle, s)
def inWaiting(self):
"""Returns the number of bytes waiting to be read"""
data = struct.pack("L", 0)
data=fcntl.ioctl(self.__handle, FIONREAD, data)
return struct.unpack("L", data)[0]
def outWaiting(self):
"""Returns the number of bytes waiting to be write
mod. by J.Grauheding
result needs some finetunning
"""
rbuf=fcntl.ioctl(self.__handle, FIONWRITE, self.buf)
return rbuf
def flush(self):
"""Discards all bytes from the output or input buffer"""
tcflush(self.__handle, TCIOFLUSH)
#### end inline of artec FTDI spesific Uspp code ###############################################
#### Dongle code starts here ##################################################################
#### global funcs ####
def usage(s):
print "Artec USB Dongle programming utility ver. 2.6"
print "Usage:"
print "Write file : ",s," [-vq] -c <name> <file> <offset>"
print "Readback file : ",s," [-vq] -c <name> [-vq] -r <offset> <length> <file>"
print "Options:"
print " <file> <offset> When file and offset are given file will be written to dongle"
print " file: File name to be written to dongle"
print " offset: Specifies data writing starting point in bytes to 4M window"
print " For ThinCan boot code the offset = 4M - filesize. To write"
print " 256K file the offset must be 3840K"
print " "
print " -c <name> Indicate port name where the USB Serial Device is"
print " name: COM port name in Windows or Linux Examples: COM3,/dev/ttyS3"
print " See Device Manager in windows for USB Serial Port number"
print " "
print " -v Enable verbose mode. Displays more progress information"
print " "
print " -q Perform flash query to see if dongle flash is responding"
print " "
print " -r <offset> <length> <file> Readback data. Available window size is 4MB"
print " offset: Offset byte addres inside 4MB window. Example: 1M"
print " use M for MegaBytes, K for KiloBytes, none for bytes"
print " use 0x prefix to indicate hexademical number format"
print " length: Amount in bytes to read starting from offset. Example: 1M"
print " use M for MegaBytes, K for KiloBytes, none for bytes"
print " file: Filename where data will be written"
print " "
print " -e Erase device. Erases Full 4 MegaBytes"
print "Board test options: "
print " -t Marching one and zero test. Device must be empty"
print " To test dongle erase the flash with command -e"
print " Enables dongle memory tests to be executed by user"
print " "
print " -b Leave flash blank after test. Used with option -t"
print " -l Fast poll loop test. Does poll loop 1024 times"
print " used to stress test connection"
print ""
print "Examples:"
print ""
print " ",s," -c COM3 loader.bin 0 "
print " ",s," -c /dev/ttyS3 boot.bin 3840K"
print " ",s," -c COM3 -r 0x3C0000 256K flashcontent.bin"
print " ",s," -c /dev/cu.usbserial-003011FD -v (Mac OS X)"
######################
class DongleMode:
def __init__(self):
self.v = 0
self.f = 0
self.d = 0
self.q = 0
self.r = 0
self.t = 0
self.e = 0
self.b = 0
self.l = 0
self.filename=""
self.portname=""
self.address=-1
self.offset=-1
self.length=-1
self.version=4
def convParamStr(self,param):
mult = 1
value = 0
str = param
if str.find("K")>-1:
mult = 1024
str=str.strip("K")
if str.find("M")>-1:
mult = 1024*1024
str=str.strip("M")
try:
if str.find("x")>-1:
value = int(str,0)*mult #conver hex string to int
else:
value = int(str)*mult #conver demical string to int
except ValueError:
print "Bad parameter format given for: ",param
return value
class Dongle:
def __init__(self,name, baud, timeout): #time out in millis 1000 = 1s baud like 9600, 57600
self.mode = 0
try:
if sys.platform=='win32':
self.tty = SerialPortWin(name,timeout, baud)
elif sys.platform=='linux2':
self.tty = SerialPortLin(name,timeout, baud)
elif sys.platform=='darwin':
self.tty = SerialPortOSX(name,timeout, baud)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -