I am trying to send and receive a string over UART in Raspberry Pi 3. I've connected the TX and RX pin of the Pi, but when the program runs I get an error:
Read failed: Resource temporarily unavailable
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char ** argv) {
int fd;
// Open the Port. We want read/write, no "controlling tty" status, and open it no matter what state DCD is in
//fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fd = open("/dev/ttyS0", O_RDWR);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS0 - ");
return(-1);
}
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
while(1){
//for(int k=0; k<10; k++){
// Write to the port
int n = write(fd,"hello",6);
if (n < 0) {
perror("Write failed - ");
return -1;
}
// Read up to 255 characters from the port if they are there
char buf[256];
n = read(fd, &buf, 255);
if (n < 0) {
perror("Read failed - ");
return -1;
}
else if (n == 0) {
printf("No data on port\n");
}
else {
buf[n] = '\0';
printf("%i bytes read : %s", n, buf);
}
}
close(fd);
return 0;
}