1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <memory.h>
#include <termios.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int fd_port, fd_file;
int max_read; // maximum read line (in 1000 reads)
int read_count; // number of port reads
int total_read; // total bytes received from the port
int bytes_read; // total bytes received (in 1000 reads)
// allow setting vmin and vtime from the command line
int vmin = (argc < 3) ? 1 : atoi(argv[1]);
int vtime = (argc < 3) ? 0 : atoi(argv[2]);
struct termios options;
// open output file
fd_file = open("comm2.bin", O_WRONLY|O_CREAT);
fprintf(stderr, "fd_file %d\n", fd_file);
// open comm 2
fd_port = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NONBLOCK);
fprintf(stderr, "fd_port %d\n", fd_port);
// Configure port settings
fcntl(fd_port, F_SETFL, O_NONBLOCK * 0 );
// Save current port settings so we don't corrupt anything on exit
memset(&options,0,sizeof(options));
tcgetattr(fd_port, &options);
options.c_cc[VMIN] = vmin;
options.c_cc[VTIME] = vtime; // t * 0.1s
fprintf(stderr, "vmin %d, vtime %d\n", options.c_cc[VMIN], options.c_cc[VTIME]);
options.c_iflag |= PARMRK; // 0000010
// set baud rate
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD);
// clear mark/space parity
options.c_cflag &= ~CMSPAR;
// clear character size, stop bit count, and parity setting
options.c_cflag &= ~(CSIZE|CSTOPB|PARENB|PARODD);
// No parity, 1 stop bit (8N1), 8 data bits.
options.c_cflag |= CS8;
// Turn off hardware flow control
// options.c_cflag &= ~CRTSCTS;
// Turn on hardware flow control
options.c_cflag |= CRTSCTS;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tcflush(fd_port, TCIFLUSH);
// Write changes to the port configuration
tcsetattr(fd_port, TCSANOW, &options);
/* Drop RTS to enable receiver */
int flags = TIOCM_RTS;
ioctl( fd_port, TIOCMBIC, &flags );
// initialize counters
max_read = read_count = total_read = bytes_read = 0;
while (total_read < 200000)
{
unsigned char buff[256];
// read bytes
int cnt = read(fd_port, &buff, sizeof(buff));
// save bytes
write(fd_file, &buff, cnt);
// accumulate statistics
if (cnt > 0)
{
read_count += 1;
bytes_read += cnt;
total_read += cnt;
if (cnt > max_read)
max_read = cnt;
// display statistics every 1000 reads
if (read_count == 1000)
{
printf( "max: %3d, tot: %5d\n", max_read, bytes_read );
max_read = read_count = bytes_read = 0;
}
}
}
// close comm port
close(fd_port);
// close output file
close(fd_file);
return 0;
}
|