2 Replies Latest reply on May 29, 2015 5:10 PM by CMata_Intel

    Ubilinux Serial Communication C++

    gerrik84k

      Hi,

       

      i have a probem interfacing a modem with at commands via the serial port of my edison. i'm running ubilinux and i write my code in eclipse using the ide from intel. i tried to set up my own serial class to read and write from the usb port. it already works but i dont understand the whole process in detail.

       

      so i will share a bit of my code and hope someone can answear my questions:

       

      bool MySerial::begin(char *pDevice, speed_t baud, bool fRtsCts, bool fcanon) {
      
      
        struct termios options;
      
      
        /* open the port */
        _fdOut = open(pDevice, O_RDWR | O_NOCTTY | O_NDELAY);
        if(!_fdOut){
        std::cout << "Unable to open port:" << pDevice << std::endl;
        return false;
        }
        fcntl(_fdOut, F_SETFL, 0);
      
      
        /* get the current options */
        tcgetattr(_fdOut, &options);
      
      
        options.c_cflag     |= (CLOCAL | CREAD);
        //Setting Hardware Flow Control
        options.c_cflag |= CRTSCTS;
      
      
        //No parity (8N1):
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;
      
      
        // set raw input
        options.c_lflag     &= ~(ICANON | ECHO | ECHOE | ISIG);
        // set raw output
        options.c_oflag     &= ~OPOST;
        options.c_cc[VMIN]  = 255;
        options.c_cc[VTIME] = 10;
      
      
        /* set the options */
        tcsetattr(_fdOut, TCSANOW, &options);
        return true;
      }
      

       

      these are my adjustments for the serial communication.

       

      char MySerial::sRead() {
        //read from UART
        int length;
        char buffer;
      
      
        length = read(_fdOut, &buffer, 1);
        if (length == 1) {
        return buffer;
        } else {
        std::cout << strerror(errno) << std::endl;
        return -1;
        }
      }
      
      
      size_t MySerial::sWrite(char * pay, int length) {
        int n_written = 0;
        if ((n_written = write(_fdOut, pay, length)) != length) {
        return -1;
        }
        usleep((n_written + 25) * 200);
        ioctl(_fdOut,I_FLUSH,FLUSHRW);
        return n_written;
      }
      

       

      and my read and write.
      my first question would be why i always get ´the write command i send back into my read ? for example i do write "AT\r" and get back

       

      AT

       

      OK

       

      it would be easier to parse only the response of the command. do i have to flush the output buffer ?

       

      thanks in advance

        • 1. Re: Ubilinux Serial Communication C++
          gerrik84k

          So i can give my own post some answears

           

          while i was writing the question the answear appears on my mind... i forgot to deactivate the echo mode of my modem (ATE[<value>]) so now i was able to receive only the response od my modem.

           

          But still i havent fixed a problem when i first power up my modem... i have to write the first at command via screen /dev/ttyUSB0 115200 to the modem before i can run my c++ binary and read and write commands right.

           

          Also i'm still not happy with my serial class, i dont know if my approch is the best way to do this. Mybe someone have some more experience and can give me some hints ?

          • 2. Re: Ubilinux Serial Communication C++
            CMata_Intel

            Hi gerrik84k

             

            Do you have updates in this? I think you should take a look at EmutexLabs they are the experts with Ubilinux so I think you will find helpful ideas in there.

             

            -CMata