Eu sempre usei este site para perceber isso.
http://www.easysw.com/~mike/serial/serial.htmlEu tenho este programinha a rodar em Macosx para sincronizar a hora com um Arduino.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/select.h>
#include <fcntl.h>
#define INTERVAL (60*60*12)
#define MINUTE 60
#define TEN_MINUTES 600
#define HALF_HOUR (60 * 30)
#define ONE_HOUR 3600
int conf_port()
{
int fd, n; /* File descriptor for the port */
struct termios iniConfig, myConfig;//serial port
char myStr[21];
fd = open("/dev/cu.usbserial-A400g2JR", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
printf("Erro ao abrir porta");
return fd;
}
else
//ioctl(fd, F_SETFL, FNDELAY);//come back after read if no bits present
fcntl(fd, F_SETFL, 0);
//configure port
tcgetattr(fd, &iniConfig);
tcgetattr(fd, &myConfig);
//set speeds
cfsetispeed(&myConfig, B19200);
cfsetospeed(&myConfig, B19200);
printf("set speeds\n");
//character parity and checking
myConfig.c_cflag &= ~PARENB;
myConfig.c_cflag &= ~CSTOPB;
myConfig.c_cflag &= ~CSIZE;//mask the character size bits
myConfig.c_cflag |= CS8;//8 data bits
printf("set char\n");
myConfig.c_cflag &= ~HUPCL;//no DTR going down on last close
myConfig.c_cflag &= ~CRTSCTS;//no hardware control
printf("set DTR off\n");
myConfig.c_lflag = ICANON;//needs an end of line to return anything. :) Yeay
//set everything
tcsetattr(fd, TCSANOW, &myConfig);
printf("apply changes\n");
return (fd);
}
int main (int argc, const char * argv[]) {
time_t then, now;
struct tm *myTime;
double time_then=0, time_diff = 0;
FILE *Log;
int port, n=0;
char myStr[20];
then = time(NULL);// starting time
myTime = localtime(&then);// making this a bit more presentable
printf("it's %d:%d:%d\n", myTime->tm_hour, myTime->tm_min, myTime->tm_sec);
//open serial port ?!?!?
port = conf_port();
//open log file.
Log = fopen("TempLog.csv", "w");
fprintf(Log, "Living room temperature measurement\n");
fprintf(Log, "%s \n", asctime(myTime));
n = read(port, myStr, 20);
printf("returned %d bytes - %s\n",n, myStr);
/*
while (time_diff <= INTERVAL) {
now = time(NULL);//time now...
myTime = localtime(&now);
time_diff = difftime(now, then);
if (time_diff-time_then >=60 ) //minuto a minuto
{
time_then = time_diff;//so we can come in again.
//ask for temperature.
//wait for reply.
//print reply to file. :)
fprintf(Log, "it's %d,%d,%d\n", myTime->tm_hour, myTime->tm_min, myTime->tm_sec);
//printf("time diff is: %d\n", (int)time_diff);
}
}*/
fclose(Log);//tidy up...
close(port);
return 0;
}
E aparentemente fazer um log da temperatura. Mas deve dar-te uma ideia de como fazer o teu programa para rodar em Linux (ambos os programas assentam em POSIX, logo deve compilar sem erros).