#include #include #include #include #include #include #include #include #include #include #define SERIAL "/dev/ttyS0" #define OK 0 #define ERROR (-1) static int fd = -1; static char *device = NULL; int serial_init(char *); int serial_finish(void); int serial_out(int); int main( int argc, char *argv[] ) { char *p; if ( argc != 2 ) { fprintf(stderr, "usage: %s string\n", argv[0] ); exit (1); } for ( p = argv[1]; *p != '\0'; p++ ) { serial_init(SERIAL); serial_out( *p ); serial_finish(); if ( *p < ' ' ) { usleep( 1000 ); } } return (0); } int serial_init(char *dev) { struct termios termio; if ( fd >= 0 ) { return (OK); } if ( device != NULL) { device = (char *)malloc( strlen(dev) + 1 ); strcpy( device, dev ); } if ( (fd = open(device, O_WRONLY)) < 0 ) { perror("open()"); return (ERROR); } bzero(&termio, sizeof(termio)); termio.c_cflag = B9600 | CS8 | CLOCAL; termio.c_iflag = IGNPAR; tcflush(fd, TCIFLUSH); if ( tcsetattr(fd, TCSANOW, &termio) < 0 ) { perror("tcsetattr()"); return (ERROR); } return (OK); } int serial_finish() { if ( fd < 0 ) { return (ERROR); } if ( close(fd) < 0 ) { perror("close()"); return (ERROR); } fd = -1; return (OK); } int serial_out(int i) { char c; c = i; if ( write( fd, &c, sizeof(char)) != sizeof(char) ) { perror("write()"); return (ERROR); } return (OK); } /* EOF **************************************************************/