Home > database >  Reading data from the serial port of a STM32 using the Linux terminal
Reading data from the serial port of a STM32 using the Linux terminal

Time:10-13

I'm trying to read and send data with a STM32F429ZI using a RS232-USB cable.

For the hardware, I'm using a RS232 DB9 to TTL MAX3232 converter connected to the UART2 ports PA2 and PA3 of the STM32. I don't know if this is relevant, but I'm using a couple of 10 cm long cables to connect the TX-RX of the RS232 module to the STM32.

Then, I'm trying to use the Linux terminal on Kubuntu to send and read data from the uC. These are the steps I'm following to configure the connection:

  • Using ls -lah /dev/ I look where the RS232-USB is connected. In my case, I can see that it connects to /dev/ttyUSB0.
  • Then I give my user permissions to read and write the USB port using sudo chmod o rw /dev/ttyUSB0.
  • After that, I configure the baud rate of the connection with the stty command. In my case, I'm configuring the STM32 to work at 9600 bauds per second, so stty -F /dev/ttyUSB0 9600. Using stty -F /dev/ttyUSB0 -a, I can see that the speed is actually 9600 bauds per second.

So far so good. I can send data from my computer to the STM32 with no problems. To test this, I'm doing the following:

  • I have a 2x16 LCD display connected to the STM32, where I print the data I send from my computer.
  • To send data from the terminal, I'm just doing echo -n 'a' > /dev/ttyUSB. This seems to work just fine, as I can print the data in the LCD display correctly.
  • I have even tested a program to count the characters on a file and the time the operations takes, in order to corroborate the 9600 baud rate. To do this, I created a file with 9600 characters and I used cat test.txt | tr '\n' '#' > /dev/ttyUSB0 to send the file to the STM32. This is working mostly fine, I usually get the correct answer but other times I don't. Nonetheless, the times it doesn't work are quite low, so I'm assuming it is due to noise.

So, having tested I can actually send data from my computer to the STM32, I tried to do the opposite: to send data from the STM32 to my computer. But this doesn't seem to work, as I can't really read anything in my computer.

I have read in several forums that to read data from the serial on the Linux console, one just has to use the cat command on the device. So, I tried that in several ways but I just couldn't read anything:

  • cat /dev/ttyUSB0 shows nothing and I have to quit with Ctrl C.
  • cat -v /dev/ttyUSB0 shows nothing and I have to quit with Ctrl C.
  • cat < /dev/ttyUSB0 shows nothing and I have to quit with Ctrl C.
  • cat /dev/ttyUSB0 & just shows a number and it finishes.

So, I don't know if I'm just using the cat command wrong or if it is a hardware problem or why I can send data from my computer but not read.

Here is the part of the program (in C) I'm using in the STM32 to read and send data:

while(1)
    {
        if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)
        {
            Data = USART_ReceiveData(USART2);

            while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
                USART_SendData(USART2, Data);
        }
    }

If someones needs it, I can upload the configuration of the USART ports, but I don't know if it will be relevant considering I can read data just fine.

Any help is appreciated.

Thanks in advance.

Edit: here's the current project - https://github.com/AugustoRiedinger/06TP_E02 ; and the project to read data https://github.com/AugustoRiedinger/06TP_E01

CodePudding user response:

Your loop says "as long as it is not possible to send a byte, repeatedly try to send it anyway, as soon as it is possible to send a byte, discard it without sending"

Change:

       while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
            USART_SendData(USART2, Data);

To:

       while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
       USART_SendData(USART2, Data);
  • Related