AmirHmZz / mpbridge

CLI tool to synchronise and manage files on MicroPython running devices
https://pypi.org/project/mpbridge
MIT License
49 stars 5 forks source link

Failed to access port while trying to enter REPL #9

Closed Wind-stormger closed 1 year ago

Wind-stormger commented 1 year ago
~$ mpbridge dev /dev/ttyACM0 /home/wind/temp
 - Syncing files on /dev/ttyACM0 with /home/wind/temp
 - Entering raw repl
 - Syncing files
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit 
 - Syncing files
 ✓ Synced files successfully
 ✓ Hard reset board successfully
 R Entering REPL using mpremote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] to exit this shell
device disconnected

 R Exiting REPL
Traceback (most recent call last):
  File "/home/wind/.local/bin/mpbridge", line 8, in <module>
    sys.exit(main())
  File "/home/wind/.local/lib/python3.10/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/home/wind/.local/lib/python3.10/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/home/wind/.local/lib/python3.10/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/wind/.local/lib/python3.10/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/wind/.local/lib/python3.10/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/home/wind/.local/lib/python3.10/site-packages/mpbridge/shell.py", line 64, in dev
    bridge.start_dev_mode(port, dir_path)
  File "/home/wind/.local/lib/python3.10/site-packages/mpbridge/bridge.py", line 59, in start_dev_mode
    pyb = SweetPyboard(device=port)
  File "/home/wind/.local/lib/python3.10/site-packages/mpremote/pyboard.py", line 292, in __init__
    raise PyboardError("failed to access " + device)
mpremote.pyboard.PyboardError: failed to access /dev/ttyACM0

Tested in Ubuntu 22.04.1 LTS and Windows10, same err.

Wind-stormger commented 1 year ago

Windows10 powershell:

mpbridge dev COM21 D:\temp\temp
 - Syncing files on COM21 with D:/temp/temp
 - Entering raw repl
 - Syncing files
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit
 - Syncing files
 ✓ Synced files successfully
 ✓ Hard reset board successfully
 R Entering REPL using mpremote
Connected to MicroPython at COM21
Use Ctrl-] to exit this shell
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Scripts\mpbridge.exe\__main__.py", line 7, in <module>
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpbridge\shell.py", line 64, in dev
    bridge.start_dev_mode(port, dir_path)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpbridge\bridge.py", line 71, in start_dev_mode
    start_repl(port)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpbridge\bridge.py", line 82, in start_repl
    do_repl(state, argparse_repl().parse_args([]))
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpremote\repl.py", line 79, in do_repl
    do_repl_main_loop(
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpremote\repl.py", line 8, in do_repl_main_loop
    console_in.waitchar(state.pyb.serial)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpremote\console.py", line 99, in waitchar
    while not (self.inWaiting() or pyb_serial.inWaiting()):
                                   ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialutil.py", line 594, in inWaiting
    return self.in_waiting
           ^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialwin32.py", line 259, in in_waiting
    raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError()))
serial.serialutil.SerialException: ClearCommError failed
AmirHmZz commented 1 year ago

I've tested it on esp32 and it worked. Would you please tell me which board you are working with?

AmirHmZz commented 1 year ago

It seems there's a problem with hard reset. What is your opinion about disabling auto hard reset?

Wind-stormger commented 1 year ago

It is the ESP32S3 development board, REPL is output through built-in USB, not UART, this may affect the hard reset.

AmirHmZz commented 1 year ago

I will add an -r option to dev command to enable hard reset and make it disabled by default.

AmirHmZz commented 1 year ago

@Wind-stormger would you please uninstall mpbridge with: pip uninstall mpbridge And install it with this command: pip install git+https://github.com/AmirHmZz/mpbridge.git@disable-hard-reset

Wind-stormger commented 1 year ago
 - Syncing files on COM21 with D:/temp/temp
 - Entering raw repl
 - Syncing files
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit
 - Syncing files
 ✓ Synced files successfully
 - Exiting raw repl
 R Entering REPL using mpremote
failed to access COM21 (it may be in use by another program)
AmirHmZz commented 1 year ago

@Wind-stormger would you please reinstall mpbridge with the same commands again and check if the bug is gone?

Wind-stormger commented 1 year ago

No, still exists.

AmirHmZz commented 1 year ago

