/* ISEG SHQ control Copyright I.Tsurin Liverpool University V1.0 (10.04.2011) */ /* http://www.iseg-hv.com/start.php/lang.en/start/hardware/prd_showproduct/id.57 */ /* http://slackware.osuosl.org/slackware-3.3/docs/mini/Serial-Port-Programming */ /* http://www.easysw.com/~mike/serial/serial.html */ /* Acknowledgment: http://www.daniweb.com/software-development/c/code/216534 */ /* Acknowledgment: Jens Preiss (Uni Freiburg) for giving this work a momentum */ #include /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ #define Wsize 32 /* The longest string has 27 characters */ int fd; int InitPort(void) { struct termios options; // fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { fputs("Unable to open port\n", stdout); return 0; } else { tcgetattr(fd, &options); cfsetispeed(&options, B9600); /* Set the input baud rate */ cfsetospeed(&options, B9600); /* Set the output baud rate */ options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */ options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_cflag &= ~CSTOPB; /* Remove 2nd stop bit */ options.c_cflag &= ~CSIZE; /* Clear data length */ options.c_cflag |= CS8; /* Character size = 8 bits */ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* RAW input */ options.c_oflag &= ~OPOST; /* RAW output */ options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */ tcsetattr(fd, TCSANOW, &options); /* set attributes immediately */ return 1; } } int WritePort(char Command[]) { char *BufPtr; int i; BufPtr = Command; for (i=0; i>3); /* read status of switch 2 */ WritePort("T2\r\n"); ReadPort(Word1, Word2); for(i=0;i>3); /* read input parameters */ char Text[11] = ""; char *TPtr, *token; fputs("Enter Ch# U(V) Ramp(V/s): ", stdout); fflush(stdout); sgets(Text, sizeof Text); TPtr = Text; token = strsep(&TPtr, delimiters); Chan = atoi(token); token = strsep(&TPtr, delimiters); Volt = atoi(token); token = strsep(&TPtr, delimiters); Ramp = atoi(token); if (Chan == 0) break; else if ( ((Chan==1)&&(!SW1)) || ((Chan==2)&&(!SW2)) ) printf("Turn on HV switch\n\n"); else if ( ((Chan==1)&&(Volt>Vlim1)) || ((Chan==2)&&(Volt>Vlim2)) || (Chan>2) || (Ramp<2) || (Ramp>255)) fputs("Input parameters out of range\n\n", stdout); else { for (i=0;i<5;i++) AChan[i]=0x00; for (i=0;i<5;i++) AVolt[i]=0x00; for (i=0;i<5;i++) ARamp[i]=0x00; sprintf(AChan, "%d", Chan); sprintf(AVolt, "%d", Volt); sprintf(ARamp, "%d", Ramp); for(i=0;i