I need help with python script

#!/usr/bin/env python3

import time
import serial
import sys, getopt
import os.path

from os import path

import binascii

Currently only single block programming possible. Use Renesas Flash Programmer V2 to flash multiple blocks.

Number of blocks in PD78F0513

nBlocks = 32

StatusList = {
0x00: “No data”,
0x04: “Command number error”,
0x05: “Parameter error”,
0x06: “Normal acknowledgment (ACK)”,
0x07: “Checksum error”,
0x0F: “Verify error”,
0x10: “Protect error”,
0x15: “Negative acknowledgment (NACK)”,
0x1A: “MRG10 error”,
0x1B: “MRG11 error”,
0x1C: “Write error”,
0x20: “Read error”,
0xFF: “Processing in progress (BUSY)”,
}

def add_crc(b, size):
crc = 0x00 # initial value

    for x in range(size + 1):
        crc = (crc - b[x+1]) % 256
    return crc

class Programmer(object):

def __init__(self):
    print("NEC Programmer v1.0")
    print("written by Martin Jansson\n\n")

def SerialInit(self, serial_port):
    print("Requesting serial port and resetting chip...")
    self.ser = serial.Serial(
        port=serial_port,
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1)

    self.ser.isOpen()

    # Reset microcontroller with RTS line
    self.ser.setRTS(True)
    time.sleep(11/1000)
    self.ser.setRTS(False)
    time.sleep(11/1000)
    self.ser.setRTS(True)

    time.sleep(400/1000)

    self.ser.write(bytearray([0x00]))
    time.sleep(11/1000)
    self.ser.write(bytearray([0x00]))
    time.sleep(11/1000)

    return       

def Receive(self):
    b = bytearray()

    b = self.ser.read(2) # read 2 bytes to determine length

    if len(b) < 2:
        print("error: Length less than 2")
        return
    b += self.ser.read(b[1]+1+1)
    # TODO: ADD CRC CHECK
    print("Response: ", StatusList[b[2]])

    return b[2]

def Reset(self):
    b = bytearray()

    b.append(0x01)      # Start byte
    b.append(0x01)      # Length
    b.append(0x00)      # Command
    b.append(add_crc(b, b[1])) # crc
    b.append(0x03)      # Stop byte

    self.ser.write(b)
    self.Receive()

    return

def BlankCheckBlock(self, block):

    start_address = block * 1024
    end_address = ((block + 1) * 1024) -1
    b = bytearray()

    b.append(0x01)   # Start byte
    b.append(0x07)   # Length
    b.append(0x32)   # Command

    b.append(start_address >> 16 & 0xFF)
    b.append(start_address >> 8 & 0xFF)
    b.append(start_address & 0xFF)
    b.append(end_address >> 16 & 0xFF)
    b.append(end_address >> 8 & 0xFF)
    b.append(end_address & 0xFF)

    b.append(add_crc(b, b[1])) # crc
    b.append(0x03) # stop byte
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("Block not empty")
        return False
    print("Block empty")    
    return True

def Program(self, startBlock, file):
    endBlock = startBlock
    if startBlock == 0:
        startBlock = 0
        endBlock = nBlocks - 1 
    self.ProgramBlock(startBlock, endBlock, file)

    return

def ProgramBlock(self, startBlock, endBlock, file):
    start_address = startBlock * 1024
    end_address = ((endBlock + 1) * 1024) -1
    b = bytearray()

    b.append(0x01)   # Start byte
    b.append(0x07)   # Length
    b.append(0x40)   # Command (programming)

    b.append(start_address >> 16 & 0xFF)
    b.append(start_address >> 8 & 0xFF)
    b.append(start_address & 0xFF)
    b.append(end_address >> 16 & 0xFF)
    b.append(end_address >> 8 & 0xFF)
    b.append(end_address & 0xFF)

    b.append(add_crc(b, b[1])) # crc
    b.append(0x03) # stop byte

    #print(b.hex())
    print("Sending programming command...")
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("error: Did not receive ACK, returning...")
        return False

    b = bytearray()

    b.append(0x02)   # Start byte
    b.append(0x00)   # Length (00h = 256 bytes)

    with open(file, 'rb') as f:
        # read file as hex.
        hexdata = f.read().hex()
    b.extend(bytearray.fromhex(hexdata))

        
    b.append(add_crc(b, 256)) # crc
    b.append(0x03) # stop byte

    print("Sending data..")
    #print(b)
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("error: Programming failed!")
        return False
    print("Programming succeeded!")    
    return True

