Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <iostream>
- #include <sys/poll.h>
- #include <vector>
- int open(const char *device)
- {
- tcflag_t bitrate = B115200;
- int fd = ::open( device, O_RDWR | O_NOCTTY );
- if ( fd < 0 )
- {
- fprintf( stderr, "Failed to open serial device '%s' (errno: %s)\n", device, strerror(errno) );
- return -1;
- }
- if (::ioctl(fd, TIOCEXCL))
- {
- fprintf( stderr, "Failed to lock serial device '%s' (errno: %s)\n", device, strerror(errno) );
- return -1;
- }
- // Check if device is a terminal device
- if ( !isatty( fd ) )
- {
- fprintf( stderr, "Device '%s' is not a terminal device (errno: %s)!\n", device, strerror(errno) );
- ::close( fd );
- return -1;
- }
- struct termios settings;
- // Set input flags
- settings.c_iflag = IGNBRK // Ignore BREAKS on Input
- | IGNPAR; // No Parity
- // ICRNL: map CR to NL (otherwise a CR input on the other computer will not terminate input)
- // Set output flags
- settings.c_oflag = 0; // Raw output
- // Set controlflags
- settings.c_cflag = bitrate
- | CS8 // 8 bits per byte
- | CSTOPB // Stop bit
- | CREAD // characters may be read
- | CLOCAL; // ignore modem state, local connection
- // Set local flags
- settings.c_lflag = 0; // Other option: ICANON = enable canonical input
- // non-canonical mode: set device to non blocking mode:
- // Set timer
- settings.c_cc[VTIME] = 0; // 0 means timer is not used
- // Set minimum bytes to read
- settings.c_cc[VMIN] = 0; // 1 means wait until at least 1 character is received; 0 means don't wait
- // Now clean the modem line and activate the settings for the port
- tcflush( fd, TCIFLUSH );
- tcsetattr( fd, TCSANOW, &settings );
- return fd;
- }
- int readAll(int fd)
- {
- unsigned char buf[256];
- int res;
- int count = 0;
- while(1)
- {
- res = read( fd, &buf, 100 );
- if (res == 0) return count;
- if (res < 0) return -1;
- count += res;
- //printf("res = %d\n", res);
- }
- }
- void readSleepLoop(int fd1, int fd2)
- {
- printf("start readSleepLoop\n");
- for(int n = 0; n < 10000; n++)
- {
- readAll(fd1);
- readAll(fd2);
- usleep(1);
- }
- }
- void readPollLoop(int fd)
- {
- struct pollfd fds[1];
- fds[0].fd = fd;
- fds[0].events = POLLIN ;
- while(1)
- {
- int pollrc = poll( fds, 1, 1000);
- if (pollrc < 0)
- {
- perror("poll");
- }
- else if( pollrc > 0)
- {
- if( fds[0].revents & POLLIN )
- {
- unsigned char buff[256];
- int res = read(fd, buff, sizeof(buff) );
- if (res > 0)
- {
- //printf("res = %d\n", res);
- }
- }
- }
- }
- }
- void readSelectLoop(int fd)
- {
- fd_set rfds;
- struct timeval tv;
- int retval;
- printf("starting readSelectLoop\n");
- while(1)
- {
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- retval = select(fd + 1, &rfds, NULL, NULL, &tv);
- //printf("retval = %d\n", retval);
- if (retval < 0)
- {
- perror("select()");
- }
- else if (retval > 0)
- {
- unsigned char buff[256];
- int res = read(fd, buff, sizeof(buff) );
- if (res > 0)
- {
- //printf("res = %d\n", res);
- }
- }
- }
- }
- int main(int argc, char* argv[])
- {
- int fd1 = open("/dev/ttyACM0");
- int fd2 = open("/dev/ttyACM1");
- if(fd1 < 0 || fd2 < 0)
- {
- return -1;
- }
- //readSleepLoop(fd1, fd2);
- readPollLoop(fd1);
- //readSelectLoop(fd1);
- close(fd1);
- close(fd2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement