/* * send-raw.c / Markus L. Noga, 1998 * * A program to send raw bytes to an RCX. * * Under IRIX, Linux, and Solaris, you should be able to compile this * program with cc send.c -o send. I don't know about other versions of * Unix, although I'd be interested in hearing about compatibility issues * that you are able to fix. * * Set DEFAULTTTY to the serial device you want to use. * Set the RCXTTY environment variable to override DEFAULTTTY. * * The initialization routines were shamelessly stolen from Kekoa * Proudfoot's send.c. */ /* 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. * * Kekoa Proudfoot * kekoa@graphics.stanford.edu * 10/3/98 */ #include #include #include #include #include #include #include #include #include #include #ifdef LINUX #define DEFAULTTTY "/dev/ttyS0" /* Linux - COM1 */ #else #define DEFAULTTTY "/dev/ttyd2" /* IRIX - second serial port */ #endif #define BUFFERSIZE 4096 int rcx_init(char *tty) { int fd; struct termios ios; if ((fd = open(tty, O_RDWR)) < 0) { perror("open"); exit(1); } if (!isatty(fd)) { close(fd); fprintf(stderr, "%s: not a tty\n", tty); exit(1); } memset(&ios, 0, sizeof(ios)); ios.c_cflag = CREAD | CLOCAL | CS8 | PARENB | PARODD; cfsetispeed(&ios, B2400); cfsetospeed(&ios, B2400); if (tcsetattr(fd, TCSANOW, &ios) == -1) { perror("tcsetattr"); exit(1); } return fd; } void rcx_close(int fd) { close(fd); } int main (int argc, char **argv) { unsigned char sendbuf[BUFFERSIZE]; unsigned char *sp = sendbuf; char *tty; int len; int fd; int i; /* Open the serial port */ if ((tty = getenv("RCX_PORT")) == NULL) tty = DEFAULTTTY; fd = rcx_init(tty); /* Assemble the message */ for (i = 1; i < argc; i++) { *sp++ = strtol(argv[i], NULL, 16); } if(argc<2) { puts("Reading raw RCX port, press Ctrl-C to abort.\n" "Remember, if the green LED in the tower is out, the PC cannot read.\n"); while(1) if(read(fd,&i,1)==1) { printf("'%c'[%-2x] ",i,i); fflush(stdout); } } else { puts("Sending raw bytes to the RCX port.\n"); len = write(fd, sendbuf, sp - sendbuf); // Send it printf("%d byte(s) written.\n",len); } /* Close the tty */ rcx_close(fd); return 0; }