// 466stream.c // Adam MacBeth #include #include #include #include #include #include #include #include #define BAUDRATE B9600 #define DEFAULT_PORT 3 #define DEVICE_PREFIX "/dev/com" int main(int argc, char* argv[]) { int fd,err,portnum; int note; char portname[30]; char hex_note[2]; struct termios oldtio,newtio; FILE *input_file; if(argc < 3) { printf("Usage: 466stream [-portnum] filename\n"); exit(1); } if(argv[1][0] == '-') portnum = atoi(&argv[1][1]); else portnum = DEFAULT_PORT; input_file = fopen(argv[argc-1],"r"); if(input_file == NULL) { printf("File '%s' does not exist.\n", argv[argc-1]); exit(1); } sprintf(portname, "%s%d", DEVICE_PREFIX, portnum); printf("Using COM%d (%s)\n", portnum, portname); fd = open(portname, O_RDWR | O_NOCTTY); if(fd < 0){ printf("Error opening COM%d. Exiting...\n", portnum); exit(1); } tcgetattr(fd,&oldtio); /* save current serial port settings */ bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */ /* BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. CRTSCTS : output hardware flow control (only used if the cable has all necessary lines. See sect. 7 of Serial-HOWTO) CS8 : 8n1 (8bit,no parity,1 stopbit) CLOCAL : local connection, no modem contol CREAD : enable receiving characters */ newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; /* IGNPAR : ignore bytes with parity errors ICRNL : map CR to NL (otherwise a CR input on the other computer will not terminate input) otherwise make device raw (no other input processing) */ newtio.c_iflag = IGNPAR | ICRNL; /* Raw output. */ newtio.c_oflag = 0; newtio.c_lflag = 0; newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); err = write(fd,"s",1); /* signal streaming with 's' */ if(err < 1) { printf("Error writing to port\n"); goto End; } err = read(fd,¬e,1); if(err < 1) { printf("Error reading from port\n"); goto End; } while(1) { hex_note[0] = fgetc(input_file); if(hex_note[0] == EOF) break; hex_note[1] = fgetc(input_file); note = strtol(hex_note,(char **)NULL,16); //printf("Writing to port...%d\n",note); err = write(fd,¬e,1); if(err < 1) { printf("Error writing to port\n"); goto End; } //printf("Reading from port...%d\n",note); err = read(fd,¬e,1); if(err < 1) { printf("Error reading from port\n"); goto End; } } End: ; tcsetattr(fd,TCSANOW,&oldtio); /* restore old serial settings */ exit(1); }