lunes, 23 de febrero de 2009

Serial desde C - termios.h




//Agregar los include string.h, stdio.h, stdlib.h, termios.h (principal) , unistd.h,fcntl.h
//sys/types.h, sys/stat.h, sys/ioctl.h, errno.h

#define Puerto "/dev/ttyUSB0"

int opencom;
struct termios oldtios;
struct termios newtios;
char comando[2];

int main(int argc, char **argv)
{

printf("RS232 Microchip chispi_pic:\n");

opencom=open(Puerto,O_RDWR | O_NOCTTY);
if(opencom<0){
fprintf(stderr, "Error: Could not open %s for reading/writing!\n",Puerto);
exit(-1);
}

//tcgetattr(opencom,&oldtios);

newtios.c_cflag=B1200 | CS8 | CLOCAL | CREAD;
newtios.c_iflag=IGNPAR;
newtios.c_oflag=0;
newtios.c_lflag=0; /*No canonico*/
newtios.c_line=0;
newtios.c_cc[VMIN]=1;
newtios.c_cc[VTIME]=0;

tcflush(opencom, TCIFLUSH);
tcsetattr(opencom,TCSANOW,&newtios);

scanf("%s",comando);

write(opencom,comando,2);
printf("\n");
close(opencom);
}


IRC

#freenode->#usljujuy

Seguidores

Eventos n_n

Tira Ecol Nano,Bilo y Luca