def BlockErase(self, block):
    start_address = block * 1024
    end_address = ((block + 1) * 1024) -1
    b = bytearray()

    b.append(0x01)   # Start byte
    b.append(0x07)   # Length
    b.append(0x22)   # Command (block erase)

    b.append(start_address >> 16 & 0xFF)
    b.append(start_address >> 8 & 0xFF)
    b.append(start_address & 0xFF)
    b.append(end_address >> 16 & 0xFF)
    b.append(end_address >> 8 & 0xFF)
    b.append(end_address & 0xFF)

    b.append(add_crc(b, b[1])) # crc
    b.append(0x03) # stop byte

    print("Sending erase block command..")
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("error: Did not receive ACK, returning...")
        return False
    print("Block erased.") 
    return True


def ChipErase(self):
    b = bytearray()

    b.append(0x01)   # Start byte
    b.append(0x01)   # Length
    b.append(0x20)   # Command (chip erase)

    b.append(add_crc(b, b[1])) # crc
    b.append(0x03) # stop byte

    print("Sending erase chip command..")
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("error: Did not receive ACK, returning...")
        return False
    print("Chip erased.") 
    return True

def Verify(self, block, file):
    if block == None:
        block = 1
    return self.VerifyBlock(block,file)

def VerifyBlock(self, block, file):
    start_address = block * 1024
    end_address = ((block + 1) * 1024) -1
    b = bytearray()

    b.append(0x01)   # Start byte
    b.append(0x07)   # Length
    b.append(0x13)   # Command (verify)

    b.append(start_address >> 16 & 0xFF)
    b.append(start_address >> 8 & 0xFF)
    b.append(start_address & 0xFF)
    b.append(end_address >> 16 & 0xFF)
    b.append(end_address >> 8 & 0xFF)
    b.append(end_address & 0xFF)

    b.append(add_crc(b, b[1])) # crc
    b.append(0x03) # stop byte

    #print(b.hex())
    print("Sending verify command..")
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("error: Did not receive ACK, returning...")
        return False

    b = bytearray()

    b.append(0x02)   # Start byte
    b.append(0x00)   # Length (00h = 256 bytes)

    with open(file, 'rb') as f:
        # read file as hex.
        hexdata = f.read().hex()
    b.extend(bytearray.fromhex(hexdata))

        
    b.append(add_crc(b, 256)) # crc
    b.append(0x03) # stop byte

    print("Sending data..")
    #print(b)
    self.ser.write(b)

    status = self.Receive()
    if status != 0x06:
        print("error: Verification failed! (mismatch?)")
        return False
    print("Verification OK!")    
    return True

def usage():
print(“usage: -h, --help Print this help”)
print(" -p, --port Specify serial port")
print(" -b, --block Specify specified block")
print(" -f, --flash Specify file to flash")
print(" -e, --erase Erase chip/block")
print(" -v, --verify Specify file to verify")
print(" -c, --checkempty Check if chip/block is empty")
return

def main():
try:
opts, args = getopt.getopt(sys.argv[1:], “hp:fecb:vi:”, [“help”, “port=”, “flash”, “erase”, “checkempty”, “block=”, “verify”, “input=”])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like “option -a not recognized”
usage()
sys.exit(2)
port = “COM1”
file = None
block = 0
blockSpecified = False

