Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- //#include <stdio.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <string.h> // needed for memset
- using namespace std;
- #define BPS B9600 //velocidad puerto
- #define PORTNAME "/dev/ttyACM0" //0 o 1
- /***********************************************************************/
- class control{
- public:
- control (string port);
- ~control ();
- void Run();
- private:
- unsigned char c;//Buffer lectura /escritura tty
- int tty_fd;
- void configurePort(string port);
- char ReadTTY ();//Lee ttyACM0
- bool WriteTTY();//Escribe ttyACM0
- };
- /***********************************************************************/
- char control::ReadTTY (){//Lee ttyACM0
- if (read(tty_fd,&c,1)>0)
- write(STDOUT_FILENO,&c,1);
- return c;
- }
- /***********************************************************************/
- bool control::WriteTTY(){//Escribe ttyACM0
- if (read(STDIN_FILENO,&c,1)>0) {write(tty_fd,&c,1);return true;}
- return false;
- }
- /***********************************************************************/
- /***********************************************************************/
- control::control(string port) {}
- /***********************************************************************/
- control::~control (){
- if (tty_fd!=0) {close(tty_fd);}
- }
- /***********************************************************************/
- void control::configurePort(string port){
- //Para configurar puerto serie
- struct termios tio;
- struct termios stdio;
- 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); //READ 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(port.c_str(), 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);
- }
- /***********************************************************************/
- void control::Run(){
- configurePort (PORTNAME);//dev/ttyACM0 para empezar
- while (true){//bucle principal
- ReadTTY ();
- WriteTTY();
- }
- }
- /***********************************************************************/
- /***********************************************************************/
- /***********************************************************************/
- /***********************************************************************/
- /***********************************************************************/
- int main (){
- control miniCNC(PORTNAME);
- miniCNC.Run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement