cancel
Showing results for 
Search instead for 
Did you mean: 

Not receiving data from STM 32 Nucleo

Xx11
Associate

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.read("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("Commands_1", []):
        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:
        ser.write(command)

# 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 = ser.read(response_length)  # Read the specified number of bytes
        return response

if __name__ == "__main__":
    try:
        # 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")
        print("----------------------")
        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":
                print("Exiting...")
                break
            
            # Check if the command exists in the commands dictionary
            if user_input not in commands:
                print(f"Invalid command: {user_input}")
                continue

            # 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")
                else:
                    print("No response received.")

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

    except KeyboardInterrupt:
        print("\nExiting...")
        sys.exit(0)

 

0 REPLIES 0