cancel
Showing results for 
Search instead for 
Did you mean: 

How to Transmit Raw Decimal Values (0-255) using UART?

AsifShaik
Associate

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. 

AsifShaik_1-1717399519079.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Andrew Neil
Evangelist III

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.

View solution in original post

5 REPLIES 5
Andrew Neil
Evangelist III

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.

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));
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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.


@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.

Things like RealTerm can show you hex codes

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..