I use this code .It send data but cant get it.I short circuit Tx & Rx in Raspberry. I use this tutorial:


and copy all things from above link

    #include <stdio.h>
    #include <unistd.h> 
    #include <fcntl.h>
    #include <termios.h>        
    int main(){
    int uart0_filestream = -1;

        uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);      //Open in non blocking read/write mode
        if (uart0_filestream == -1)
            printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");

        struct termios options;
        tcgetattr(uart0_filestream, &options);
        options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;     //<Set baud rate
        options.c_iflag = IGNPAR;
        options.c_oflag = 0;
        options.c_lflag = 0;
        tcflush(uart0_filestream, TCIFLUSH);
        tcsetattr(uart0_filestream, TCSANOW, &options);

    printf("Before TX BYTES  \n");

    //----- TX BYTES -----
        unsigned char  tx_buffer[20];
        unsigned char  *p_tx_buffer;
         p_tx_buffer = &tx_buffer[0];
        *p_tx_buffer++ = 'H';
        *p_tx_buffer++ = 'e';
        *p_tx_buffer++ = 'l';
        *p_tx_buffer++ = 'l';
        *p_tx_buffer++ = 'o';

        if (uart0_filestream != -1)
            int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
            //Filestream, bytes to write, number of bytes to write
            if (count < 0)
                printf("UART TX error\n");
            printf("After send hello \n");

    printf("Befoere recive\n");

    //----- CHECK FOR ANY RX BYTES -----
        if (uart0_filestream != -1)
            // Read up to 255 characters from the port if they are there
            unsigned char rx_buffer[256];
            int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);

            if (rx_length < 0)
                printf("will occur if there are no bytes");
            else if (rx_length == 0)
                printf("No data waiting");
                //Bytes received
                rx_buffer[rx_length] = '\0';
                printf("%i bytes read : %s\n", rx_length, rx_buffer);

         printf("After receive \n");

    return 0;

        And output is:
    Before Send Hello  
    After send Hello
    Befoere recive
    will occur if there are no bytes
    Close UART

When you use a serial link data is transferred bit by bit. Typically 10 bits are used per character, a start bit, eight data bits, and a stop bit.

Each bit takes 1000000/(baud rate) microseconds. At a baud rate of 9600 each bit will take 104 microseconds and one character will take 1042 microseconds, just over a millisecond.

This is a long time in computer terms.

The end result is you send data and then straight away try to read it. The data will not have passed from the output UART to the input UART.

Put a delay between the write and the read.

| improve this answer | |
  • dear i put delay(100) and run it. – m-tech Nov 25 '16 at 18:57
  • dear i put delay(100); between read and write but but buy it sometimes print Hello and sometimes print nothing and sometimes print another string like "MAO","rasper" etc...sometimes print no date waiting. what is wrong? I want stable output and real output – m-tech Nov 25 '16 at 19:00
  • oh my God, I send "Hello" but it Print "login","ttyAMAO" and "raspbe" and " Pi". I think this string are targeted – m-tech Nov 25 '16 at 19:12
  • 1
    You have not disabled the login console on /dev/ttyAMA0. You need to search to find out how to do that for your model of Pi. – joan Nov 25 '16 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.