2016-11-04 18:44:38 +00:00
|
|
|
'''
|
|
|
|
* Copyright (C) 2016 Alex Koren
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
'''
|
|
|
|
|
2016-11-04 18:40:42 +00:00
|
|
|
import serial
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os
|
2016-11-06 21:55:15 +00:00
|
|
|
import re
|
2016-11-04 18:40:42 +00:00
|
|
|
|
|
|
|
e = "\xff\xff\xff"
|
|
|
|
|
2016-11-05 22:12:11 +00:00
|
|
|
def getBaudrate(ser, fSize=None, checkModel=None):
|
2016-11-04 18:40:42 +00:00
|
|
|
for baudrate in (2400, 4800, 9600, 19200, 38400, 57600, 115200):
|
|
|
|
ser.baudrate = baudrate
|
|
|
|
ser.timeout = 3000 / baudrate + .2
|
|
|
|
print 'Trying with baudrate: ' + str(baudrate) + '...'
|
|
|
|
ser.write("\xff\xff\xff")
|
|
|
|
ser.write('connect')
|
|
|
|
ser.write("\xff\xff\xff")
|
|
|
|
r = ser.read(128)
|
|
|
|
if 'comok' in r:
|
|
|
|
print 'Connected with baudrate: ' + str(baudrate) + '...'
|
|
|
|
noConnect = False
|
2016-11-08 15:26:26 +00:00
|
|
|
status, unknown1, model, fwversion, mcucode, serial, flashSize = r.strip("\xff\x00").split(',')
|
2016-11-08 15:10:12 +00:00
|
|
|
print 'Status: ' + status.split(' ')[0]
|
2016-11-08 15:22:47 +00:00
|
|
|
if status.split(' ')[1] == "1":
|
2016-11-08 15:10:12 +00:00
|
|
|
print 'Touchscreen: yes'
|
|
|
|
else:
|
|
|
|
print 'Touchscreen: no'
|
2016-11-04 18:40:42 +00:00
|
|
|
print 'Model: ' + model
|
2016-11-08 15:11:57 +00:00
|
|
|
print 'Firmware version: ' + fwversion
|
2016-11-08 15:26:26 +00:00
|
|
|
print 'MCU code: ' + mcucode
|
2016-11-04 18:40:42 +00:00
|
|
|
print 'Serial: ' + serial
|
|
|
|
print 'Flash size: ' + flashSize
|
|
|
|
if fSize and fSize > flashSize:
|
|
|
|
print 'File too big!'
|
|
|
|
return False
|
2016-11-05 22:12:11 +00:00
|
|
|
if checkModel and not checkModel in model:
|
2016-11-04 18:40:42 +00:00
|
|
|
print 'Wrong Display!'
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def setDownloadBaudrate(ser, fSize, baudrate):
|
|
|
|
ser.write("")
|
|
|
|
ser.write("whmi-wri " + str(fSize) + "," + str(baudrate) + ",0" + e)
|
|
|
|
time.sleep(.05)
|
|
|
|
ser.baudrate = baudrate
|
|
|
|
ser.timeout = .5
|
|
|
|
r = ser.read(1)
|
|
|
|
if "\x05" in r:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def transferFile(ser, filename, fSize):
|
|
|
|
with open(filename, 'rb') as hmif:
|
|
|
|
dcount = 0
|
|
|
|
while True:
|
|
|
|
data = hmif.read(4096)
|
|
|
|
if len(data) == 0:
|
|
|
|
break
|
|
|
|
dcount += len(data)
|
|
|
|
ser.timeout = 5
|
|
|
|
ser.write(data)
|
|
|
|
sys.stdout.write('\rDownloading, %3.1f%%...'% (dcount / float(fSize) * 100.0))
|
|
|
|
sys.stdout.flush()
|
|
|
|
ser.timeout = .5
|
|
|
|
time.sleep(.5)
|
|
|
|
r = ser.read(1)
|
|
|
|
if "\x05" in r:
|
|
|
|
continue
|
|
|
|
else:
|
2016-11-08 20:45:52 +00:00
|
|
|
print
|
|
|
|
return False
|
2016-11-04 18:40:42 +00:00
|
|
|
break
|
|
|
|
print
|
|
|
|
return True
|
|
|
|
|
2016-11-05 22:12:11 +00:00
|
|
|
def upload(ser, filename, checkModel=None):
|
|
|
|
if not getBaudrate(ser, os.path.getsize(filename), checkModel):
|
2016-11-08 20:45:52 +00:00
|
|
|
print 'Could not find baudrate'
|
2016-11-04 18:40:42 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if not setDownloadBaudrate(ser, os.path.getsize(filename), 115200):
|
2016-11-08 20:45:52 +00:00
|
|
|
print 'Could not set download baudrate'
|
2016-11-04 18:40:42 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if not transferFile(ser, filename, os.path.getsize(filename)):
|
2016-11-08 20:45:52 +00:00
|
|
|
print 'Could not transfer file'
|
2016-11-04 18:40:42 +00:00
|
|
|
exit(1)
|
|
|
|
|
2016-11-08 20:45:52 +00:00
|
|
|
print 'File transferred successfully'
|
2016-11-04 18:40:42 +00:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-11-05 22:12:11 +00:00
|
|
|
if len(sys.argv) != 4 and len(sys.argv) != 3:
|
|
|
|
print 'usage:\npython nextion.py file_to_upload.tft /path/to/dev/ttyDevice [nextion_model_name]\
|
|
|
|
\nexample: nextion.py newUI.tft /dev/ttyUSB0 NX3224T024\
|
|
|
|
\nnote: model name is optional'
|
2016-11-04 18:40:42 +00:00
|
|
|
exit(1)
|
|
|
|
|
2016-11-06 21:51:48 +00:00
|
|
|
try:
|
|
|
|
ser = serial.Serial(sys.argv[2], 9600, timeout=5)
|
|
|
|
except serial.serialutil.SerialException:
|
|
|
|
print 'could not open serial device ' + sys.argv[2]
|
2016-11-04 18:40:42 +00:00
|
|
|
exit(1)
|
2016-11-08 15:22:47 +00:00
|
|
|
if serial.VERSION <= "3.0":
|
2016-11-07 09:51:53 +00:00
|
|
|
if not ser.isOpen():
|
|
|
|
ser.open()
|
|
|
|
else:
|
|
|
|
if not ser.is_open:
|
|
|
|
ser.open()
|
2016-11-04 18:40:42 +00:00
|
|
|
|
2016-11-05 22:12:11 +00:00
|
|
|
checkModel = None
|
|
|
|
if len(sys.argv) == 4:
|
|
|
|
checkModel = sys.argv[3]
|
2016-11-06 21:55:15 +00:00
|
|
|
pattern = re.compile("^NX\d{4}[TK]\d{3}$")
|
|
|
|
if not pattern.match(checkModel):
|
|
|
|
print 'Invalid model name. Please give a correct one (e.g. NX3224T024)'
|
|
|
|
exit(1)
|
2016-11-05 22:12:11 +00:00
|
|
|
upload(ser, sys.argv[1], checkModel)
|