Tengo en el arduino:
void monitor_reloj()
{
clock.getTime();
segundo = clock.second;
if(segundo != segundo_anterior) {
segundo_anterior = segundo;
Serial.print(clock.dayOfMonth,DEC);
Serial.print(" ");
Serial.println(clock.year,DEC);
Serial.print(" ");
Serial.write(clock.dayOfMonth);
Serial.print(" ");
Serial.write(highByte(clock.year));
Serial.write(lowByte(clock.year));
} //if !segundo_anterior
}
"segundo" y "segundo anterior" son variables generales que en el setup() se ponen a 0.
Tengo un programa en el PC que lee el puerto serie y pone en la pantalla lo que lee:
main()
{
gint leer,cont,a;
gboolean acabo_sn;
gchar buf[128];
gint fd; //el puerto serie
fd = iniciar_puerto_serie("/dev/ttyACM0",115200);
usleep(10000); //No estoy seguro de que esto tenga que ir aquí...
tcflush(fd,TCIFLUSH); //despues de leer se flushea el buffer del serie
for(leer=0;leer < 128;leer++) buf[leer] = 0x00;
acabo_sn = FALSE;
cont = 0;
while(!acabo_sn) {
leer = read(fd,buf,128);
if(leer > 0 AND leer < 128) {
//printf("leer=%d buf->\n\n%s",leer,buf);
for(a=0;a < 5;a++) printf("%c",buf[a]);
printf(" ");
for(a=5;a < 8;a++) printf("%02X ",buf[a]);
for(a=8;a < leer;a++) printf("%d ",buf[a]);
printf("\n");
for(leer=0;leer < 128;leer++) buf[leer] = 0x00;
leer = 0;
tcflush(fd,TCIFLUSH); //despues de leer se flushea el buffer del serie
}
}
}
Lo que ocurre es que la instrucción write ¿falla? al arrancar el ordenador. El programa del PC lee los datos que envía el arduino y los datos enviados por el Serial.print son correctos (el día y el año) pero los que envia el write no.
Esto se arregla arrancando el IDE del arduino y comunicando el IDE con el arduino, enviando un programa o poniendo el monitor serie. Después de hacer esto si que se reciben los datos correctamente.
La config del serie desde mi programa:
//-------------------------------------------------------------------------------------------------
// Bajado de: http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
// y modificado para que se salga del app si falla
// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
// and a baud rate (bps) and connects to that port at that speed and 8N1.
// opens the port in fully raw mode so you can send binary data.
//--------------------------------------------------------------------------------------
gint iniciar_puerto_serie(const gchar *puerto_serie, gint baudios)
{
struct termios toptions;
gint fd;
fd = open(puerto_serie, O_RDWR | O_NOCTTY);// | O_NDELAY);
if(fd == -1)
{
printf("No se puede abrir el puerto: %s\n",puerto_serie);
//exit(1);
}
if(tcgetattr(fd, &toptions) < 0)
{
perror("No se consigue adquirir los atributos del puerto serie\n");
//exit(1);
}
speed_t brate; // = baudios; // let you override switch below if needed
switch(baudios) {
case 4800: brate=B4800; break;
case 9600: brate=B9600; break;
#ifdef B14400
case 14400: brate=B14400; break;
#endif
case 19200: brate=B19200; break;
#ifdef B28800
case 28800: brate=B28800; break;
#endif
case 38400: brate=B38400; break;
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
}
cfsetispeed(&toptions, brate);
cfsetospeed(&toptions, brate);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
toptions.c_cflag &= ~HUPCL; //arduino auto-reset OFF
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 0; //20;
if( tcsetattr(fd, TCSANOW, &toptions) < 0)
{
printf("No se consigue asignar los atributos del puerto serie %s\n",puerto_serie);
//exit(1);
}
return fd;
}
La cuestión es ¿Que hace el IDE para comunicarse con el arduino que NO hago yo?