Python Forum
How to stop Xmodem after bin file transfer was finished - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: How to stop Xmodem after bin file transfer was finished (/Thread-How-to-stop-Xmodem-after-bin-file-transfer-was-finished)



How to stop Xmodem after bin file transfer was finished - shaya2103 - Nov-27-2019

hello freinds,


I am sending a BIN file successfully to my energy meter through serial port using the code in the bottom. My problem is that Xmodem doesn't stop after all 128 blocks where sent and i have no idea how to break from it and communicate again with my meter.

Please your kind help.
def sendFile():
    fileSize = os.path.getsize('104_new.BIN')
    total = math.ceil(fileSize /128) 
    ser= serialPort.serialport
    ser.flushInput()
    print (ser)

    def getc(size, timeout=0):
        gbytes = ser.read(1)
        #sleep(0.008)
        print(f'Read Byte: {gbytes}')
        return gbytes or None


    def putc(data, timeout=0):
        pbytes = ser.write(data)
        sleep(0.2)
        print(f'Put Byte: {pbytes}')
        return pbytes or None

    modem =XMODEM(getc,putc)
    f = open('104_new.BIN', "rb")
    modem.send(f,retry=50,callback=total)
    f.close()
    ser.close()
    ser.flushOutput()
    senddata_edit.insert(0,'scan\r')
this is the error code i get:

Error:
send error: expected ACK; got None Exception in thread Thread-2: Traceback (most recent call last): File "C:\Users\shay\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner self.run() File "C:\Users\shay\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\PythonTerminal-master\PythonTerminal-master\mainpy2.py", line 211, in sendFile status= modem.send(f,retry=50,timeout=0) File "C:\Users\shay\AppData\Local\Programs\Python\Python37-32\lib\site-packages\xmodem\__init__.py", line 349, in send self.putc(EOT) File "C:\PythonTerminal-master\PythonTerminal-master\mainpy2.py", line 203, in putc pbytes = ser.write(data) File "C:\Users\shay\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 315, in write raise SerialException("WriteFile failed ({!r})".format(ctypes.WinError())) serial.serialutil.SerialException: WriteFile failed (OSError(9, 'The handle is invalid.', None, 6))