/* Thyracont smartline vaccum gauge */ #include #include typedef struct thct thct; typedef struct tcg tcg; struct thct { char port[128]; int fd; }; struct tcg { thct *t; char addr[4]; char name[32]; }; thct* newtc(char *p) { thct *t; if((t = mallocz(sizeof(thct), 1)) == nil) sysfatal("%r: newtc mallocz failed"); strcpy(t->port, p); return t; } tcg* newtcg(thct *t, char *ad, char *n) { tcg *g; if( ad == nil || n == nil) return nil; if( (g = mallocz(8, 1)) == nil) sysfatal("%r: newtcg mallocz failed"); g->t = t; strcpy(g->addr, ad); strcpy(g->name, n); return g; } int chksum(char *s) { int t = 0; while(*s) t += *s++; return t % 64 + 64; } /*-=WIP=-*/ char* mkheader(char *ad, char *ac, char *cm, char *ln) { if(ad == 0 | ac == 0 || cm == 0 || ln == 0) return 0; char *buf = mallocz(11, 1); strncpy(buf, ad, 3); strncpy(buf+3, ac, 1); strncpy(buf+4, cm, 2); strncpy(buf+6, ln, 2); buf[8] = (char)chksum(buf); buf[9] = '\r'; return buf; } char* mkold(char *ad, char *cm) { if(ad == 0 | cm == 0) return 0; char *buf = mallocz(11, 1); strncpy(buf, ad, 3); strncpy(buf+3, cm, 1); buf[4] = (char)chksum(buf); buf[5] = '\r'; return buf; } int contc(thct *t) { if( (t->fd = open(t->port, ORDWR)) == 0 ) return 1; else return 0; } int closetc(thct *t) { return close(t->fd); } long sendmsg(tcg *g, char *m) { return write(g->t->fd, m, strlen(m)); } long readmsg(tcg *g, char *b, long n) { return pread(g->t->fd, b, n, 0); } void freetc(thct *t) { free(t); } int verify(char *s) { char b[4] = {0}; int n; memcpy(b, s+4, 2); n = atoi(b); print("accesscode: %d\n", n); return n; } int loopctl(int i) { static int g = 0; switch(i){ case -1: g = 0; return g; case 0: return g; case 1: g = 1; return g; default : return 0; } } int cleanbreak(void *, char *n) /* Note handler */ { if(strcmp(n,"interrupt") != 0) return 0; print("Note received: %s\n",n); loopctl(-1); return 1; } /* test code loop read should fork off */ void loop(tcg *g) { int pid, rl; char *buf; char reply[32]; buf = mkheader(g->addr, "0", "MV", "00"); /*buf = mkold(g->addr, "M");*/ print("mkheader: %s\n", buf); print("checksum: %d\n", buf[8]); /*-=WIP=-*/ loopctl(1); pid = rfork(RFPROC|RFMEM); if (pid < 0) exits("%r"); else if (pid == 0) { while(loopctl(0)){ rl = readmsg(g, reply, 13); if(rl == 13) print("%s\n",reply); memset(reply, 0, 32); } exits("%r"); } else while(loopctl(0)){ sendmsg(g, buf); sleep(1000); } } void main(int argc, char *argv[]) { if(argc < 2){ print("missing serial port.\n "); exits("missing argument"); } int atn; thct *t = newtc(argv[1]); tcg *g; atn = atnotify((*cleanbreak), 2); g = newtcg(t, "001", "chamber"); print("gauge addr: %s, name: %s\n", g->addr, g->name); contc(t); loop(g); closetc(t); freetc(t); print("Done.\n"); exits(nil); }