// file : datalog.c // // A program to upload datalog entries from an RCX brick. // Compile using: gcc datalog.c -o datalog // Set TTY to the serial device you want to use. // An extensively modified version of Kekoa Proudfoot's 'send' program. // D J Rees, 28/3/02 // Copyright (C) 1998, Kekoa Proudfoot. All Rights Reserved. // License to copy, use, and modify this software is granted provided that // this notice is retained in any copies of any part of this software. // The author makes no guarantee that this software will compile or // function correctly. Also, if you use this software, you do so at your // own risk. Some additional documentation is available at: // http://graphics.stanford.edu/~kekoa/rcx/tools.html // // Kekoa Proudfoot // kekoa@graphics.stanford.edu // 10/3/98 #include #include #include #include #include #include #include #include #include #include #define LOGSIZE 500 /* max datalog size - could be bigger */ #define LOGCHUNK 48 /* must be less than 50 */ #define FALSE 0 #define TRUE 1 #define TTY "/dev/ttyS0" /* linux - COM1 */ #define BUFFER_SIZE 4096 #define RETRIES 5 #define TIMEOUT 500000 /* response time-out in microsecs */ #define DUMP_LINE_SIZE 16 #define DUMP_GROUP_SIZE 4 // dump buffer in hex void hexdump(unsigned char *buf, int len) { int i; for (i=0; i= rlen) { fprintf(stderr, "response too long"); return -1; } rbuf[rpos] = vbuf[vpos]; vpos = vpos+2; } if (vbuf[vpos] != ((~vbuf[vpos+1]) & 0xff)) { fprintf(stderr, "invalid response checksum complement"); return -1; } if ((checksum & 0xff) != vbuf[vpos]) { fprintf(stderr, "invalid response checksum"); return -1; } // all OK return rpos; } // try command RETRY times int rcx_trans(int fd, unsigned char *sbuf, int slen, unsigned char *rbuf, int rlen) { int retry, len; for (retry=0; retry= 0) return len; fprintf(stderr, " - retrying\n"); } fprintf(stderr, "command %02x failed\n", sbuf[0]); exit(1); } // main program to upload datalog entries int main (int argc, char **argv) { unsigned char sendbuf[BUFFER_SIZE]; unsigned char recvbuf[BUFFER_SIZE]; int datalog[LOGSIZE]; int fd, sendlen, len, logsize, l, m, n; if (argc == 1) { fprintf(stderr, "Need number of datalog entries to be uploaded\n"); exit(1); } logsize = strtol(argv[1], NULL, 10); if (logsize > LOGSIZE) { fprintf(stderr, "Too many datalog entries requested\n"); exit(1); } // open the serial port fd = rcx_init(TTY); // uncomment the following if the brick program // needs to be run to fill its datalog entries // clear command // sendbuf[0] = 0xd2; // remote command // sendbuf[1] = 0x00; // clear // sendbuf[2] = 0x00; // sendlen = 3; // len = rcx_trans(fd, sendbuf, sendlen, recvbuf, BUFFER_SIZE); // start command // sendbuf[0] = 0xd2; // remote command // sendbuf[1] = 0x02; // start program 1 // sendbuf[2] = 0x00; // sendlen = 3; // len = rcx_trans(fd, sendbuf, sendlen, recvbuf, BUFFER_SIZE); // wait until all datalog entries generated // l = 0; // while (l <= logsize) { // sleep(10); // wait a bit // sendbuf[0] = 0xa4; // sendbuf[1] = 0; // sendbuf[2] = 0; // sendbuf[3] = 1; // sendbuf[4] = 0; // sendlen = 5; // len = rcx_trans(fd, sendbuf, sendlen, recvbuf, BUFFER_SIZE); // l = recvbuf[3]<<8 | recvbuf[2]; // } // upload 'logsize' datalog points, LOGCHUNK points at a time for (l=0; l>8; if (l+LOGCHUNK >= logsize) n = logsize-l+1; else n = LOGCHUNK; sendbuf[3] = n; sendbuf[4] = 0; sendlen = 5; // send it and get any response len = rcx_trans(fd, sendbuf, sendlen, recvbuf, BUFFER_SIZE); hexdump(recvbuf, len); for (m=0; m