Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The example code pyserial doesn't work on windows, #4178

Open
v479038280 opened this issue Jan 6, 2025 · 3 comments
Open

The example code pyserial doesn't work on windows, #4178

v479038280 opened this issue Jan 6, 2025 · 3 comments

Comments

@v479038280
Copy link

v479038280 commented Jan 6, 2025

Description

pyserial works fine without nicegui,but when running the example on windows,this error occurred ,

"D:\Program Files\python\Lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM1': PermissionError(13, 'Refused', None, 5)

It seems than we must open the port first

@v479038280
Copy link
Author

v479038280 commented Jan 6, 2025

got more error when running on another windows system

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "multiprocessing\spawn.py", line 122, in spawn_main
  File "multiprocessing\spawn.py", line 131, in _main
  File "multiprocessing\spawn.py", line 246, in prepare
  File "multiprocessing\spawn.py", line 297, in _fixup_main_from_path
  File "<frozen runpy>", line 291, in run_path
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "D:\temp\dist_app\app100\serial_data_handle.py", line 9, in <module>
    ser = serial.Serial('COM1',115200,timeout=5)  
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\temp\dist_app\sys_pack\Lib\site-packages\serial\serialwin32.py", line 33, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "D:\temp\dist_app\sys_pack\Lib\site-packages\serial\serialutil.py", line 244, in __init__
    self.open()
  File "D:\temp\dist_app\sys_pack\Lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM1': PermissionError(13, 'Refused。', None, 5)

@v479038280
Copy link
Author

v479038280 commented Jan 6, 2025

This one works fine ,

import serial

from nicegui import app, run, ui

ui.input('Send command').on('keydown.enter', lambda e: (
    ser.write(f'{e.sender.value}\n'.encode()),
    e.sender.set_value(''),
))
log = ui.log()


async def read_loop() -> None:
    global ser
    ser = serial.Serial('COM1',115200,timeout=5)
    while not app.is_stopped and ser.is_open:
        line = await run.io_bound(ser.readline)
        if line:
            log.push(line.decode())
    ser.close()

app.on_startup(read_loop)
ui.run()

@v479038280
Copy link
Author

run.io_bond() is too slow. serial_asyncio is much better .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant