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

[Bug] stdin inserts extra 0x06 at start of every write command #1052

Closed
Novakasa opened this issue Apr 28, 2023 · 1 comment
Closed

[Bug] stdin inserts extra 0x06 at start of every write command #1052

Novakasa opened this issue Apr 28, 2023 · 1 comment
Assignees
Labels
bug Something isn't working software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)

Comments

@Novakasa
Copy link

Novakasa commented Apr 28, 2023

Describe the bug
It looks like for every PybricksHub.write command, the hub receives an extra 0x06 character in the beginning, using most recent pybricksdev and beta firmware.

Seems like the 0x06 is the Command.WRITE_STDIN const. I would have expected this to be filtered out on the hub side.

Also, PybricksHub.write_line only adds \n as a newline. I'm not sure about this, but I would have expected \r\n since it's also the stdout format. write_line also adds the extra 0x06 at the start

To reproduce
Firmware: Pybricks MicroPython v3.3.0b4 on 2023-04-21; Powered Up City Hub with STM32F030RC
pybricksdev 1.0.0a45

PC program:

import asyncio
from pybricksdev.ble import find_device
from pybricksdev.connections.pybricks import PybricksHub

async def main():
    dev = await find_device()
    print(dev)
    hub = PybricksHub()
    await hub.connect(dev)
    print("connected!")
    await hub.run("ble-server/hub_programs/test_stdin_hub.py", print_output=True, wait=False)
    await asyncio.sleep(0.5)
    print("writing line...")
    await hub.write(b"012345\r\n")
    print("writing line...")
    await hub.write(b"hello\r\n")
    print("writing line...")
    await hub.write(b"\r\n")
    await asyncio.sleep(5.0)
    await hub.disconnect()

    print("OK!")

asyncio.run(main())

hub program:

# NOTE: Run this program with the latest
# firmware provided via https://beta.pybricks.com/

from pybricks.tools import wait

# Standard MicroPython modules
from usys import stdin, stdout
from uselect import poll

keyboard = poll()
keyboard.register(stdin)
buffer = bytearray()

for _ in range(3):
    while True:

        if keyboard.poll(50):
            byte = stdin.buffer.read(1)[0]
            print(byte, end=" ")
            buffer.append(byte)
            if buffer[-2:] == b"\r\n":
                print("\nline received")
                print(str(buffer))
                buffer = bytearray()
                break

wait(100)

Output:

2F:AC:DC:65:90:53: city-blue
connected!
100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 388/388 [00:00<00:00, 1.11kB/s]
writing line...
writing line...
6 48 49 50 51 52 53 13 10
line received
bytearray(b'\x06012345\r\n')
writing line...
6 104 101 108 108 111 13 10
line received
bytearray(b'\x06hello\r\n')
6 13 10 
line received
bytearray(b'\x06\r\n')
OK!

Expected behavior
I am expecting no extra 0x06 at the beginning of stdin for each write command.

@Novakasa Novakasa added the triage Issues that have not been triaged yet label Apr 28, 2023
@dlech dlech added bug Something isn't working software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) and removed triage Issues that have not been triaged yet labels Apr 28, 2023
@dlech
Copy link
Member

dlech commented Apr 28, 2023

for every PybricksHub.write command, the hub receives an extra 0x06 character in the beginning

Thanks for reporting. Not sure how I missed this when testing.

I'm not sure about this, but I would have expected \r\n

I expected the same too, but it caused problems and was changed in pybricks/pybricksdev@a08db5f.

@dlech dlech self-assigned this Apr 28, 2023
dlech added a commit to pybricks/pybricks-micropython that referenced this issue Apr 28, 2023
When implementing the new STDIN_WRITE command, we missed removing
the command byte from the data passed to the bluetooth handler function
which caused 0x06 to be included in the data read from stdin.

Fixes: pybricks/support#1052
Novakasa added a commit to Novakasa/brickrail that referenced this issue Apr 28, 2023
try it with newest 3.3.0b5 firmware as well

added some stdin tests that helped figure out pybricks/support#1052

This will enable new download and run strategy with cached hub programs

might not need frozen module after all, train program runs fine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)
Projects
None yet
Development

No branches or pull requests

2 participants