/* Auslesedemo unter Verwendung von vc920_960.h/.c für Voltcraft VC920 & VC960 www: http://hardtest.florian-strunk.de */ #include #include #include /*kann weg? */ #include #include #include #include #include #include #include #include #include "vc920_960.h" #define STD_FLG (0 | CS8 | CREAD & ~PARENB & ~CSTOPB | HUPCL) int fd; unsigned int get_time() { struct timeval ti; struct timezone tzp; gettimeofday(&ti,&tzp); return ti.tv_usec+1000000*ti.tv_sec; } /*---------------------------------------------------------------------*/ int open_serial(char* out_dev, int baudrate) { int fd; int bdflag; struct termios tty; int x; char device[]="/dev/ttyUSB0"; fprintf(stderr,"Using serial output to %s, baudrate %i\n", device,baudrate); if (baudrate==19200) bdflag=B19200; else if (baudrate==9600) bdflag=B9600; else if (baudrate==4800) bdflag=B4800; else if (baudrate==2400) bdflag=B2400; else if (baudrate==1200) bdflag=B1200; else if (baudrate==38400) bdflag=B38400; else if (baudrate==57600) bdflag=B57600; else if (baudrate==115200) bdflag=B115200; else if (baudrate==230400) bdflag=B230400; else { fprintf(stderr,"Unsupported baudrate %i\n",baudrate); exit(-1); } fd=open(out_dev,O_RDWR); // ttyS1 = COM2 if (fd==-1) { perror("open failed"); exit(-1); } tcgetattr(fd, &tty); tty.c_iflag = IGNBRK | IGNPAR; tty.c_oflag = 0; tty.c_lflag = 0; tty.c_line = 0; tty.c_cc[VTIME] = 0; tty.c_cc[VMIN] = 1; tty.c_cflag =STD_FLG; cfsetispeed(&tty,bdflag); cfsetospeed(&tty,bdflag); tcsetattr(fd, TCSAFLUSH, &tty); return fd; } /*---------------------------------------------------------------------*/ void close_serial(int fd) { close(fd); } /*---------------------------------------------------------------------*/ void set_rts_dtr(int fd) { int arg; arg=TIOCM_RTS|TIOCM_DTR; ioctl(fd,TIOCMBIS,&arg); arg=TIOCM_RTS; ioctl(fd,TIOCMBIC,&arg); } /*---------------------------------------------------------------------*/ int main(int argc,char **argv) { unsigned char data; unsigned char buffer[100]; char units[20]; int n; int divisor; float it; unsigned int t,tf; char s[10]; struct dvm_data dvmdata; fd=open_serial("/dev/ttyUSB0", 2400); set_rts_dtr(fd); // DTR/RTS setzen t=get_time(NULL); while(1) { n=0; memset(buffer,0,20); while(1) { read(fd,&buffer[n],1); //if ((buffer[n]&0xf0)==0xe0 || (n==11)) // break; if (((n>0) && (buffer[n-1]==0x0D) && (buffer[n]==0x8A)) || (n==11) ) break; n++; } #if 0 // Raw output for(n=0;n<11;n++) printf("%02x ",buffer[n]); #endif tf=(int)((get_time(NULL)-t)/100000.0); set_dvm_data(buffer, &dvmdata); if (dvmdata.overload==0) { //int nachkomma = 0; //nachkomma = dvmdata.divisor / 10; printf("%0.1f %s%0.4f %s%s\n",tf/10.0,dvmdata.sign,dvmdata.digits/dvmdata.divisor,dvmdata.prefix,dvmdata.unit); } else { printf("%0.1f %s %s%s\n",tf/10.0," .OL ",dvmdata.prefix,dvmdata.unit); } fflush(stdout); } }