What about now? Would you please retry?

Wind-stormger commented 1 year ago
mpbridge dev COM21 D:\temp\temp
 - Syncing files on COM21 with D:/temp/temp
 - Entering raw repl
 - Syncing files
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit
 - Syncing files
 ✓ Synced files successfully
 - Exiting raw repl
 R Entering REPL using mpremote
Connected to MicroPython at COM21
Use Ctrl-] to exit this shell

>>>
>>>
MPY: soft reboot
start
0
1
2
end
MicroPython v1.19.1-782-g699477d12 on 2022-12-20; ESP32S3 module (spiram) with ESP32S3
Type "help()" for more information.
>>>
 R Exiting REPL
 - Entering raw repl
 - Syncing files
 ↑ Putting /main.py              |████████████████████| 100% Complete
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit
 - Syncing files
 ✓ Synced files successfully
 - Exiting raw repl
 R Entering REPL using mpremote
Connected to MicroPython at COM21
Use Ctrl-] to exit this shell

>>>
MPY: soft reboot
start
0
1
2
3
4
5
end
MicroPython v1.19.1-782-g699477d12 on 2022-12-20; ESP32S3 module (spiram) with ESP32S3
Type "help()" for more information.
>>>

It works.

pyb.close()
Wind-stormger commented 1 year ago

Maybe time. sleep (0.3) is unnecessary. After I tried to comment it out, I didn't encounter any problems for the time being. Perhaps conservative or keep it?

AmirHmZz commented 1 year ago

@Wind-stormger Wait! We are not done yet :D Would you please try again? I've enabled auto hard reset again in order to test it.

Wind-stormger commented 1 year ago

In windows10

mpbridge dev COM21 D:\temp\temp
 - Syncing files on COM21 with D:/temp/temp
 - Entering raw repl
 - Syncing files
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit
 - Syncing files
 ✓ Synced files successfully
 ✓ Hard reset board successfully
 R Entering REPL using mpremote
Connected to MicroPython at COM21
Use Ctrl-] to exit this shell
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main   
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Scripts\mpbridge.exe\__main__.py", line 7, in <module>    
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1130, in __call__  
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1055, in main      
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1657, in invoke    
    return _process_result(sub_ctx.command.invoke(sub_ctx)) 
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1404, in invoke    
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 760, in invoke     
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpbridge\shell.py", line 64, in dev     
    bridge.start_dev_mode(port, dir_path)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpbridge\bridge.py", line 73, in start_dev_mode
    start_repl(port)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpbridge\bridge.py", line 84, in start_repl
    do_repl(state, argparse_repl().parse_args([]))
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpremote\repl.py", line 79, in do_repl  
    do_repl_main_loop(
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpremote\repl.py", line 8, in do_repl_main_loop
    console_in.waitchar(state.pyb.serial)
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\mpremote\console.py", line 99, in waitchar
    while not (self.inWaiting() or pyb_serial.inWaiting()): 
                                   ^^^^^^^^^^^^^^^^^^^^^^   
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialutil.py", line 594, in inWaiting
    return self.in_waiting
           ^^^^^^^^^^^^^^^
  File "C:\Users\Wind\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialwin32.py", line 259, in in_waiting
    raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError()))
serial.serialutil.SerialException: ClearCommError failed (PermissionError
AmirHmZz commented 1 year ago

What about now?

Wind-stormger commented 1 year ago

time.sleep(0.5) , not enough to support.

mpbridge dev COM21 D:\temp\temp
 - Syncing files on COM21 with D:/temp/temp
 - Entering raw repl
 - Syncing files
 ✓ Synced files successfully
 ? Press [Enter] to Sync >> Hard Reset >> Enter REPL   Press [Ctrl + C] to exit
 - Syncing files
 ✓ Synced files successfully
 ✓ Hard reset board successfully
 R Entering REPL using mpremote
failed to access COM21 (it may be in use by another program)

time.sleep(0.8) , just fine.

time.sleep(1) , fine.

Perhaps this is related to the USB reaction speed of the PC system?

AmirHmZz commented 1 year ago

Thanks for your cooperation! I will make auto hard reset optional and increase sleep time to 1.2 seconds.

Wind-stormger commented 1 year ago
# pyb.close()
time.sleep(0.8)

It works.