4 Replies Latest reply on Mar 14, 2017 10:14 AM by Intel Corporation

    Using read and write with mraa

    Aravind_Sridhar

      I'm using the mraa uart library to setup a serial connection. However, I need access to the file descriptor once the connection has been established as I need to use the select() function from the standard c library to check if there is any data on the serial port.

      I tried to access the file descriptor via the mraa_uart_context and I'm not able to, for some reason. Is there a way to do this via the mraa library itself? Or is there a way to access the file descriptor once the connection has been established via mraa_uart_init_raw?

      Any help would be appreciated.

      Thanks

      Aravind

       

      PS: this is my gdb log. 'uart' is of type mraa_uart_context. However, when I try to print it's members, nothing comes up.

       

      p uart

      $1 = (mraa_uart_context) 0x805a9a0

      ptype uart

      type = struct _uart {

          <incomplete type>

      } *

        • 1. Re: Using read and write with mraa
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi Aravind, 

          Thank you for contacting us, let us try to help you. 
          I would like to know if you have seen these two threads before https://communities.intel.com/thread/77148 and https://communities.intel.com/thread/57503. In the first one, there are provided some examples to use UART with MRAA, you can check that code, you might find something useful. The other one, explains a way to do it from the Arduino IDE that you might want to try too. 

          Regards, 
          -Pablo M.

          • 2. Re: Using read and write with mraa
            Aravind_Sridhar

            Hi Pablo,

            Thanks for responding. I did look at the first thread. But I think it has some additional links that focus only on the mraa read and write apis. I need to use something like the select() function which would allow me to check if there is any data on the serial port. I cannot find an equivalent function with the mraa library to do that. If you know of something like that, that would be very helpful.

             

            Thanks

            Aravind

            • 3. Re: Using read and write with mraa
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi Aravind,

              I’ve been doing some research on using the select() function to know if there’s any data on the serial port and haven’t found much really, I found a link with some information though https://software.intel.com/en-us/articles/internet-of-things-using-mraa-to-abstract-platform-io-capabilities. There’s an UART example at end that mentions the following “Using read() and write() directly will block if there is nothing available to read. To avoid this blocking behavior, in most cases, you will want to create a function that can check to see if data is available before trying a read. This can be accomplished using the select() system call. See the UPM wt5001 driver for an example. This capability is implemented as a method called dataAvailable()”.
              I’m wondering if you have checked the files where this is, https://github.com/intel-iot-devkit/upm/blob/master/src/wt5001/wt5001.cxx. If not, you can find it attached:                                                                                                                                                                                                                                                                                                                    

              bool WT5001::dataAvailable(unsigned int millis)
               {
                 if (m_ttyFd == -1)
                   return false;
                
                 struct timeval timeout;
                
                 // no waiting
                 timeout.tv_sec = 0;
                 timeout.tv_usec = millis * 1000;
                
                 fd_set readfds;
                
                 FD_ZERO(&readfds);
                
                 FD_SET(m_ttyFd, &readfds);
                
                 if (select(m_ttyFd + 1, &readfds, NULL, NULL, &timeout) > 0)
                   return true;                // data is ready
                 else
                   return false;
               }

              Hope this helps,

              Regards,
              Pablo M.

              • 4. Re: Using read and write with mraa
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hello Aravind, 

                Do you have updates on this case? 

                Regards, 
                Pablo M.