My code so far:
Code: Select all
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char ** argv) {
char buf[256]={},c;
int fd,n,i;
// Open the Port. We want read/write, no "controlling tty" status, and open it no matter what state DCD is in
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyAMA0 - ");
return(-1);
}
struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(fd, TCSANOW, &options);
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
// Write to the port
while (1)
{
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
i = 0;
do
{
n = read(fd, (void*)&c, 1);
if (n > 0)
{
buf[i++] = c;
}
}
while (c != '\r' && i < 255 && n > 0);
buf[i] = '\0';
if (n < 0) {
perror("Read failed - ");
return -1;
} else if (i == 0)
printf("No data on port\n");
else {
printf("%d bytes read : %s", n, buf);
}
// Don't forget to clean up
close(fd);
return 0;
}
I'm a bit stumped as I even tried another UART setup code and got the same output.
screen shot:
https://www.dropbox.com/s/kojrdc2gnqodndf/Error.png?m
