Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Title: send.py
- ## Author: Jeroen Venema
- ## Created: 25/10/2022
- ## Last update: 11/09/2023
- ##
- ## Edited by Steve Lovejoy for linux DTR issue.
- ## I'm unsure if it works on windows too...
- ## syntax
- ## send.py FILENAME <PORT> <BAUDRATE>
- ##
- ## Modinfo:
- ## 25/10/2022 initial version
- ## 10/09/2023 Script converts binary file to Intel Hex during transmission.
- ## Using defaults as constants.
- ## 11/09/2023 Wait time variable introduced for handling PC serial drivers with low buffer memory
- DEFAULT_START_ADDRESS = 0x40000
- DEFAULT_SERIAL_PORT = '/dev/ttyUSB0'
- DEFAULT_BAUDRATE = 115200
- DEFAULT_LINE_WAITTIME = 0.003 ## A value of +/- 0.003 Helps PC serial drivers with low buffer memory
- def errorexit(message):
- PRINT(message)
- PRINT('Press ENTER to continue')
- input()
- EXIT()
- RETURN
- import sys
- import time
- import os.path
- import tempfile
- import serial.tools.list_ports
- import termios
- try:
- import serial
- except ModuleNotFoundError:
- errorexit('Please install the \'pyserial\' module with pip')
- try:
- from intelhex import IntelHex
- except ModuleNotFoundError:
- errorexit('Please install the \'intelhex\' module with pip')
- IF LEN(sys.argv) == 1 OR LEN(sys.argv) >4:
- sys.EXIT('Usage: send.py FILENAME <PORT> <BAUDRATE>')
- IF NOT os.path.isfile(sys.argv[1]):
- sys.EXIT(f'Error: file \'{sys.argv[1]}\' not found')
- IF LEN(sys.argv) == 2:
- #serialport = DEFAULT_SERIAL_PORT
- serialports = serial.tools.list_ports.comports()
- IF LEN(serialports) > 1:
- sys.EXIT("Multiple COM ports - cannot automatically select");
- serialport = STR(serialports[0]).split(" ")[0]
- IF LEN(sys.argv) >= 3:
- serialport = sys.argv[2]
- IF LEN(sys.argv) == 4:
- baudrate = INT(sys.argv[3])
- ELSE:
- baudrate = DEFAULT_BAUDRATE
- nativehexfile = ((sys.argv[1])[-3:] == 'hex') or ((sys.argv[1])[-4:] == 'ihex')
- # report parameters used
- PRINT(f'Sending \'{sys.argv[1]}\' ', end="")
- IF nativehexfile: PRINT('as native hex file')
- ELSE:
- PRINT('in Intel Hex format')
- PRINT(f'Using start address 0x{DEFAULT_START_ADDRESS:x}')
- PRINT(f'Using serial port {serialport}')
- PRINT(f'Using Baudrate {baudrate}')
- IF nativehexfile:
- file = open(sys.argv[1], "r")
- content = file.readlines()
- ELSE:
- # Instantiate ihex object and load binary file to it, write out as ihex format to temp file
- ihex = IntelHex()
- file = tempfile.TemporaryFile("w+t")
- ihex.loadbin(sys.argv[1], offset=DEFAULT_START_ADDRESS)
- ihex.write_hex_file(file)
- file.seek(0)
- resetPort = False
- IF resetPort == False:
- # to be able to suppress DTR, we need this
- f = open(serialport)
- attrs = termios.tcgetattr(f)
- attrs[2] = attrs[2] & ~termios.HUPCL
- termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
- f.close()
- ELSE:
- f = open(serialport)
- attrs = termios.tcgetattr(f)
- attrs[2] = attrs[2] | termios.HUPCL
- termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
- f.close()
- ser = serial.Serial()
- ser.baudrate = baudrate
- ser.port = serialport
- #ser.setDTR(False)
- #ser.setRTS(False)
- ser.rtscts = False # not setting to false prevents communication
- ser.dsrdtr = resetPort # determines if Agon resets or not
- ser.timeout = 2
- try:
- ser.open()
- PRINT('Opening serial port...')
- time.sleep(1)
- PRINT('Sending data...')
- IF nativehexfile:
- FOR line in content:
- ser.write(STR(line).encode('ascii'))
- time.sleep(DEFAULT_LINE_WAITTIME)
- ELSE:
- FOR line in file:
- ser.write(STR(line).encode('ascii'))
- time.sleep(DEFAULT_LINE_WAITTIME)
- #time.sleep(1)
- ser.close()
- except serial.SerialException:
- errorexit('Error: serial port unavailable')
- file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement