How to Transmit Raw Decimal Values (0-255) using UART?
Hi I would like to ask if there is any possible alternative to how I'm transmitting values from my STM32F407 Discovery to my PC which receives and buffers the data using a Python program.
These are the functions I'm using to transmit my values:
void Serial_logi(int val) {
char buffer[10];
itoa(val, buffer, 10);
Serial_log(&buffer[0]);
}
void Serial_log(char *s) {
while (*s) {
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
; // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
The current implementation converts the decimal value which I wish to transmit, val (0-255) into a string of characters, and then transmits the string character by character.
The problem with this implementation is that when I use my Python program to receive the data it does not store it as I want. For example, when I transmit the value 255, instead of storing the first byte as 255, it stores 50 as the first byte because the ASCII value of 2 is 50, and the remaining 2 bytes as 53 each which is the ASCII of 5. The python snippet I use to store and print the values is shown below:
ReadData = sport.read(3)
startbyte = ReadData[0]
nDisLineNo = ReadData[1]
numbyte = ReadData[2]
print(startbyte)
print(nDisLineNo)
print(numbyte)
All in all, I will be using the transmission code to transmit 3 identifiers(ranging from 0-255), followed by pixel values that range from 0-127. I would like to ask if there is any way to modify my functions such that I can transmit raw decimal values ranging from 0-255, such that when I read them on my Python program, it is able to distinguish between each byte that I transmit. Thanks!
I've attached a sample of what my data looks like in the serial monitor below. The first three bytes are identifiers I use to indicate the start of the line, line number, and number of pixels in one line (255,3,176), while the rest are pixel data.

