/* * NetBSD で TC100S からデータを読む。 * これを動かしておいて、 * * 線量率: * % printf '\x0201\x0301' > /dev/dtyU0 * 積算線量: * % printf '\x0202\x0302' > /dev/dtyU0 * スペクトル: * % printf '\x02080\x0338' > /dev/dtyU0 * スペクトルのリセット: * % printf '\x02880\x0330' > /dev/dtyU0 * * とかする。 */ #include #include #include #include #include #include int fd; int readtc(buf, len) void *buf; int len; { int n; n = read(fd, buf, len); if (n < 0) err(0, "read"); if (n == 0) { printf("EOF\n"); exit(0); } return n; } int cksum(buf, len) void *buf; int len; { u_char *p = buf; u_int sum = 0; while (len-- > 0) sum ^= *p++; return sum & 0xff; } void displine(p, n) char *p; int n; { printf("n = %d, ", n); while (n-- > 0) printf("%02x ", *p++); printf("\n"); } int main() { struct termios t; int n; char rbuf[2048]; fd = open("/dev/dtyU0", 2); if (fd < 0) err(0, "open"); tcgetattr(fd, &t); cfmakeraw(&t); t.c_cflag |= CLOCAL; tcsetattr(fd, TCSANOW, &t); tcsetattr(fd, TCSANOW, &t); /* XXX 2回やらないと動かない */ write(fd, "\n\n", 2); tcdrain(fd); sleep(1); tcflush(fd, TCIFLUSH); for (;;) { n = readtc(rbuf, sizeof rbuf); parse_line(rbuf, n); fflush(stdout); } } int parse_line(rbuf, n) char *rbuf; int n; { int i; u_int reply, x; displine(rbuf, n); if (rbuf[0] == 0x15 && n == 1) { /* unknown command */ printf("ERROR\n"); return 0; } if (rbuf[0] == 0x0a && n == 1) { /* unplug or shutdown */ printf("DISCONNECTED\n"); return 0; } if (rbuf[0] == 6 && n == 1) { printf("ACK\n"); return 0; } if (rbuf[0] != 2) { displine(rbuf, n); return -1; } reply = rbuf[1] << 8 | rbuf[2]; if (reply == 0x3031 && n == 14) { /* 線量率 */ sscanf(rbuf + 3, "%x", &x); printf("線量率 %d.%03d μSv/h\n", x / 1000, x % 1000); sscanf(rbuf + 12, "%2x", &x); if (cksum(rbuf + 1, 10) != x) { printf("cksum error\n"); displine(rbuf, n); } return 0; } if (reply == 0x3032 && n == 14) { /* 積算線量 */ sscanf(rbuf + 3, "%x", &x); printf("積算線量 %d.%d μSv/h\n", x / 10, x % 10); sscanf(rbuf + 12, "%2x", &x); if (cksum(rbuf + 1, 10) != x) { printf("cksum error\n"); displine(rbuf, n); } return 0; } if (reply == 0x3037 && n == 3) { time_t t; time(&t); printf("BEGIN spectrum %.20s\n", ctime(&t) + 4); read_spectrum(); return 0; } displine(rbuf, n); return -1; } int read_spectrum() { int total = 0, sum = 0, n, i; u_int x; u_short spectrum_data[512]; u_char rbuf[2048]; while (total < 512) { n = readtc(rbuf, sizeof rbuf); for (i = 0; i < n; i += 4) { if (sscanf(rbuf + i, "%4x", &x) != 1) { printf("data error\n"); return -1; } spectrum_data[total++] = x; } sum ^= cksum(rbuf, n); } sum ^= 0x30; /* BEGIN ... */ sum ^= 0x37; /* endmark and checksum */ n = readtc(rbuf, sizeof rbuf); if (rbuf[0] == 3 && n == 3) { sscanf(rbuf + 1, "%2x", &x); if (x != sum) printf("cksum error\n"); } else { printf("data error\n"); return -1; } for (i = 0; i < 512; i++) { printf("%d ", spectrum_data[i]); if (i % 16 == 15) printf("\n"); } printf("END spectrum\n"); return 0; }