int setupport(char *fdpath){ int fd = open(fdpath, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open dev - "); return 1; } else { fcntl(fd, F_SETFL, 0); //fcntl(fd, F_SETFL, FNDELAY); } if (verbosity) { printf("baud=%d\n", getbaud(fd)); } return fd; }
int Manual(int argc, unsigned char **argv) { /* Serial port initialization */ fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open /dev/ttyS0 - "); return 1; } else { fcntl(fd, F_SETFL, 0); } getbaud(fd); initport(fd); int PVi, k; K= uatof(Gain); /* Things will be drawn more quickly if you always acquire the screen before trying to draw onto it. */ acquire_screen(); Graphics(); /* Call to graphics from a different function to avoid messy code */ RLE_SPRITE *rle; sCmd[0]= 'C'; writeport(fd, sCmd); PVi= 0; k= 100; M=0; while (!key[KEY_ESC]) /* Only pressing ESC can Exit */ { char ValveState; if(ValveState=='O') { sCmd[0]= 'A'; writeport(fd, sCmd); } if(ValveState=='C') { sCmd[0]= 'C'; writeport(fd, sCmd); } M++; /* Check if user needs some help */ if(key[KEY_F1]) Help(); k++; i= 897; Captura = create_sub_bitmap(screen, 72, 350, 898, 368); rle = get_rle_sprite(Captura); destroy_bitmap(Captura); draw_rle_sprite(screen, rle, 71, 350); destroy_rle_sprite(rle); /* This line reads data from the interfase which is the process variable(measured variable) of the system */ fcntl(fd, F_SETFL, FNDELAY); // don't block serial read readport(fd,sResult); PVi= (int) *sResult; PV= PVi; /* if(PVi<=40) { PV= 51; system("festival --tts Messages/Disconnected&"); blit(Disconnected, screen, 0, 0, 70, 290, 887, 52); } */ if(PV<=48) PVi= 51; PV= 1.794117647*(PVi-51); SP= PV; if(key[KEY_RIGHT]) { //fd = close("/dev/parport0"); Simulator(); } if(key[KEY_PGUP]) OP= (OP+46); if(key[KEY_PGDN]) OP= (OP-46); if(key[KEY_UP]) { OP=162; //(OP+3.66); sCmd[0]= 'A'; writeport(fd, sCmd); ValveState='O'; } if(key[KEY_DOWN]) { if(OP>=1) OP= 0;//(OP-3.66); sCmd[0]= 'C'; writeport(fd, sCmd); ValveState='C'; } if(key[KEY_PRTSCR]) { Captura = create_sub_bitmap(screen, 0, 0, 1024, 768); save_bitmap("images/User/Captura.pcx", Captura, pal); destroy_bitmap(Captura); } Timer++; if(OP<=0) OP= 0; // if (PV>=40) // { textprintf_ex(screen, font, 230, 297, BLACK, WHITE, "%3.1f", (PV/368)*100); // Medición textprintf_ex(screen, font, 450, 297, BLACK, WHITE, "%3.1f", (SP/368)*100); // SP textprintf_ex(screen, font, 710, 297, BLACK, WHITE, "%3.1f", (OP/368)*100); // Controlador // } if(k>=100) { k= 0; vline(screen, 967, 351, 717, GRAY); blit(Clean, screen, 0, 0, 968, 350, 2, 368); } int Recorder; Recorder++; if(Recorder>=900) { Recorder= 0; Captura = create_sub_bitmap(screen, 258, 350, 715, 368); } Captura = create_sub_bitmap(screen, 248, 350, 705, 368); if(PV>=362) PV= 365; if(OP>=367) OP= 365; if(PV<=0) PV= 0; if(OP<=0) OP= 1; /* OPi= fixtoi(itofix((OP*0.69234783)/255*100)); sCmd[0]= (unsigned char)OPi+50; sCmd[0]= sCmd[0]+0.00; writeport(fd, sCmd); */ // textprintf_ex(screen, font, 30, 297, BLACK, WHITE, "%i - %s - %3.1f", PVi, sResult, PV); // Medición /* Draw the behaviour of the PV, SP and OP over time */ line(screen, 71+i, 717-PV, 71+i, 717-PVj, RED); PVj= PV; /* Flag for line y2 as a precedent state */ line(screen, 71+i, 717-OP, 71+i, 717-OPj, BLUE); OPj= OP; /* Flag for line y2 as a precedent state */ fprintf(outfile,"%i\t%f\t %f\t %f\n", M, ((PV/368)*100), ((SP/368)*100), ((OP/368)*100)); rest(Delay); } int ScreenWide; RLE_SPRITE *rle0, *rle1; BITMAP *Screen3; Screen3 = load_bitmap("images/Close/Base.pcx", pal); system("mp3blaster /home/mentesuprema/Residencia/sounds/swing2.wav &"); rest(5); rle0= get_rle_sprite(Screen2); rle1= get_rle_sprite(Screen3); for(ScreenWide=0;ScreenWide<=768;ScreenWide=ScreenWide+5) { draw_rle_sprite(screen, rle0, 0, 768); draw_rle_sprite(screen, rle1, 0, -768+ScreenWide); } destroy_rle_sprite(rle0); destroy_rle_sprite(rle1); destroy_bitmap(Screen2); destroy_bitmap(Screen3); destroy_bitmap(Clean); destroy_bitmap(Disconnected); release_screen(); Close(); exit(0); }
int main(int argc, char *argv[]) { speed_t baud = B57600; // default baudrate stdinfd = fileno(stdin); stdoutfd = fileno(stdout); serialfd = -1; erlpipefd = -1; struct termios oldserial, oldstdin; //saveterminal(stdinfd,oldstdin); // check(tcgetattr(stdinfd,&oldstdin) == 0, "Failed to backup stdin"); // if erlang mode is set then redirect stderr to erlerr.txt //read command line arguments check(parsecmdargs(argc, argv) != -1,"Command Line Argument Failure"); if(erlang == TRUE) { freopen("erlerr.txt", "w", stderr); } debug("pid is %d", getpid()); //setup pipe if require doesn't work yet if(pipedebug == 1) // { check(makepipe(ERLPIPE) == 0, "Pipe creation or opening failed for: "); debug("Pipe created:"); debug("Process %d opening erlpipe", getpid()); erlpipefd = open(ERLPIPE, O_WRONLY|O_NONBLOCK); check(erlpipefd > 0, "Erlpipe failed to open pipe: %s\nSetup reader in second terminal\n tail -f erlpipe",ERLPIPE); debug("Erlpipe opened"); /* check(makepipe(ERLPIPEIN) == 0, "Erlpipein creation or opening failed for: "); debug("Pipe created:"); debug("Process %d opening erlpipein", getpid()); erlpipeinfd = open(ERLPIPEIN); check(erlpipeinfd > 0, "Erlpipein: Failed to open pipe: %s\nSetup reader in second terminal\n tail -f erlpipe",ERLPIPEIN); debug("Erlpipe opened"); */ } // save terminal settings //get baud rate from command line arguments baud = getbaud(baudrate); check(baud != B0, "Illegal baud rate"); if(serial == TRUE){ //open serial port serialfd = open(serialport, O_RDWR); check(serialfd > 0,"Can't open serial port\n Try dmesg | grep tty from shell \n"); debug("Opened serial port: %s",serialport); //set serial port to raw mode check(makeraw(serialfd)==0,"Failed to set serial port raw mode"); //set baudrate serial check(setspeed(serialfd,baud)==0,"Failed to set baud rate"); debug("Baud rate set to: %d", baudrate); } //set stdin to raw mode // check(makeraw(stdinfd)==0, "Failed to set stdin to raw mode"); // debug("Not Entered raw mode:"); //send acknowledge to erlang??? /* Watch stdinfd and serialfd if input is available process then output Serial -> Erlang Erlang -> Serial */ /* int numbytes; while(1){ numbytes = read_erlang(erlmesg.mesg); check(numbytes > 0, "Read error from erlang"); debug("Read %d bytes from Erlang", numbytes); write_serial(numbytes, erlmesg.mesg); debug("Wrote %d bytes to serial:", numbytes); } //main loop */ { fd_set readfds; int maxfd; int is, numbytes; FD_ZERO(&readfds); maxfd = (stdinfd > serialfd) ? stdinfd : serialfd; maxfd = (maxfd > erlpipeinfd) ? maxfd : erlpipeinfd; while(1){ if(serial){ FD_SET(serialfd, &readfds); } if(pipedebug){ FD_SET(erlpipeinfd, &readfds); } FD_SET(stdinfd, &readfds); is = select(maxfd+1, &readfds, NULLFDS, NULLFDS, NULLTV); check(is>0,"Select error -"); if (serial && FD_ISSET(serialfd,&readfds)){ // Serial porth ready to read FD_CLR(serialfd,&readfds); numbytes = read(serialfd, erlmesg.mesg, MAXMESG); check(numbytes > 0, "Read error from serial port"); write_erlang(numbytes, erlmesg.mesg); } if (FD_ISSET(stdinfd,&readfds)){ //Stdin-Erlang ready to read FD_CLR(stdinfd, &readfds); numbytes = read_erlang(erlmesg.mesg); check(numbytes > 0, "Read error from erlang"); debug("Read %d bytes from Erlang", numbytes); write_serial(numbytes, erlmesg.mesg); debug("Wrote %d bytes to serial:", numbytes); } // input pipe /* if (pipedebug && (FD_ISSET(erlpipeinfd,&readfds)){ FD_CLR(erlpipeinfd, &readfds); } */ } } //hanldle input from erlang //handle input from serial port // restoreterminal(stdinfd,&oldstdin); // tcsetattr(stdinfd,TCSANOW,&oldstdin); return 0; error: //restoreterminal(stdinfd,&oldstdin); // tcsetattr(stdinfd,TCSANOW,&oldstdin);; debug("Restored terminal"); exit(1); }