I have found a few examples and have wrote a program that outputs a message on the serial port waits for a response and then outputs the response to the terminal.
Code: Select all
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
main()
int fd = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
//Could not open Port
perror("open_port: Unable to Open /dev/ttyAMA0 -");
gtk_statusbar_push(GTK_STATUSBAR (statusbar), 0, "Serious Error");
}
int z = write(fd, "Enter Message\n", 14);
if(z<0)
fputs("write fail\n", stderr);
fd = open("/dev/ttyAMA0", O_RDONLY | O_NOCTTY);
if (fd == -1)
{
//Could not open Port
perror("open_port: Unable to Open /dev/ttyAMA0 -");
gtk_statusbar_push(GTK_STATUSBAR (statusbar), 0, "Serious Error");
}
char a[32];
int n = read(fd, a, sizeof(a));
if (n < 0)
fputs("read failed!\n", stderr);
printf("You entered:- %s", a);
}
I saw on a forum that a guy was asking for a similar piece of code, and the top response was a second guy offering to quote the job and charge the first guy for help.
also remember that to connect the serial on the pi to a pc serial you need a max232c and 4 caps. Info on the MAX232c is on the datasheet on the RS website (other component suppliers are available)
In real life i have that routine in a gtk_signal_connect.
I hope this helps someone