/* This a toy program to examine serial port and may be useful to identify your mouse type usage example: xdcom b9600 pn l8 c0 -Kenar- */ #include #include enum{ TIMEOUT = 5000, }; void err(char *s){ fprint(2, "%s\n", s); exits(s); } void usage(void) { print("usage example: com b9600 pn l8 c0\n"); exits("usage"); } int tputs(int fd, char *s){ int n; alarm(TIMEOUT); n = write(fd, s, strlen(s)); alarm(0); if(n < 0){ fprint(2, "%s: timed out in writing %s\n", argv0, s); exits("timeout"); } return n; } void main(int argc, char *argv[]){ char *speed = "b9600"; char *parity = "pn"; char *length = "l8"; char *stop = "s1"; char *comport = "c0"; char *p; int data, ctl, cctl; int n, cpid; char buf[256]; USED(argc); argv0 = *argv++; p = *argv++; while(p) { switch(*p){ case 'b': speed = p; break; case 'p': parity = p; break; case 'l': length = p; break; case 's': stop = p; break; case 'c': comport = p; break; default: usage(); } p = *argv++; } sprint(buf, "/dev/eia%c", comport[1]); data = open(buf, ORDWR); if(data < 0) err("com port not open"); sprint(buf, "/dev/eia%cctl", comport[1]); ctl = open(buf, ORDWR); if(ctl < 0) err("com ctl not open"); /* set com protocol */ tputs(ctl, speed); tputs(ctl, length); tputs(ctl, parity); tputs(ctl, stop); /* turn to raw mode */ cctl = open("/dev/consctl", OWRITE); if(cctl < 0) err("/dev/consctl not open"); tputs(cctl, "rawon"); cpid = fork(); if(cpid == 0){ /* child process */ int i; close(0); while((n = read(data, buf, sizeof(buf))) > 0 ) { for(i = 0; i < n; i++) print("%.02ux ", (uchar)buf[i]); print("\n"); } exits(0); } /* parent */ close(1); while((n = read(0, buf, sizeof(buf))) > 0){ if(buf[0] == 0x7f) break; write(data, buf, n); } postnote(PNPROC, cpid, "terminate"); exits(0); }