flash = False
erase = False
verify = False
checkEmpty = False
for o, a in opts:
    if o in ("-v", "--verify"):
        verify = True
    elif o in ("-h", "--help"):
        usage()
        sys.exit()
    elif o in ("-p", "--port"):
        port = a
        print("setting port to: ", port)
    elif o in ("-i", "--input"):
        file = a
        if not path.exists(file):
            print("missing input file.\n")
            usage()
            sys.exit(2)
    elif o in ("-b", "--block"):
        blockSpecified = True
        block = int(a)
        print("specified block: ", block)
    elif o in ("-f", "--flash"):
        flash = True
    elif o in ("-e", "--erase"):
        erase = True
    elif o in ("-c", "--checkempty"):
        checkEmpty = True
    else:
        assert False, "unhandled option"

if flash or erase or verify or checkEmpty:
    if file == None and not (erase or checkEmpty):
        print("No file supplied")
        sys.exit(2)

    foo = Programmer()
    foo.SerialInit(port)
    time.sleep(500/1000) # 500 ms sleep
    foo.Reset()
    time.sleep(500/1000) # 500 ms sleep

    if erase:
        if not blockSpecified:
            print("\nErasing chip...\n")
            if not foo.ChipErase():
                foo.ser.close()
                sys.exit(2)
        else:
            print("\nErasing block", block, "...\n")
            if not foo.BlockErase(block):
                foo.ser.close()
                sys.exit(2) 
    if flash:
        print("\nFlashing chip...\n")
        if not foo.Program(block, file):
            foo.ser.close()
            sys.exit(2)

    if verify:
        print("\nVerifying chip...\n")
        if not foo.VerifyBlock(block, file):
            foo.ser.close()
            sys.exit(2)

    if checkEmpty:
        print("\nGetting empty blocks...\n")
        blockStates = []
        for x in range(nBlocks):
            if not foo.BlankCheckBlock(x):
                blockStates.append("not empty")
            else: 
                blockStates.append("empty")
        
        for x in range(nBlocks):
            print("Block", x, blockStates[x])

        foo.ser.close()
        

    foo.ser.close()

if name == “main”:
main()


I run this script but it does nothing , i don’t know what’s wrong.
any help
Thanks advance

How are you running the script?

What command are you using?

Hello @alfa, welcome to Python Discourse.
Can you please give more detail about your script. What the script trying to do (I assume it’s for flash some hardware), do you already connect your hardware to host PC and use the commandline interface

try this from terminal or cmd.exe:

C:\Users\Repeony>python "flash utility.py" --help

it will give error:

Traceback (most recent call last):
  File "C:\Users\Repeony\flash utility.py", line 4, in <module>
    import serial
ModuleNotFoundError: No module named 'serial'

You need to install pyserial package, follow steps below from your terminal:

C:\Users\Repeony>mkdir flashing_script
C:\Users\Repeony>cd flashing_script
C:\Users\Repeony\flashing_script>python -m venv .venv
C:\Users\Repeony\flashing_script>.venv\Scripts\activate.bat
(.venv) C:\Users\Repeony\flashing_script>pip install pyserial

Open explorer.exe copy your python file to new directory you just made, than type this in the same terminal:

C:\Users\Repeony\flashing_script>python "flash utility.py" --help
usage: -h, --help Print this help
 -p, --port Specify serial port
 -b, --block Specify specified block
 -f, --flash Specify file to flash
 -e, --erase Erase chip/block
 -v, --verify Specify file to verify
 -c, --checkempty Check if chip/block is empty

From here i don’t know what to do, because i am not familiar with flashing hardware :wink:
maybe try this in terminal:

python "flash utility" --port=yourPortNumberHere -f --input=yourInputFileHere

Also, please read the documentation from where you download this file,
Hope it will help :+1:

message from @Quercus
If at some time, when asking questions or offering replies, you need to post code within these discussions, please format it correctly for posting, so that important details such as indentation are displayed properly. One way to do this is to place lines of three back ticks before and after the code, as follows:

```
# example code
for i in range(10):
    print("Hello World!")
```

There is also a </> button at the top of the editing window for formatting code, after it has been selected.

my English is not good so thanks for message from @Quercus

Thank you very much @Quercus .after add python to windows path it works

you saved me!

Thanks again :+1: