0
   #include <fcntl.h>  
   #include <termios.h> 
   #include <unistd.h>  
   #include <errno.h>  
   #include <cerrno>
   #include <iostream>
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <fstream>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <cstdlib>
   #include <ctime>


   using namespace std;
   typedef struct termios ComSet;

   int main()
   {
   int acm=-1;                                   
   acm=open("/dev/ttyACM0",O_RDWR | O_NOCTTY);
   if(acm == -1)
    {
     cout<<"Error Opening ttyACM0"<<endl;
     exit(1);
    }
    else
    {
      cout<<"Preparing ttyACM0..."<<endl;
      ComSet SerialSettings;
      tcgetattr(acm, &SerialSettings);
      cfsetispeed(&SerialSettings,B115200);
      cfsetospeed(&SerialSettings,B115200);

    /* 8N1 Mode */
    SerialSettings.c_cflag &= ~PARENB;  
    SerialSettings.c_cflag &= ~CSTOPB;   
    SerialSettings.c_cflag &= ~CSIZE;    
    SerialSettings.c_cflag |=  CS8;     

    SerialSettings.c_cflag &= ~CRTSCTS;       
    SerialSettings.c_cflag |= CREAD | CLOCAL; 


    SerialSettings.c_iflag &= ~(IXON | IXOFF | IXANY);          
    SerialSettings.c_iflag &=ICANON;
    SerialSettings.c_iflag &= ~(ECHO | ECHOE | ISIG); 
    SerialSettings.c_oflag &= ~OPOST;                           

 /* Setting Time outs */
    SerialSettings.c_cc[VMIN]  = 1;     
    SerialSettings.c_cc[VTIME] = 0;     

    if((tcsetattr(acm,TCSANOW,&SerialSettings)) != 0)   
    {
        cout<< " ERROR ! in Setting attributes"<<endl;

    }
    else
    {
        cout<< "=======ttyACM0 Setting====="<<endl;
        cout<<"BaudRate = 115200 StopBits = 1 Parity = none"<<endl;
        cout<<"Reading ttyACM0... "<<endl;
        char read_buffer[1024];                                
        bzero(read_buffer,1024);
        int read_bytes=0;                                      
        while(1)
         {

                write(acm,"Hello World !",13);                                              
                read_bytes=read(acm,&read_buffer,1024);                                    
                if(read_bytes>0)
                {
                    cout<<read_buffer;
                    bzero(read_buffer,1024);
                }
                else
                {
                    cout<<"No-Data"<<endl;
                }

                tcflush(acm, TCOFLUSH);
                tcflush(acm, TCIFLUSH);
         }
    }



  close(acm); /* Close the serial port */

}

return 0;
}

Why is this code not working ? i m trying to receive same message from the arduino but it will not work.it completely ignores write.i have also found that it works if i use cin.get() before write.

it works if i want to read from arduino only. Here is my arduino code

  String Input;
  boolean done=false;

  void setup() 
  {
     Serial.begin(115200);
  }

  void loop() 
  {
     Serial.println(GetSerial());
      Input="";
  }

  String GetSerial()
  {  
      done=false;
      Input="";
      while(!done)
  {  
      while(Serial.available()>0)
      {
         Input=Serial.readStringUntil('\n');  //Block Reading
         done=true; 
      }
   } 
   return Input;
  }

i want to receive whatever is sent on serial port as it is with as much less delay as possible

  • 3
    What happens when you run the program? What is the actual output of the program? What output did you expect? Have you tried debugging and stepping through it? And what is `ymain`? – Some programmer dude Aug 05 '16 at 11:43
  • i want it to echo same message back to serial port i.e in this case Hello World ! but its not returning anything.sorry about the ymain i have been trying other ways to solve this issue thats why i rename the main in this – Ashish Kullu Aug 05 '16 at 11:47
  • What is your proof that something is actually being received on the serial port, so the problem must be with your program? – Sam Varshavchik Aug 05 '16 at 12:30
  • thats what i said earlier it works but ignore write without cin.get() . "so the problem must be with your program?" what problem that what i m asking – Ashish Kullu Aug 05 '16 at 12:38
  • how to use tcflush properly ? and i have already tried with '\n' it didnt work – Ashish Kullu Aug 05 '16 at 16:23
  • " There is no code that does what you claim "it" is supposed to do ??" i have already written such code in c# and java which dose write to serial and wait for reply same goes for python.and secondally should i just remove the tcflush ?? – Ashish Kullu Aug 05 '16 at 19:05

0 Answers0