2024-06-03 12:28 AM - edited 2024-06-03 12:29 AM
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.
Solved! Go to Solution.
2024-06-03 06:14 AM - edited 2024-06-03 06:14 AM
The UART just transmits bytes - so, yes you can transmit any value from 0-255.
But, when you do that, you will not get human-readable text - it will just be the pure binary byte values.
Therefore the thing which receives it (your Python code) must be expecting pure binary - not text.
2024-06-03 06:14 AM - edited 2024-06-03 06:14 AM
The UART just transmits bytes - so, yes you can transmit any value from 0-255.
But, when you do that, you will not get human-readable text - it will just be the pure binary byte values.
Therefore the thing which receives it (your Python code) must be expecting pure binary - not text.
2024-06-03 09:13 AM
For ASCII, probably want to send a separating character like a space or comma
To send bytes
uint8_t data[] = { 255, 3, 176 };
void UARTSendBinaryData(uint8_t *buffer, int size)
{
while(size--)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
; // Wait for Empty
USART_SendData(USART2, *buffer++); // Send Byte
}
}
UARTSendBinaryData(data, sizeof(data));
2024-06-03 10:49 AM
Thank you so much for this, all this while I was trying to transmitting decimals, I was doing it correctly but assumed it was incorrect because I could not see it using a serial monitor. I only now realized that raw decimals transmitted via UART will come up as gibberish in the terminal, but when I read and display them using Python it verifies that my transmission is accurate.
2024-06-03 02:14 PM
@AsifShaik wrote:I was trying to transmitting decimals
Remember, the UART just transmits bytes - whether you choose to visualise the content of those bytes as decimal, or hex, or binary, or ASCII, or anything else is entirely irrelevant to the UART.
2024-06-03 04:22 PM
Things like RealTerm can show you hex codes