/*********************************************************************/ /* Author: Anthony Hanson and Mike Hage */ /* Date: April 1st, 2001 */ /* Description: This program initializes the DataQ starter kit */ /* and acquiries data from the 4 Analog channels and the 3 digital */ /* channels. It returns them in a list form {a0 a1 a2 a3 d0 d1 d2} */ /* Once invoked it will return the updated acquired values every */ /* time an "a" char is sent. A "x" sent to the program */ /* will force it to clean up and quit. A "c" will cause data to */ /* to continuously stream. Anything else will be ignored. */ /* */ /* The program is only set up on comport 0 so be sure to hook up the */ /* DATAQ board to this comport, or you can change the port variable */ /* below to suit your needs. */ /* */ /* The Starter kit can be obtained from www.dataq.com for around */ /* $15 US dollars or from www.dataq.com/edn for free. */ /*********************************************************************/ #include #include #include #include #include #include #include #include #include void CloseCOMPORT(); void OpenCOMPORT(); void GetACQUASITION(); int Tx(unsigned char *cr_array); int Rv(unsigned char *cr_array); char COMPORT_file[25]; int COMPORT_fd; char dataq_init_cmd[42] = {0,66,1,0,83,48,0,78,90,0,69,65,0,69,56,0,69,52,0,69,52,0,69,56,0,69,69,0,69,53,0,69,65,0,67,63,0,68,48,0,83,49}; int port=0, time=50, debug=0, dataq_init_cmd_length=42; int main(void) { int i=0,x=0, status=0; int digital0,digital1,digital2; double analog0,analog1,analog2,analog3; unsigned char cur_cmd[3] = "FF", arytmp[26],ary[256], *cur_cmd_ptr, *aryptr, szString[81]; /*Set the commport file variable */ sprintf(COMPORT_file,"/dev/ttyS%i",port); /* Try to open connection to desired comport */ OpenCOMPORT(); printf("Initilizing DATAQ AQUISITON CARD - "); aryptr = ary; for (x=0; x < 9; ) { sprintf(cur_cmd,"%c",dataq_init_cmd[x++]); cur_cmd_ptr = cur_cmd; Tx(cur_cmd_ptr); tcflush(COMPORT_fd, TCIFLUSH); sprintf(cur_cmd,"%c",dataq_init_cmd[x++]); cur_cmd_ptr = cur_cmd; Tx(cur_cmd_ptr); while (!Rv(aryptr)) {}; sprintf(cur_cmd,"%c",dataq_init_cmd[x++]); cur_cmd_ptr = cur_cmd; Tx(cur_cmd_ptr); while (!Rv(aryptr)) {}; } for (i=0;i<10;i++) { while (!Rv(aryptr)) {}; printf("%c",aryptr[0]); } for (;x < dataq_init_cmd_length; ) { sprintf(cur_cmd,"%c",dataq_init_cmd[x++]); cur_cmd_ptr = cur_cmd; Tx(cur_cmd_ptr); tcflush(COMPORT_fd, TCIFLUSH); sprintf(cur_cmd,"%c",dataq_init_cmd[x++]); cur_cmd_ptr = cur_cmd; Tx(cur_cmd_ptr); while (!Rv(aryptr)) {}; sprintf(cur_cmd,"%c",dataq_init_cmd[x++]); cur_cmd_ptr = cur_cmd; Tx(cur_cmd_ptr); while (!Rv(aryptr)) {}; } printf("DONE\n"); printf("\nSend to AQUIRE a data acquisition or to continously acquire data (CTRL-C to exit) or to exit application\n"); printf(">"); while(1) { fgets(szString,80,stdin); strcpy(ary,szString); aryptr = ary; switch (ary[0]) { case 'a': /*Get a data acquisation*/ GetACQUASITION(); break; case 'c': /*Get continous data acquisations*/ while(1) { GetACQUASITION(); printf("\n"); } break; case 'x': /*Exit application gracefully*/ printf("Exiting DATAQ\n"); CloseCOMPORT(); exit(0); } printf("\n>"); } return 0; } void GetACQUASITION() { int digital0,digital1,digital2; double analog0,analog1,analog2,analog3; unsigned char arytmp[26],*aryptr; int i; aryptr = arytmp; /*Get a Data Acquisation*/ tcflush(COMPORT_fd, TCIFLUSH); for (i=0;i<24;i++) { while (!Rv(aryptr)) {}; arytmp[i] = aryptr[0]; //printf("%x ",arytmp[i]); } /* Look for sequence 0,1,1,1,0 in bit 0 of sequencial Dn */ for (i=0;i<18;i++) { if (((arytmp[i] & 0x01)==0x00) && ((arytmp[i+2] & arytmp[i+4] & arytmp[i+6] & 0x01)==0x01) && ((arytmp[i+8] & 0x01)==0x00)) { break; } } if (i == 18) { printf("Failed to find start\n"); exit(1); } i++; if (arytmp[i+1] & 0x02) { digital0 = 1; } else { digital0 = 0; } if (arytmp[i+1] & 0x04) { digital1 = 1; } else { digital1 = 0; } if (arytmp[i+1] & 0x08) { digital2 = 1; } else { digital2 = 0; } analog0 = -10 + (double)(.078 * arytmp[i]); analog1 = -10 + (double)(.078 * arytmp[i+2]); analog2 = -10 + (double)(.078 * arytmp[i+4]); analog3 = -10 + (double)(.078 * arytmp[i+6]); if (debug) { printf("Analog2 is %8.3f\n",analog2); printf("Analog1 is %8.3f\n",analog1); printf("Analog0 is %8.3f\n",analog0); printf("Analog3 is %8.3f\n",analog3); printf("Digital0 is %i\n",digital0); printf("Digital1 is %i\n",digital1); printf("Digital2 is %i\n",digital2); } printf("<%3.3f %3.3f %3.3f %3.3f %i %i %i",analog0,analog1,analog2,analog3,digital0,digital1,digital2); } int Tx(unsigned char *cr_array) { int ret=0, iLength=strlen(cr_array); /* flush the recieve buffer */ tcflush(COMPORT_fd, TCIFLUSH); if (iLength > 0) { ret = write(COMPORT_fd,cr_array,iLength); } else { ret = write(COMPORT_fd,cr_array,1); } if (debug) { printf("Wrote %i characters(%x) to %s\n",ret,*cr_array,COMPORT_file); } if (ret != iLength) { return 1; } else { return 0; } } int Rv(unsigned char *cr_array) { unsigned int iBytes; char cchar; /* Get the next byte in the recieve buffer */ iBytes = read(COMPORT_fd,&cchar,sizeof(char)); cr_array[0]=cchar; //cr_array[1]="\0"; return(iBytes); } void CloseCOMPORT() { if(COMPORT_fd > 0) { close(COMPORT_fd); } } void OpenCOMPORT() { struct termios term; /* Open the comport */ COMPORT_fd = open(COMPORT_file,O_RDWR); if (debug) { printf("COMPORT_FD = %i\n",COMPORT_fd); } /* Exit if could not open the comport */ if(COMPORT_fd < 0) { printf("In OpenCOMPORT(): Could not open %s\n", COMPORT_file); perror("open"); exit(1); } tcgetattr(COMPORT_fd,&term); if (debug) { printf("before:%d\n",term.c_oflag); } term.c_cflag &= ~CSIZE; term.c_cflag |= CS8; term.c_cflag |= HUPCL; term.c_cflag |= CREAD; term.c_cflag &= ~CRTSCTS; term.c_cflag |= IXON; term.c_oflag &= ~OPOST; term.c_iflag = IGNBRK|IGNPAR; term.c_lflag = 0; term.c_cc[VMIN] = 0; term.c_cc[VTIME] = time; /* Time to wait for a response */ /* Turn on the control lines */ cfsetispeed(&term,B4800); cfsetospeed(&term,B4800); tcsetattr(COMPORT_fd,TCSANOW,&term); usleep(25000); /* Turn off the control lines */ cfsetispeed(&term,B0); cfsetospeed(&term,B0); tcsetattr(COMPORT_fd,TCSANOW,&term); /* Turn on the control lines */ cfsetospeed(&term,B4800); tcsetattr(COMPORT_fd,TCSANOW,&term); if (debug) { printf("after:%d\n",term.c_oflag); } }