int setup_serial_port() { struct termios newtio; char serialPort[32]; ini_gets("sms","usbserial","/dev/ttyUSB0", serialPort, sizeof(serialPort), inifile); info("Using serial device %s", serialPort); g_serial_fd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY); die_if(g_serial_fd < 0, "Unable to open the serial port %s",serialPort); if(flock(g_serial_fd, LOCK_EX | LOCK_NB) == -1) { close(g_serial_fd); die_strerror("Serial port %s in use", serialPort); } bzero(&newtio, sizeof(newtio)); newtio.c_cflag = getBaud() | CRTSCTS | CS8 | CLOCAL | CREAD | O_NDELAY; // uncomment next line to disable hardware handshake newtio.c_cflag &= ~CRTSCTS; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; newtio.c_cc[VTIME] = 1; newtio.c_cc[VMIN] = 0; /* no blocking read */ tcflush(g_serial_fd, TCIOFLUSH); tcsetattr(g_serial_fd, TCSANOW, &newtio); return g_serial_fd; }
void SerialPort::getSerial(int& baud, int& dataBits, int& parity, int& stopBits) { baud = getBaud(); dataBits = getDataBits(); parity = getParity(); stopBits = getStopBits(); }
int main ( void ) { int fd; FILE *of; int totalTime_ms = 0; int bytes, status; struct termios oldtio; unsigned short seqNo = 0; unsigned short diTime = getDotTimeIn_ms(wpm, 0); printf( "Baud %d %f\n", wpm, getBaud(wpm, 0) ); printf( "Dot time %d %f\n", wpm, (float) diTime); printf( "Char rate %d\n", wpm * 5 ); of = fopen( "out.vcd", "w" ); if ( NULL == of ) { fprintf( stderr, "Error opening file!\n" ); exit ( 0 ); } /* Open modem device for reading and writing and not as controlling tty because we don't want to get killed if linenoise sends CTRL-C. */ fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); if ( fd < 0 ) { perror(MODEMDEVICE); exit( -1 ); } tcgetattr( fd, &oldtio ); /* save current serial port settings */ asyncInit( fd ); ioctl(fd, TIOCMGET, &status); status |= TIOCM_DTR; ioctl(fd, TIOCMSET, status); // Ensure the Arduino is not held in reset read( fd, buf, BUFSIZE ); if ( '>' != buf[0] ) { printf( "Wrong start promt!\n" ); exit( 0 ); } // write( fd, "?", 1 ); // Send something to shake up the uart. printf("Init done\n"); int n = 0; for ( int i = 0; i < strlen(msg); i++ ) { int j; if ( ' ' != msg[i] ) { char * p; if( isdigit( msg[i] ) ) p = digits[msg[i] - '0']; else p = alphas[msg[i] - 'A']; for ( j = 0; j < strlen(p); j++ ) { if ('.' == p[j] ) { printf ( "/%d", di_ ); n += sprintf( buf+n, "/%d\n", di_ * diTime ); totalTime_ms += di_ * diTime; } else if ( '-' == p[j] ) { printf ( "/%d", da_ ); n += sprintf( buf+n, "/%d\n", da_ * diTime ); totalTime_ms += da_ * diTime; } else printf ("? " ); if ( j < (strlen(p) - 1)) { printf("\\%d", markSpace_ ); n += sprintf( buf+n, "\\%d\n", markSpace_ * diTime ); totalTime_ms += markSpace_ * diTime; } } if ( msg[i+1] != ' ' ) { printf ("\\%d", charSpace_ ); n += sprintf( buf+n, "\\%d\n", charSpace_ * diTime ); totalTime_ms += charSpace_ * diTime; } } else { printf ("\\%d", wordSpace_ ); n += sprintf( buf+n, "\\%d\n", wordSpace_ * diTime ); totalTime_ms += wordSpace_ * diTime; } } #if 0 char buf[256]; sprintfSeqNo_(buf, 0xa5a5 ); printf("Res: %s\n", buf ); sprintfTime_(buf, 0x66a5a5 ); #endif printf("\nSequence count %d:\n%s\n", seqNo, buf ); printf("Total time %f s\n", totalTime_ms/1000. ); #if 0 ioctl(fd, FIONREAD, &bytes); if (2 != bytes) { printf( "No start!\n"); exit(0); } #endif // for (int k= 0; k<100; k++) bytes = write( fd, buf, strlen(buf) ); printf ( "%ld bytes to write, %d bytes written,\n", strlen(buf), bytes ); tcdrain( fd ); sleep( ( totalTime_ms + 500 ) / 1000 ); ioctl(fd, FIONREAD, &bytes); printf( "Bytes in buffer %d\n", bytes ); /* restore the old port settings */ memset(buf, 0, BUFSIZE ); read( fd, buf, BUFSIZE ); printf ("Read from tty:\n"); printf ("%s\n", buf); writeVcdHead( of ); fprintf ( of, "%s", buf ); fclose( of ); write( fd, "?", 1 ); sleep( 1 ); memset(buf, 0, BUFSIZE ); read( fd, buf, BUFSIZE ); printf ("%s\n", buf); tcsetattr(fd, TCSANOW, &oldtio); return 0; }