Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <string.h> // needed for memset
- #define BPS B9600 //B115200
- #define PORTNAME "/dev/ttyACM0"
- int main(int argc,char** argv)
- {
- struct termios tio;
- struct termios stdio;
- int tty_fd;
- unsigned char c='D';
- memset(&stdio,0,sizeof(stdio));
- stdio.c_iflag=0;
- stdio.c_oflag=0;
- stdio.c_cflag=0;
- stdio.c_lflag=0;
- stdio.c_cc[VMIN]=1;
- stdio.c_cc[VTIME]=0;
- tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
- tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
- memset(&tio,0,sizeof(tio));
- tio.c_iflag=0;
- tio.c_oflag=0;
- tio.c_cflag=CS8|CREAD|CLOCAL; // 8N1
- tio.c_lflag=0;
- tio.c_cc[VMIN]=1;
- tio.c_cc[VTIME]=5;
- tty_fd=open(argc==1 ? PORTNAME :argv[1], O_RDWR | O_NONBLOCK); // O_NONBLOCK might override VMIN and VTIME, so read() may return immediately.
- cfsetospeed(&tio,BPS); // 9600
- cfsetispeed(&tio,BPS);
- tcsetattr(tty_fd,TCSANOW,&tio);
- while (c!='q')
- {
- if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
- if (read(STDIN_FILENO,&c,1)>0) write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
- }
- close(tty_fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement