Thread: Uart communication

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    3

    Uart communication

    Helllo,

    I'm currently trying to implement the communication between a raspberry pi and an flight computer (developed by my school) via UART. For that I connected the RX and TX pins of the pi to test my C-code which will be running on the pi.

    I was able to get the communication going with this code and send a float array via Uart.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdint.h>
    #include <errno.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <termios.h>
     
    int main() {
    
        
    printf("Initialisiere UART ...\n");    
    
    
    int uart0_filestream = -1;
    uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (uart0_filestream == -1) {
        printf("[ERROR] UART open()\n");
    }
    
    
    
    struct termios options;
    tcgetattr(uart0_filestream, &options);
        options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; 
        options.c_iflag = IGNPAR;
        options.c_oflag = 0;
        options.c_lflag = 0;
    tcflush(uart0_filestream, TCIFLUSH);
    tcsetattr(uart0_filestream, TCSANOW, &options);
     
    
    int i;
    int out;
    int rx_length;
    float a[]={4.2351894, 2.48656, 5.98763, 1548.6548632, 54861.6766319, 5, 2.5, 564.2138765411};
    
    
    float rx_buffer[50];
    uint8_t tx_buffer[255];
    uint8_t *p_tx_buffer;
    
    p_tx_buffer = (uint8_t*) &a;
    
    printf("Initialisierung abgeschlossen...\n");
            
            
    
     for(i=0;i < (sizeof (a) / sizeof (float));i++){
                            printf("%f, ", a[i]);
                        }
      printf("\n");
      printf("Sende Daten ...\n");
    
      
     //Bytes senden 
    
     
      
    if (uart0_filestream != -1)    {
        out = write(uart0_filestream, p_tx_buffer, sizeof a); // 
        if (out < 0) {
            printf("[ERROR] UART TX\n");
        } else {
            printf("[STATUS: TX %i Bytes]", out);
                    printf("\n");
        };
    } // if uart0  
      
      
    sleep(1);
     
    // Bytes empfangen
    
    
    
    if (uart0_filestream != -1) {
        rx_length = read(uart0_filestream, (uint8_t*)&rx_buffer, 50);
     
        if (rx_length < 0) {
            printf("[ERROR] UART RX\n");
        } else if (rx_length == 0) {
            printf("[ERROR] UART RX - no data\n");
        } else {
            rx_buffer[rx_length] = '\0';
            printf("[STATUS: RX %i Bytes]", rx_length);
                        for(i = 0; i < 8; i++){
                            printf("%f, ", rx_buffer[i]);
                        }
                        printf("\n");
        } //rx_length check
    } //if uart0
    
    
    close(uart0_filestream);
        
    return 0;
    }
    After that I tried to build functions and call them from the main function. But for some reason the programm gets stuck during the read command.

    Here is the code

    main.c:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include "uart.h"
    
    
    
    int main(){ 
    
        uart_init();
        printf("Initialisierung abgeschlossen \n");
        
    
        float a[]={4.2351894, 2.48656, 5.98763, 1548.6548632, 54861.6766319, 5, 2.5, 564.2138765411};
        float fluco_data[8];
        
        uart_send ((uint8_t*)&a, sizeof(a));
    
        sleep(1);
       
        uart_read((uint8_t*)&fluco_data, sizeof(fluco_data));
        
        uart_close;
    
    }
    and the uart.c:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdint.h>
    #include <errno.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <termios.h>
    
    void uart_init (void){
        
        static int uart0_filestream = -1;
        
        uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
        
        if (uart0_filestream == -1) {
        printf("[ERROR] UART open()\n");
        }
        
        struct termios options;
        tcgetattr(uart0_filestream, &options);
                options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; 
                options.c_iflag = IGNPAR;
                options.c_oflag = 0;
                options.c_lflag = 0;
        tcflush(uart0_filestream, TCIFLUSH);
        tcsetattr(uart0_filestream, TCSANOW, &options);
    }
    
    
    uint16_t uart_send (void *dataOut, uint16_t size){
        
        int uart0_filestream;
        int out;
        
        if (uart0_filestream != -1)    {
        out = write(uart0_filestream, dataOut, size); // 
        if (out < 0) {
            printf("[ERROR] UART TX\n");
        } else {
            printf("[STATUS: TX %i Bytes]", out);
                    printf("\n");
        }        
        } 
    
        return (out);
        printf ("Daten gesendet...\n");
    }
    
    
    uint16_t uart_read (void *dataIn, uint16_t size){
        
        int uart0_filestream;
        uint16_t rx_length;
        
        if (uart0_filestream != -1) {
            
            printf ("Empfange Daten...\n");
        rx_length = read(uart0_filestream, dataIn, size);
             
        if (rx_length < 0) {
            printf("[ERROR] UART RX\n");
        } else if (rx_length == 0) {
            printf("[ERROR] UART RX - no data\n");
        } else {
            //rx_buffer[rx_length] = '\0';
            printf("[STATUS: RX %i Bytes]", rx_length);
                    printf("\n");
        } //rx_length check
        } //if uart0
        
        return(rx_length);
    }
    
    void uart_close (void){
        int uart0_filestream;
        close(uart0_filestream);
    }
    I'm probably doing somehting wrong with the pointers, but I'm learning c for not even a week and I can't find my mistake. I would appreciate it, if someone can help me.

    Cheers
    Michael

    p.s. sorry for my English
    Last edited by Silli; 04-28-2016 at 07:33 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You need to return uart0_filestream from uart_init and pass it into the other functions.

    Currently you are defining uart0_filestream as a local variable in the other functions and using it uninitialized, in essence expecting it to magically have the correct value.

    If you turn up the warning level on your compiler it should warn you about using an uninitialized variable. To get the highest warning level on gcc, use the -W -Wall -pedantic flags. You might want to leave out -pedantic since it's a little fussy, but I like to use it.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    3
    Yep, that worked. Thank you for your help.

    And just for understanding. Would it also be possible to define uart0_filestream as a global variable in the uart_init function so every other function is able to access it?

    Cheers Michael

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Yes, you could of course make it a global variable. But why? If it's just laziness, so you don't have to pass it into the other functions then that's not a good enough reason. The danger of globals is that they can be modified by any function within their scope, so tracking down errors is more difficult, especially in a large program. Therefore, we tend to discourage people who are learning the language from using globals as they can be a bad habit and are almost always avoidable.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    3
    Alright thank you. Nothing to do with laziness I just want to understand what possibilities are available and what is the better to chosse for what reason.

    Thank you for you help again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uart psoc3
    By MacJ in forum C Programming
    Replies: 5
    Last Post: 09-26-2015, 07:34 AM
  2. Uart interrupt triggers after one keystroke
    By munchie146 in forum C Programming
    Replies: 6
    Last Post: 03-30-2014, 05:32 PM
  3. UART Serial Communication
    By VoqionXazr in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-03-2013, 11:50 PM
  4. Getting a string from the UART/COM port
    By thatchergrey in forum C Programming
    Replies: 10
    Last Post: 01-23-2009, 04:18 AM
  5. Bluetooth UART
    By ssharish2005 in forum Tech Board
    Replies: 2
    Last Post: 02-06-2008, 06:31 AM