0

I want to use serial port of my raspberry (version 3) in order to communicate with the PC. It is my code, borrowed from http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port/using-the-uart:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>                     //Used for UART
#include <fcntl.h>                      //Used for UART
#include <termios.h>            //Used for UART

int main (int argc, char* argv[])
{
        int fd, retv;
        struct termios options;
        char str[10]="hello";

        if (argc != 2)
        {
                fprintf (stderr, "Usage: %s /dev/ttyx\n", argv[0]);
                exit (1);
        }

        fd = open (argv[1], O_RDWR | O_NOCTTY);
        if (fd < 0)
        {
                perror ("Serial file couldn't be opend !");
                return -1;
        }

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

        retv = write(fd, str, strlen(str));
        printf ("Sent bytes: %d\n", retv);

        return 0;
}

When i set console-on-serial in raspi-config, I can see serial output. So RPI's serial port is OK. I have turn off serial console via raspi-config due to the serial access conflict, but i can't send anything. Above code work fine when i use some usb-to-serial device. Example:

pi@RPI:~$./test /dev/ttyUSB0 
Sent bytes: 5

However, I can't send any byte on ttyAMA0:

pi@RPI:~$./test /dev/ttyAMA0 
Sent bytes: 0

Why it doesn't work with ttyAMA0?

SAP
  • 23
  • 5

1 Answers1

2

If you are using Raspberry Pi 3 model, it's a bit more complex.There are two UARTs. The ttyAMA0 is used for Bluetooth. There is another uart to the ttyS0, which is connected to the GPIO pins.

Check this link for more informations.
http://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3/

Related questions
How do I make serial work on the Raspberry Pi3
UART problems on my Pi3

dakridas
  • 61
  • 5