Showing results for 
Search instead for 
Did you mean: 

Unable to retrieve data from STM 32 board using pyserial


In this code, when I use the temp command, I always get output as "No response received." That means my python CLI is not receiving any data from the STM 32 board. But when I run the same command on the screen terminal on my mac, I get the data. What could the problem be?

I have ensured that the serial port configuration in my Python code (baud rate, data bits, stop bits, etc.) matches exactly with the configuration used when tested with screen.




import sys
import argparse
import configparser
import yaml
import serial
from rich import print

# Default values
DEFAULT_SERIAL_PORT = "/dev/cu.usbmodem14303"
BAUD_RATE = 115200

# Load configuration from the .ini file (if present)
config = configparser.ConfigParser()"config.ini")

# Override default values with values from the .ini file (if present)
serial_port = config.get("cli_params", "serial_port", fallback=DEFAULT_SERIAL_PORT)

# Parse command-line arguments
parser = argparse.ArgumentParser(description="My CLI Application")
parser.add_argument("--serial-port", type=str, default=serial_port, help="Serial port to use.")
args = parser.parse_args()

# Override values with command-line arguments
serial_port = args.serial_port

# Load commands from the YAML file
def load_commands(filename):
    with open(filename, "r") as file:
        commands_data = yaml.safe_load(file)
    commands = {}
    for cmd_data in commands_data.get("IES_module", []):
        cmd_name = cmd_data["Command"]
        commands[cmd_name] = cmd_data

    return commands

# Function to send a command to the STM32 board via serial
def send_command(serial_port, command):
    with serial.Serial(serial_port, BAUD_RATE, timeout=5) as ser:

# Modify the receive_response function
def receive_response(serial_port, response_length=16):
    with serial.Serial(serial_port, BAUD_RATE, timeout=1) as ser:
        ser.flushInput()  # Clear the input buffer
        response =  # Read the specified number of bytes
        return response

if __name__ == "__main__":
        # Load commands from the YAML file
        commands = load_commands("commands.yaml")

        # Print the available commands for the IES module
        print("Available commands for IES module:")
        print("Command | R/W | Notes")
        for cmd_name, cmd_data in commands.items():
            print(f"{cmd_name} | 0x{cmd_data['ID']:02X} | {cmd_data['R/W']} | {cmd_data['Notes']}")

        while True:
            # Get the command from the user input or use a default command for testing purposes
            user_input = input("Enter command: ").strip().lower()

            if user_input == "exit":
            # Check if the command exists in the commands dictionary
            if user_input not in commands:
                print(f"Invalid command: {user_input}")

            # Translate the command name to the command ID
            command_data = commands[user_input]
            command_id = command_data['ID']

            # Send the command to the STM32 board
            send_command(serial_port, bytes([command_id]))

            if user_input == "temp":
                response = receive_response(serial_port, response_length=16)
                if response:
                    print(f"Received response (raw bytes): {response}")
                    print(f"Response length: {len(response)} bytes")
                    print("No response received.")

                response = receive_response(serial_port)
                if response:
                    print(f"Received response (raw bytes): {response}")
                    print("No response received.")

    except KeyboardInterrupt:




I always get the output as "No response received." when the "temp" command is run.

Associate II

@Xx11 I have the same issue - successful comms with screen, nothing with pyserial. Have you solved this?