#include #include #include #include #include #include #include #include #include int sd; /* Serial port handle */ struct termios mytty; /* Console settings */ char *errmsg[]={"OK", "Erro de leitura de arquivo","Error", "Sem resposta"}; /* This function is called whenever the program terminates */ void back_to_normal(void) { /* Set console back to normal */ fcntl(STDIN_FILENO, 0, FNDELAY);; } int OpenSerial(char *serial_dev, int baudrate) { char msg[80]; int baudflag; sd=open(serial_dev, O_RDWR | O_NDELAY); if(sd==-1) { sprintf(msg, "\nUnable to open \"%s\"", serial_dev); perror(msg); return -1; } /* Set attributes of the serial interface to raw mode */ if(tcgetattr(sd, &mytty)==-1) { perror(NULL); close(sd); return -1; } mytty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR|IGNCR|ICRNL|IXON); mytty.c_oflag &= ~OPOST; mytty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); mytty.c_cflag &= ~(CSIZE|PARENB); mytty.c_cflag |= CS8; /* Set Baud Rate to 9600 baud */ switch(baudrate) { case 115200: baudflag=B115200; break; case 57600: baudflag=B57600; break; case 38400: baudflag=B38400; break; case 19200: baudflag=B19200; break; case 4800: baudflag=B4800; break; default: baudrate=9600; baudflag=B9600; break; } cfsetispeed(&mytty, baudflag); cfsetospeed(&mytty, baudflag); if(tcsetattr(sd, TCSANOW, &mytty)==-1) { perror(NULL); close(sd); return -1; } fprintf(stderr, "Opened \"%s\" @ %d baud\n" "Press Ctrl-C to exit\n", serial_dev, baudrate); return 0; } /* Retorna o ponto que vem depois do ultimo ponto no nome do arquivo */ char *extencao(char *nome) { char *p; for(p=nome; *nome; nome++) if(*nome=='.') p=nome; return p; } /* Envia arquivo em formato HEX da intel */ void envia_hex(char *nomearq) { FILE *arq; char bloco[256]; int n,ack, erro; fd_set descritores; /* Set of i/o handles */ struct timeval timeout; arq=fopen(nomearq,"rb"); erro=0; if(!arq) { perror(NULL); return; } do{ fgets(bloco,80,arq); write(sd, bloco, strlen(bloco)); FD_ZERO(&descritores); /* Inicializa a lista de handles */ FD_SET(sd, &descritores); timeout.tv_sec=5; timeout.tv_usec=0; select(FD_SETSIZE, &descritores, NULL, NULL, &timeout); ack=bloco[8]; if((n=read(sd,bloco, 80))<0) { erro=3; break; } write(STDIN_FILENO, bloco, n); } while(!feof(arq) && ack=='0'); fprintf(stderr, "%s\n", errmsg[erro]); fclose(arq); } /* Envia arquivo binario */ void envia_arquivo(char *nomearq, int endini) { FILE *arq; char chksum, ack, ff=0xff, bloco[256]; int tamanho, k, nrep, erro; fd_set descritores; /* Set of i/o handles */ struct timeval timeout; arq=fopen(nomearq,"rb"); erro=ack=0; if(!arq) { perror(NULL); return; } do { if(!ack) { tamanho = fread(bloco, 1, 256, arq); nrep=3; } if(!tamanho) { erro=1; break; } write(sd, &ff,1); write(sd, &endini,2); write(sd, &tamanho,2); write(sd, bloco, tamanho); for(k=0, chksum=0; k 0) { switch(estado) { case 0: { tipoDado = (int)letra[0]; estado++; if(tipoDado == 4) { gravaDados(dataFig1, dataFig2); printf("Winnneeerrrrrrrrrrrr!!!!!"); while(1); } break; } case 1: { posCompleto++; if(posCompleto == 2) { unsigned int pos2 = (unsigned int)letra[0]; printf("pos2: %d", pos2); pos = (unsigned int) ((pos&0x0ff) | (((unsigned int)((unsigned int)pos2&0x00ff)<<8))); posCompleto = 0; estado++; } else { pos = (unsigned int)letra[0]; printf("pos: %d", pos); } break; } case 2: { qtBytes = (int)letra[0]; estado++; break; } case 3: { //printf("tipo: %d Pos: %d QT: %d \n", tipoDado, pos, qtBytes); switch(tipoDado) { //FIG1 DATA case 1: { //printf("Receivinbg data Package1\n"); dataFig1[pos*19 + bytesReceived] = letra[0]; bytesReceived++; if(bytesReceived == qtBytes) { estado = 0; bytesReceived = 0; printf("Recebeu um pacote da fig1 -> tipo: %d Pos: %d QT: %d \n", tipoDado, pos, qtBytes); } break; } //FIG2 DATA case 2: { //printf("Receivinbg data Package2\n"); dataFig2[pos*19 + bytesReceived] = letra[0]; bytesReceived++; if(bytesReceived == qtBytes) { estado = 0; bytesReceived = 0; printf("Recebeu um pacote da fig2 -> tipo: %d Pos: %d QT: %d \n", tipoDado, pos, qtBytes); } break; } //COORDINATE case 3: { if(bytesReceived == 0) { printf("X: %d", (int)letra[0]); bytesReceived++; } else { printf("Y: %d", (int)letra[0]); estado = 0; bytesReceived = 0; printf("Recebeu um pacote de coordenada\n"); } break; } } break; } } //printf("%d\n", (int)letra[0]); } // printf("%d", (int)letra[0]);;//write(STDIN_FILENO, letra, n); *letra='\0'; /*while((n=read(STDIN_FILENO, letra,4)) > 0) write(sd, letra, n);*/ } while(*letra!='X'-64); close(sd); return 0; }