Advertisement
lovejoy777

Agon Hex loader linux DTR reset fix

Jan 28th, 2024
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ZXBasic 3.67 KB | Software | 0 0
  1. ## Title:       send.py
  2. ## Author:      Jeroen Venema
  3. ## Created:     25/10/2022
  4. ## Last update: 11/09/2023
  5. ##
  6. ## Edited by Steve Lovejoy for linux DTR issue.
  7. ## I'm unsure if it works on windows too...
  8.  
  9. ## syntax
  10. ## send.py FILENAME <PORT> <BAUDRATE>
  11. ##
  12.  
  13. ## Modinfo:
  14. ## 25/10/2022 initial version
  15. ## 10/09/2023 Script converts binary file to Intel Hex during transmission.
  16. ##            Using defaults as constants.
  17. ## 11/09/2023 Wait time variable introduced for handling PC serial drivers with low buffer memory
  18.  
  19. DEFAULT_START_ADDRESS = 0x40000
  20. DEFAULT_SERIAL_PORT   = '/dev/ttyUSB0'
  21. DEFAULT_BAUDRATE      = 115200
  22. DEFAULT_LINE_WAITTIME = 0.003      ## A value of +/- 0.003 Helps PC serial drivers with low buffer memory
  23.  
  24. def errorexit(message):
  25.   PRINT(message)
  26.   PRINT('Press ENTER to continue')
  27.   input()
  28.   EXIT()
  29.   RETURN
  30.  
  31. import sys
  32. import time
  33. import os.path
  34. import tempfile
  35. import serial.tools.list_ports
  36. import termios
  37. try:
  38.   import serial
  39. except ModuleNotFoundError:
  40.   errorexit('Please install the \'pyserial\' module with pip')
  41. try:
  42.   from intelhex import IntelHex
  43. except ModuleNotFoundError:
  44.   errorexit('Please install the \'intelhex\' module with pip')
  45.  
  46.  
  47.  
  48. IF LEN(sys.argv) == 1 OR LEN(sys.argv) >4:
  49.   sys.EXIT('Usage: send.py FILENAME <PORT> <BAUDRATE>')
  50.  
  51. IF NOT os.path.isfile(sys.argv[1]):
  52.   sys.EXIT(f'Error: file \'{sys.argv[1]}\' not found')
  53.  
  54. IF LEN(sys.argv) == 2:
  55.   #serialport = DEFAULT_SERIAL_PORT
  56.   serialports = serial.tools.list_ports.comports()
  57.   IF LEN(serialports) > 1:
  58.     sys.EXIT("Multiple COM ports - cannot automatically select");
  59.   serialport = STR(serialports[0]).split(" ")[0]
  60. IF LEN(sys.argv) >= 3:
  61.   serialport = sys.argv[2]
  62.  
  63. IF LEN(sys.argv) == 4:
  64.   baudrate = INT(sys.argv[3])
  65. ELSE:
  66.   baudrate = DEFAULT_BAUDRATE
  67.  
  68. nativehexfile = ((sys.argv[1])[-3:] == 'hex') or ((sys.argv[1])[-4:] == 'ihex')
  69.  
  70. # report parameters used
  71. PRINT(f'Sending \'{sys.argv[1]}\' ', end="")
  72. IF nativehexfile: PRINT('as native hex file')
  73. ELSE:
  74.   PRINT('in Intel Hex format')
  75.   PRINT(f'Using start address 0x{DEFAULT_START_ADDRESS:x}')
  76. PRINT(f'Using serial port {serialport}')
  77. PRINT(f'Using Baudrate {baudrate}')
  78.  
  79. IF nativehexfile:
  80.   file = open(sys.argv[1], "r")
  81.   content = file.readlines()
  82. ELSE:
  83.   # Instantiate ihex object and load binary file to it, write out as ihex format to temp file
  84.   ihex = IntelHex()
  85.   file = tempfile.TemporaryFile("w+t")
  86.   ihex.loadbin(sys.argv[1], offset=DEFAULT_START_ADDRESS)
  87.   ihex.write_hex_file(file)
  88.   file.seek(0)
  89.  
  90. resetPort = False
  91.  
  92. IF resetPort == False:
  93. # to be able to suppress DTR, we need this
  94.   f = open(serialport)
  95.   attrs = termios.tcgetattr(f)
  96.   attrs[2] = attrs[2] & ~termios.HUPCL
  97.   termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
  98.   f.close()
  99. ELSE:
  100.   f = open(serialport)
  101.   attrs = termios.tcgetattr(f)
  102.   attrs[2] = attrs[2] | termios.HUPCL
  103.   termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
  104.   f.close()
  105.  
  106. ser = serial.Serial()
  107. ser.baudrate = baudrate
  108. ser.port = serialport
  109. #ser.setDTR(False)
  110. #ser.setRTS(False)
  111. ser.rtscts = False            # not setting to false prevents communication
  112. ser.dsrdtr = resetPort        # determines if Agon resets or not
  113. ser.timeout = 2
  114. try:
  115.     ser.open()
  116.     PRINT('Opening serial port...')
  117.     time.sleep(1)
  118.     PRINT('Sending data...')
  119.  
  120.     IF nativehexfile:
  121.       FOR line in content:
  122.         ser.write(STR(line).encode('ascii'))
  123.         time.sleep(DEFAULT_LINE_WAITTIME)
  124.     ELSE:
  125.       FOR line in file:
  126.         ser.write(STR(line).encode('ascii'))
  127.         time.sleep(DEFAULT_LINE_WAITTIME)
  128.  
  129.     #time.sleep(1)
  130.     ser.close()
  131. except serial.SerialException:
  132.     errorexit('Error: serial port unavailable')
  133.  
  134. file.close()
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement