#define BAUDRATE B9600 #define VERSION "0.3" // Define this ti disable all special characters for colour, lines, ... //#define NO_SPECIAL 1 #define BLACK 0 #define RED 1 #define GREEN 2 #define BROWN 3 #define YELLOW BROWN #define BLUE 4 #define MAGENTA 5 #define CYAN 6 #define WHITE 7 struct termios oldser; int oldline; int sfd; // Must be global (for signal handler) // The really big chars unsigned char dig0[6][9] = { " 000000 ", "00 00", "00 00", "00 00", "00 00", " 000000 "}; unsigned char dig1[6][9] = { " 000 ", " 0000 ", "00 00 ", " 00 ", " 00 ", "00000000"}; unsigned char dig2[6][9] = { "00000000", " 00", "00000000", "00 ", "00 ", "00000000"}; unsigned char dig3[6][9] = { "00000000", " 00", "00000000", " 00", " 00", "00000000"}; unsigned char dig4[6][9] = { "00 ", "00 00 ", "00 00 ", "00000000", " 00 ", " 00 "}; unsigned char dig5[6][9] = { "00000000", "00 ", "00000000", " 00", " 00", "00000000"}; unsigned char dig6[6][9] = { "00 ", "00 ", "00000000", "00 00", "00 00", "00000000"}; unsigned char dig7[6][9] = { "00000000", " 00", " 00 ", " 00 ", " 00 ", " 00 "}; unsigned char dig8[6][9] = { " 000000 ", " 00 00 ", "00000000", "00 00", "00 00", "00000000"}; unsigned char dig9[6][9] = { "00000000", "00 00", "00 00", "00000000", " 00", " 00"}; unsigned char digl[6][9] = { "00 ", "00 ", "00 ", "00 ", "00 ", "00000000"}; unsigned char digb[6][9] = { " ", " ", " ", " ", " ", " "}; unsigned char digd[6][9] = { " ", " ", " ", " ", " 0000 ", " 0000 "}; unsigned char digmi[6][9] = { " ", " ", "0000000 ", "0000000 ", " ", " "}; // The "smaller" chars unsigned char digv[5][7] = { "00 00", "00 00", "00 00", " 0000 ", " 00 "}; unsigned char digm[5][7] = { " ", " ", "000000", "0 00 0", "0 00 0"}; unsigned char digm2[5][7] = { "0 0", "00 00", "0 00 0", "0 0", "0 0"}; unsigned char digk[5][7] = { "00 00", "00 00 ", "0000 ", "00 00 ", "00 00"}; unsigned char digh[5][7] = { "00 00", "00 00", "000000", "00 00", "00 00"}; unsigned char digz[5][7] = { "000000", " 00 ", " 00 ", " 00 ", "000000"}; unsigned char digo[5][7] = { " 0000 ", "00 00", "00 00", "00 00", " 0000 "}; unsigned char diga[5][7] = { " 00 ", " 0000 ", "00 00", "000000", "00 00"}; unsigned char digf[5][7] = { "000000", "00 ", "00000 ", "00 ", "00 "}; unsigned char dign[5][7] = { " ", " ", "00000 ", "0 0", "0 0"}; unsigned char digu[5][7] = { " ", "0 0", "00 0", "00000 ", "0 "}; // Initialize the DMM-connection int init_dmm(char *file) { struct termios serterm; int linemode; int fd, buf; if((fd = open(file, O_RDWR | O_NOCTTY)) < 0) return -1; if(!isatty(fd)) return -2; // No break, no parity, 7 data bits, readable, local connection, baudrate serterm.c_iflag = IGNBRK | IGNPAR; serterm.c_oflag = 0; serterm.c_cflag = CS7 | CSTOPB | CREAD | CLOCAL | BAUDRATE; serterm.c_lflag = 0; serterm.c_cc[VTIME] = 20; serterm.c_cc[VMIN] = 0; if(tcgetattr(fd, &oldser) < 0) return -3; if(tcsetattr(fd, TCSANOW, &serterm) < 0) return -4; if(ioctl(fd, TIOCMGET, &linemode) < 0) return -5; oldline = linemode; linemode &= ~TIOCM_RTS; // Set the correct RTS signal. (Most DMMs have an // optocoupler, wich doen't work otherwise) if(ioctl(fd, TIOCMSET, &linemode) < 0) return -6; // Read out and throw away anything still in // the input buffer (takes timeout) // Otherwise, we get strage results while(read(fd, &buf, 1) > 0) ; return fd; } // Read a data block from DMM int read_dmm(int fd, char *data) { int i; write(fd, "D", 1); // This gets the DMM to send a data block for(i = 0; i < 56; i++) { if(read(fd, &data[i], 1) != 1) { printf("Timeout\n"); return -1; } } return 0; } // Cleanup int close_dmm(int fd) { if(tcsetattr(fd, TCSANOW, &oldser) < 0) // Reset terminal settings return -1; if(ioctl(fd, TIOCMSET, &oldline) < 0) // Reset RTS signal return -2; return (close(fd)); } // Print small digits void mksdigit(char c, unsigned char l) { switch(c) { case 'V': printf("%s", digv[l]); break; case 'm': printf("%s", digm[l]); break; case 'M': printf("%s", digm2[l]); break; case 'k': printf("%s", digk[l]); break; case 'H': case 'h': printf("%s", digh[l]); break; case 'z': printf("%s", digz[l]); break; case 'O': printf("%s", digo[l]); break; case 'A': printf("%s", diga[l]); break; case 'F': printf("%s", digf[l]); break; case 'n': printf("%s", dign[l]); break; case 'u': printf("%s", digu[l]); break; default: printf("%s", digb[l]); } } // Print big digits void mkdigit(char c, unsigned char l) { switch(c) { case 'O': case '0': printf("%s", dig0[l]); break; case '1': printf("%s", dig1[l]); break; case '2': printf("%s", dig2[l]); break; case '3': printf("%s", dig3[l]); break; case '4': printf("%s", dig4[l]); break; case '5': printf("%s", dig5[l]); break; case '6': printf("%s", dig6[l]); break; case '7': printf("%s", dig7[l]); break; case '8': printf("%s", dig8[l]); break; case '9': printf("%s", dig9[l]); break; case '.': printf("%s", digd[l]); break; case '-': printf("%s", digmi[l]); break; case 'L': printf("%s", digl[l]); break; default: printf("%s", digb[l]); } } // Print a string of big chars // size=1 --> The big chars; size=0 --> the smaller chars void bigdig(char *buf, int beg, int end, unsigned char size) { int r, c; for(r = 0; r < 5+size; r++) { for(c = beg; c <= end; c++) { if(size) mkdigit(buf[c], r); else mksdigit(buf[c], r); putchar(' '); if(size) putchar(' '); } putchar('\n'); } } void setcolor(unsigned char color) { #ifndef NO_SPECIAL printf("\033[%dm", 30 + color); #endif } void clrscr(void) { #ifndef NO_SPECIAL printf("\033[2J\033[1;1H"); #endif #ifdef NO_SPECIAL unsigned char c; // A pseudo clrscr for those who have no Linux console. Damn slow. // Results in a nasty flicker, but works. for(c = 0; c < 25; c++) putchar('\n'); #endif } void g1(unsigned char g) { #ifndef NO_SPECIAL if(g) printf("\016"); else printf("\017"); #endif } void highlight(unsigned char h) { #ifndef NO_SPECIAL if(h) printf("\033[1m"); else printf("\033[21m"); #endif } // Because of this own rounding function, we don't need math.h and the math lib int round(float val) { if(10 * ((float) (val - ((long) val))) >= 5) return (((long) val) + 1); else return ((long) val); } // Signal handler, which allows a clean exit void abrt(int sig) { signal(SIGINT, SIG_IGN); close_dmm(sfd); highlight(0); setcolor(WHITE); g1(0); printf("\nExit ...\n"); exit(0); }