I am working on a project where I need to send serial data to an STM32F103 USART using a USB2TTL module.
I want to be able to send numbers and have the stm32 parse the numbers and use them for a certain task. I've accomplished this by having the USARTx_IRQHandler parse the data it receives character by character and append the numbers to a buffer, which gets moved to a variable after the stm receives a delimiting character (In this case i chose 'a' as the delimiting character for no particular reason).
void USART3_IRQHandler(void)
{
/* USER CODE BEGIN USART3_IRQn 0 */
/* USER CODE END USART3_IRQn 0 */
HAL_UART_IRQHandler(&huart3);
/* USER CODE BEGIN USART3_IRQn 1 */
if(RX_Char == 'a'){
newLineCount++;
if(newLineCount == 2){
// Assign the buffered values to the setpoints
baseSetPoint = baseBuffer;
tiltSetPoint = tiltBuffer;
// Reset the buffers
baseBuffer = 0;
tiltBuffer = 0;
newLineCount = 0;
}
}else if(RX_Char >= '0' && RX_Char <= '9'){
// Convert character to digit
int currDigit = RX_Char - '0';
if(newLineCount == 0) {
// Update base buffer
baseBuffer = baseBuffer * 10 + currDigit;
}else{
// Update tilt buffer
tiltBuffer = tiltBuffer * 10 + currDigit;
}
}
//Ignore other characters
/* USER CODE END USART3_IRQn 1 */
}
Now here's the problem, this setup works perfectly when I connect to the STM32 using a Teraterm terminal, but when i send the serial data from a python script, it doesn't seem to work. It seems that the teraterm terminal is setup to follow each character with a CR (I am not quite sure about this, the setup tab has dropdown menus for transmit and receive, both of which have CR selected), so i tried following each character with '\r' in python, and also sending them with small delays because I thought maybe the stm32 didn't have enough time to register the character (Might've been a stretch given im running on 72Mhz but im new to this so im not sure).
ser = serial.Serial(
port='COM12', # Replace with your actual COM port
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
dataString = "2000a400a"
for char in dataString:
ser.write((char + '\r').encode('utf-8'))
time.sleep(0.01)
these are the relevant lines of code.
Question: How do I configure my python script to send the serial data that the stm32 can understand, just as it can understand it when I send it the data from my teraterm terminal, what am I doing wrong?
Note: I can read the data coming from the STM32 in python just fine, it's only the sending that is the problem.
Thanks in advance.