There's lots of faffing about to do it in C (69 lines in my sample to read stuff from an Arduino).
Code: Select all
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define BAUDRATE B9600
#define MODEMDEVICE "/dev/arduino"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
void main() {
int ardin, timout, c, res;
struct termios oldtio,newtio;
char buf[255];
char *filename = "/home/pi/ardtime.html";
char ht_head[300] = "<html><HEAD><script type=\"text/javascript\">\r\nfunction reFresh() {\r\nlocation.reload(true)\r\n}window.setInterval(\"reFresh()\",1000);\r\n</script></HEAD><body><h1>";
char ht_foot[100] = "</h1>Autoreload - once a second</body></html>";
ardin = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (ardin <0) {
perror(MODEMDEVICE);
exit(-1);
}
if ((timout = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
perror("Cannot open output file\n");
exit(1);
}
close(timout);
tcgetattr(ardin,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
tcflush(ardin, TCIFLUSH);
tcsetattr(ardin,TCSANOW,&newtio);
while (STOP==FALSE) {
res = read(ardin,buf,255);
buf[res]=0; /* set end of string, so we can printf */
if (res > 1) {
// printf("%s", buf);
timout = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
write(timout,ht_head,strlen(ht_head));
write(timout,buf,res);
write(timout,ht_foot,strlen(ht_foot));
close(timout);
}
if (buf[0]=='z') STOP=TRUE;
}
tcsetattr(ardin,TCSANOW,&oldtio); /* restore the old port settings */
}