I am using a Nucleo-f303re board and I currently have the board setup for USB. Communication is sent/received using a Virtual COM Port connection. The board is being using as a waveform generator. I am sending a frequency value to the STM32 and it is taking that value to be used in a function.
When I run the python code, enter a value, the frequency output is successfully changed and is confirmed via oscilloscope. Also, the program, successfully receives the sent data back. This all works once, the issue is when I enter a second value. The program then hangs after sending with no errors. On the MCU side, the frequency does not change.
import serial
def feeler(ser):
while True:
# Prompt the user to input a frequency value
freq_str = input("Enter frequency value (Hz) or 'q' to quit: ")
if freq_str.lower() == 'q':
break
# Check if the input is a valid integer
try:
freq = int(freq_str)
except ValueError:
print("Invalid input")
continue
# Send the frequency value to the STM32 over serial port
send_freq(ser, freq)
def send_freq(ser, freq):
try:
# Convert the frequency value to a byte string and send it over serial port
freq_bytes = freq.to_bytes(2, byteorder='little')
print(f"Sending frequency: {freq}")
ser.write(freq_bytes)
response = ser.read(2)
print(f"Received response: {response}")
# Convert the received bytes to an integer value
freq_received = int.from_bytes(response, byteorder='little')
# Print the received frequency value
print("Received frequency:", freq_received)
except serial.SerialException:
print('Failed to communicate with the device')
if __name__ == '__main__':
try:
ser = serial.Serial('COM6', 115200, timeout=1)
feeler(ser)
except serial.SerialException:
print('Failed to open serial port')
finally:
ser.close()
In the first test I entered "100" and in the second test I entered "200." Before digging into the MCU code, I wanted to verify if the python code was correct.