Skip to content

[Bug] Edited code on PlatformIO produces print issues, even though Pybricks tools are used. #2791

Open

Description

@toyotafj40

I decided to try PlatformIO as the editor for the current CityHub project, basically to create 7 smallish modules for 1600 lines of code, rather than one massive code block. Definitely worth the effort to switch across and gaining Source Control was also a bonus.

The Pybricks compiler and uploader were set-up and hooked into keyboard shortcuts. It has worked well for the past 3 months.

[Bug]
One bug found today is with the print statement and the use of end=''
It seems the print function print(".", end='') does not print the character immediately but buffers the print instead. The code was printing one "." every 250mS during a BLE scan but the code didn't show any dots until the final print(".") was executed.

I then ran this equivalent test program on the PybricksIDE and all was well.

print("A", end="")
wait(1000)
print("B", end="")
wait(1000)
print("C", end="")
wait(1000)
print("D")

The same code on the PlatformIO fails and the ABCD prints only after the third wait(1000), and not as each print is executed.

This is the compile.BAT file in PlatformIO

@echo off

cd /d "%~dp0micropython"

set MPY="%USERPROFILE%\.platformio\penv\Lib\site-packages\mpy_cross_v6\mpy-cross.exe"

if not exist mpy mkdir mpy

%MPY% main.py -s main.py -o mpy\main.mpy
%MPY% system.py -s system.py -o mpy\system.mpy
%MPY% radio.py -s radio.py -o mpy\radio.mpy
%MPY% ports.py -s ports.py -o mpy\ports.mpy
%MPY% flash.py -s flash.py -o mpy\flash.mpy
%MPY% decoder.py -s decoder.py -o mpy\decoder.mpy
%MPY% core.py -s core.py -o mpy\core.mpy


echo.
echo ==============================
echo MPY FILE SIZES
echo ==============================

dir /-C mpy\*.mpy

exit /b 0

This is the platform.ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:genericSTM32F413CG]
platform = ststm32
board = genericSTM32F413CG
framework = arduino

extensions.json

{
    // See http://go.microsoft.com/fwlink/?LinkId=827846
    // for the documentation about the extensions.json format
    "recommendations": [
        "platformio.platformio-ide"
    ],
    "unwantedRecommendations": [
        "ms-vscode.cpptools-extension-pack"
    ]
}

settings.json

{
    "python-envs.defaultEnvManager": "ms-python.python:system",
    "python.analysis.extraPaths": ["."],
    "python.analysis.stubPath": "./pybricks",
    "python.analysis.typeCheckingMode": "off",
    "python.analysis.diagnosticSeverityOverrides": {
        "reportMissingImports": "none",
        "reportUndefinedVariable": "none",
        "reportUnknownMemberType": "none",
        "reportUnknownArgumentType": "none",
        "reportUnknownParameterType": "none"
    }
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Pybricks upload main.py",
            "type": "shell",
            "command": "C:\\Users\\Peter\\.platformio\\penv\\Scripts\\pybricksdev.exe",
            "args": [
                "run",
                "ble",
                "${workspaceFolder}/micropython/main.py"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": "$python"
        },
        {
            "label": "Pybricks Compile Check",
            "type": "shell",
            "command": "${workspaceFolder}\\pybricks_compile_check.bat",
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "showReuseMessage": false,
                "clear": true,
                "close": false
            }
        }
    ]
}

I have done the usual reboot /new batteries etc but couldn't shake the issue. Hub memory is running around 880 bytes free.

Activity

  1. dlech commented on Aug 21, 2026

    @dlech
    Member

    Does it make a difference if you set the PYTHONUNBUFFERED=1 environment variable when running pybricksdev?

  2. toyotafj40 commented on Aug 21, 2026

    @toyotafj40
    Author

    Hi David, I tried with PYTHONUNBUFFERED=1 but no joy.

    {
        "version": "2.0.1",
    
        "tasks": [
    
            {
                "label": "Pybricks upload main.py",
                "type": "shell",
                "command": "C:\\Users\\Peter\\.platformio\\penv\\Scripts\\pybricksdev.exe",
    
                "args": [
                    "run",
                    "ble",
                    "${workspaceFolder}/micropython/main.py"
                ],
    
                "options": {
                    "env": {
                        "PYTHONUNBUFFERED": "1"
                    }
                },
    
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
    
                "problemMatcher": "$python"
            },
    
         ]
    }
    
    

    I also found a similar 'non-compatibility' bug with the BLERadio and turning OFF the transmitter.

    This works as expected in PybricksIDE.

    from pybricks.hubs import CityHub
    hub = CityHub()
    
    from pybricks.tools import wait
    from pybricks.messaging import BLERadio
    radio = None
    
    radio = BLERadio(broadcast_channel=73, observe_channels=[62])      # channels
    
    data = 73, 48, 03, 02, 9, 8, 7
    radio.broadcast(data)
    wait(1000)
    
    radio = BLERadio(broadcast_channel=None, observe_channels=[62])      # channels
    

    The transmitter stops after sending the data.

    This is the code on the PlatformIO

    def Radio(value=False):
            if (value):
                radio = BLERadio(broadcast_channel=73, observe_channels=[62]) 
            else:
                radio = BLERadio(broadcast_channel=None, observe_channels=[62]) 
    
    #elsewhere in the code....
    
    Radio(True)                              # Transmiter ON
    data = 73, 48, 03, 02, 9, 8, 7
    radio.broadcast(data)
    wait (200)                                 # typically 3 to 4 packets per 100mS
    Radio(False)                             #Transmitter OFF
    
    

    The transmitter does not stop after 200mS.

    Here is the project folder for platformIO, for the original bug. You will need to edit the JSON pathnames after installing PlatformIO.

    print_end_bug.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageIssues that have not been triaged yet

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions