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?