Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. error : QProcessPrivate::createPipe: Cannot create pipe 0x2ed031ac: Too many open files
QtWS25 Last Chance

error : QProcessPrivate::createPipe: Cannot create pipe 0x2ed031ac: Too many open files

Scheduled Pinned Locked Moved Solved Mobile and Embedded
qtcreator
27 Posts 2 Posters 10.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sachin786
    wrote on last edited by
    #1

    Hello everyone,

    i am working on ARM based board interfacing with fingerprint module project and working on qt creator for coding my program is running properly but after 5 hours my finger print module stop working and when i see in serial port terminal it show the

    error :QProcessPrivate::createPipe: Cannot create pipe 0x2ed031ac: Too many open files

    anyone know what it means??

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      That your system is opening a lot of files and is not closing them. Are you creating lots of QProcess in your application ? If so, are you also delete them properly ?

      By the way, what version of Qt are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sachin786
        wrote on last edited by
        #3

        qt 4.8.6

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What about my other questions ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sachin786
            wrote on last edited by sachin786
            #5

            sorry for late reply...

            I am having a separate thread in which i am executing process as follow in while loop.

            I am just calling slot in thread. And expect the called script perform some task.

            this is only QProcess i am using currently.

            ///// php.h //////
            
            #ifndef php_THREAD_H
            #define php_THREAD_H
            
            #include <QObject>
            #include <QDateTime>
            #include <QFile>
            #include <QtGui>
            #include <QtCore>
            
            class php : public QObject
            {
                Q_OBJECT
            public Q_SLOTS:
                void  phpScripts();
            };
            
            #endif // php_THREAD_H
            
            
            ///// php.cpp  ///
            
            
            #include <QObject>
            #include <QDateTime>
            #include <QFile>
            #include <QtGui>
            #include <QtCore>
            #include "php.h"
            #include <QProcess>
            #include <QtNetwork/QHostAddress>
            #include <QtNetwork/QNetworkInterface>
            #include <QFileInfo>
            
            
            void php::phpScripts(){
                while(1){
                    QProcess::execute("sh /usr/test/shells/sysConfig.sh");
            
                }
            }
            
            //// main.cpp ///
            
            #include "qtquick1applicationviewer.h"
            #include <QApplication>
            #include <QtDeclarative>
            #include <QDeclarativeContext>
            #include <QDeclarativeView>
            #include <QtDebug>
            #include "ApplicationData.h"
            #include "php.h"
            
            
            int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);
                //ApplicationData data;
            
                QtQuick1ApplicationViewer viewer;
                FileHandling data1;
                viewer.addImportPath(QLatin1String("modules"));
                viewer.setOrientation(QtQuick1ApplicationViewer::ScreenOrientationAuto);
                viewer.setMainQmlFile(QLatin1String("qrc:/home.qml"));
               
            
               //php Thread
               QThread *t3 = new QThread;
               php *phpThread = new php;
               phpThread->moveToThread(t3);
               QObject::connect(t3,SIGNAL(started()), phpThread, SLOT(phpScripts()));
               qDebug() << "hello from GUI thread " << app.thread()->currentThreadId();
            
               t3->start();//php thread
            
               app.exec();
               return 0;
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              In your slot you have an infinite loop that is creating indefinitely QProcess objects that execute the sysConfig.sh script but you never check whether something went wrong with it.

              What does that script do ? How do you know it executed successfully ? Does it close everything it has opened ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sachin786
                wrote on last edited by sachin786
                #7
                1. these script is use for calling other php files

                2. i have put the debug line for know my script is executed successfully in terminal

                3. and everything is closed properly

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Also from the PHP files ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sachin786
                    wrote on last edited by sachin786
                    #9

                    yes i have check also in php files that are called in my code everything is ok but still get the same issue after some time get the above error again n agian and finger print module stop working...

                    my doubt :: if something was missing about closing file then error must appeared early, above error i get after 4-5 hours application run.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      How are you accessing that reader ?

                      What does your PHP scripts do ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sachin786
                        wrote on last edited by
                        #11

                        i am using CAMA SM20 finger print module communicating via UART communication.
                        in my application there are 2 diffrent threads for php and fingerprint module.
                        fingerprint module thread does some stuff like collecting data and storing in file.
                        and php thread include some php script to read text file and uploading to server using webserice. (the text file php read is actually created and update by fingerprint module on run time)

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Can you share the code you use for the serial port communication ?

                          One thing you can do is use lsof to see what files are being constantly opened and by what.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sachin786
                            wrote on last edited by
                            #13
                            [root@Atishay build-QT_FP_RFID-QT_4_8_6-Debug]# lsof
                            1	/bin/busybox	/dev/console
                            1	/bin/busybox	/dev/console
                            1	/bin/busybox	/dev/console
                            1512	/sbin/udevd	/dev/null
                            1512	/sbin/udevd	/dev/null
                            1512	/sbin/udevd	/dev/null
                            1512	/sbin/udevd	anon_inode:inotify
                            1512	/sbin/udevd	socket:[1031]
                            1512	/sbin/udevd	socket:[1032]
                            1512	/sbin/udevd	pipe:[1033]
                            1512	/sbin/udevd	pipe:[1033]
                            3246	/usr/sbin/inetd	/dev/null
                            3246	/usr/sbin/inetd	/dev/null
                            3246	/usr/sbin/inetd	/dev/null
                            3246	/usr/sbin/inetd	socket:[306]
                            3246	/usr/sbin/inetd	socket:[307]
                            3246	/usr/sbin/inetd	socket:[308]
                            3246	/usr/sbin/inetd	socket:[309]
                            3246	/usr/sbin/inetd	socket:[310]
                            3246	/usr/sbin/inetd	socket:[311]
                            3246	/usr/sbin/inetd	socket:[312]
                            3249	/usr/sbin/boa	/dev/null
                            3249	/usr/sbin/boa	/var/log/boa/access_log
                            3249	/usr/sbin/boa	/var/log/boa/error_log
                            3249	/usr/sbin/boa	socket:[1246]
                            3344	/bin/bash	/dev/console
                            3344	/bin/bash	/dev/console
                            3344	/bin/bash	/dev/console
                            3344	/bin/bash	/etc/rc.d/rc_mxc.S
                            3348	/bin/bash	/dev/ttymxc0
                            3348	/bin/bash	/dev/ttymxc0
                            3348	/bin/bash	/dev/ttymxc0
                            3348	/bin/bash	/dev/ttymxc0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9936]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9936]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9938]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9938]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	socket:[9939]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/fb0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/tty0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/input/mouse0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/input/event1
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/tty0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	socket:[9941]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/tmp/qtembedded-0/fonts/dejavu_sans_20_50.qsf
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/tmp/qtembedded-0/fonts/dejavu_sans_40_50.qsf
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[13498]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[13498]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[15454]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[15454]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[14820]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[14820]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/spidev0.0
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[13499]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[13499]
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            8343	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                            

                            above is the log for lsof command when my application running...

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sachin786
                              wrote on last edited by sachin786
                              #14
                              **before application running output=>**
                              [root@Atishay root]# lsof
                              1	/bin/busybox	/dev/console
                              1	/bin/busybox	/dev/console
                              1	/bin/busybox	/dev/console
                              1498	/sbin/udevd	/dev/null
                              1498	/sbin/udevd	/dev/null
                              1498	/sbin/udevd	/dev/null
                              1498	/sbin/udevd	anon_inode:inotify
                              1498	/sbin/udevd	socket:[1525]
                              1498	/sbin/udevd	socket:[1526]
                              1498	/sbin/udevd	pipe:[1527]
                              1498	/sbin/udevd	pipe:[1527]
                              2653	/usr/sbin/inetd	/dev/null
                              2653	/usr/sbin/inetd	/dev/null
                              2653	/usr/sbin/inetd	/dev/null
                              2653	/usr/sbin/inetd	socket:[2860]
                              2653	/usr/sbin/inetd	socket:[2861]
                              2653	/usr/sbin/inetd	socket:[2862]
                              2653	/usr/sbin/inetd	socket:[2863]
                              2653	/usr/sbin/inetd	socket:[2864]
                              2653	/usr/sbin/inetd	socket:[2865]
                              2653	/usr/sbin/inetd	socket:[2866]
                              2656	/usr/sbin/boa	/dev/null
                              2656	/usr/sbin/boa	/var/log/boa/access_log
                              2656	/usr/sbin/boa	/var/log/boa/error_log
                              2656	/usr/sbin/boa	socket:[3527]
                              2750	/bin/bash	/dev/console
                              2750	/bin/bash	/dev/console
                              2750	/bin/bash	/dev/console
                              2750	/bin/bash	/etc/rc.d/rc_mxc.S
                              2753	/bin/bash	/dev/ttymxc0
                              2753	/bin/bash	/dev/ttymxc0
                              2753	/bin/bash	/dev/ttymxc0
                              2753	/bin/bash	/dev/ttymxc0
                              
                              
                              **after application running output=>**
                              
                              1	/bin/busybox	/dev/console
                              1	/bin/busybox	/dev/console
                              1	/bin/busybox	/dev/console
                              1498	/sbin/udevd	/dev/null
                              1498	/sbin/udevd	/dev/null
                              1498	/sbin/udevd	/dev/null
                              1498	/sbin/udevd	anon_inode:inotify
                              1498	/sbin/udevd	socket:[1525]
                              1498	/sbin/udevd	socket:[1526]
                              1498	/sbin/udevd	pipe:[1527]
                              1498	/sbin/udevd	pipe:[1527]
                              2653	/usr/sbin/inetd	/dev/null
                              2653	/usr/sbin/inetd	/dev/null
                              2653	/usr/sbin/inetd	/dev/null
                              2653	/usr/sbin/inetd	socket:[2860]
                              2653	/usr/sbin/inetd	socket:[2861]
                              2653	/usr/sbin/inetd	socket:[2862]
                              2653	/usr/sbin/inetd	socket:[2863]
                              2653	/usr/sbin/inetd	socket:[2864]
                              2653	/usr/sbin/inetd	socket:[2865]
                              2653	/usr/sbin/inetd	socket:[2866]
                              2656	/usr/sbin/boa	/dev/null
                              2656	/usr/sbin/boa	/var/log/boa/access_log
                              2656	/usr/sbin/boa	/var/log/boa/error_log
                              2656	/usr/sbin/boa	socket:[3527]
                              2750	/bin/bash	/dev/console
                              2750	/bin/bash	/dev/console
                              2750	/bin/bash	/dev/console
                              2750	/bin/bash	/etc/rc.d/rc_mxc.S
                              2753	/bin/bash	/dev/ttymxc0
                              2753	/bin/bash	/dev/ttymxc0
                              2753	/bin/bash	/dev/ttymxc0
                              2753	/bin/bash	/dev/ttymxc0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9913]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9913]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9915]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[9915]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	socket:[9916]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/fb0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/tty0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/input/mouse0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/input/event1
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/tty0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	socket:[9918]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/tmp/qtembedded-0/fonts/dejavu_sans_20_50.qsf
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/tmp/qtembedded-0/fonts/dejavu_sans_40_50.qsf
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[12299]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[12299]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[11299]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[11299]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[10579]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[10579]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/spidev0.0
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[12300]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	pipe:[12300]
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/tmp/qtembedded-0/fonts/dejavu_sans_14_50.qsf
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/tmp/qtembedded-0/fonts/dejavu_sans_15_75.qsf
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              5604	/usr/test/build-QT_FP_RFID-QT_4_8_6-Debug/QT_FP_RFID	/dev/ttymxc2
                              
                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Is it me or does your application open /dev/ttymxc2 very often ?

                                How do you open that device file ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                S 1 Reply Last reply
                                1
                                • SGaistS SGaist

                                  Is it me or does your application open /dev/ttymxc2 very often ?

                                  How do you open that device file ?

                                  S Offline
                                  S Offline
                                  sachin786
                                  wrote on last edited by
                                  #16
                                  No, in my application i called following function multiple time.
                                  fpSetup();//  /dev/ttymxc2 open oftenly
                                  
                                  /******************************************** fpSetup Starts***************************************************
                                  //Description     : Setting Up UART peripheral
                                  //Input Parameter : void
                                  //Return Type     : void
                                  ************************************************************************************************************/
                                  void fpSetup(void)
                                  {
                                      //printf("\nSetting Up Fingerprint module...\n");
                                      N=12;
                                      SERIAL_ATTR_ST serial_attr;
                                      int ret;
                                      int channel;
                                      channel=2;				// UART channel select as per need.. anas
                                      fp=UART4_Port_RDWR(channel);		// open UART port.
                                      ret=UART4_Attr_set(fp,serial_attr);
                                      if (RET_SUCCESS == ret)
                                      {
                                          //printf("\nBaud Rate Set Successful.\n");
                                      }
                                  
                                  }
                                  
                                  
                                  
                                  /************************************************************************************************************
                                  File: serial_test.h (contains all function declaration and definations for interfacing and enabling serial interface )
                                  Author   : RnD Team @ Atishay Infotech
                                  Date     : 18th Jan 2016,Monday,10:57AM
                                  Platform : IMX6 board(Cortex A9) + Linux-3.0.35
                                  Toolchain: arm-fsl-linux-gnueabi-gcc-4.6.2
                                  *************************************************************************************************************/
                                  #include <stdio.h>
                                  #include <sys/types.h>
                                  #include <sys/stat.h>
                                  #include <fcntl.h>
                                  #include <unistd.h>
                                  #include <string.h>
                                  #include <termios.h>
                                  #include <errno.h>
                                  #include <sys/ioctl.h>
                                  #include <pthread.h>
                                  #define BUF_SIZE		512
                                  /* *************************** variables ******************************/
                                  	char read_buf[BUF_SIZE];
                                  	unsigned char N;
                                  	//char confirmation_data[BUFFER];
                                  /* *************************** Structures ******************************/
                                  
                                  typedef struct {
                                      char parity;
                                  	unsigned int baud;
                                      unsigned int databits;
                                      unsigned int stopbits;
                                  } SERIAL_ATTR_ST;
                                  
                                  /************ Functions **********************/
                                  static int attr_baud_set(int,unsigned int);
                                  static void *datas_read(void*);
                                  //char rs232_read();
                                  int rs232_transfer(int fd, unsigned char data[], int buf_size);
                                  //static int RS232_test(int);
                                  void thread_DataRead(int);
                                  int UART4_Attr_set(int fd,SERIAL_ATTR_ST serial_attr);
                                  int UART4_Port_RDWR(int);
                                  //static int RS485_test(int)
                                  
                                  
                                  typedef struct {
                                      unsigned int lable;
                                      unsigned int baudrate;
                                  } SERIAL_BAUD_ST;
                                  
                                  typedef struct{
                                      unsigned int flags;
                                      unsigned int delay_rts_after_send;	/* delay after send (milliseconds) */
                                  }SERIAL_RS485_CONF_ST;
                                  
                                  
                                  
                                  #define DEBUG 1
                                  
                                  #ifdef DEBUG
                                    #define DEBUG_INFO(fmt, args...)  fprintf(stderr, fmt, ##args)
                                    #define DEBUG_ERROR(fmt, args...) fprintf(stderr, fmt"%s\t%s\t%d\n", ##args, __FILE__,__FUNCTION__,__LINE__)
                                  #else
                                    #define DEBUG_INFO(fmt, args...)
                                    #define DEBUG_ERROR(fmt, args...)
                                  #endif
                                  
                                  #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
                                  
                                  #define TIMEOUT                     1  /* read operation timeout 1s = TIMEOUT/10 */
                                  #define MIN_LEN                     128  /* the min len datas */
                                  #define DEV_NAME_LEN                32
                                  #define SERIAL_ATTR_BAUD            9600*N
                                  #define SERIAL_ATTR_DATABITS        8
                                  #define SERIAL_ATTR_STOPBITS        1
                                  #define SERIAL_ATTR_PARITY          'n'
                                  #define SERIAL_MODE_NORMAL          0
                                  #define SERIAL_MODE_485             1
                                  #define DELAY_RTS_AFTER_SEND        1   /* 1ms */
                                  #define SER_RS485_ENABLED		    1	/* 485 enable */
                                  #define RET_SUCCESS                 		0
                                  #define RET_FAILED                  		-1
                                  
                                  
                                  static SERIAL_BAUD_ST g_attr_baud[] = {
                                      {230400, B230400}, {115200, B115200},
                                      {57600, B57600}, {38400, B38400},
                                      {19200, B19200}, {9600, B9600},
                                      {4800, B4800},  {2400, B2400},
                                      {1800, B1800}, {1200, B1200},
                                  };
                                  
                                  static char g_dev_serial[][DEV_NAME_LEN] = {
                                          "/dev/ttymxc0",
                                          "/dev/ttymxc1",
                                          "/dev/ttymxc2",
                                          "/dev/ttymxc3",
                                          "/dev/ttymxc4",
                                  };
                                  
                                  static int attr_baud_set(int fd, unsigned int baud)
                                  {
                                      unsigned int i=0;
                                      int ret = RET_SUCCESS;
                                      struct termios option;
                                  
                                      /* get old serial attribute */
                                      memset(&option, 0, sizeof(option));
                                      if (RET_SUCCESS != tcgetattr(fd, &option))
                                      {
                                          DEBUG_ERROR("tcgetattr failed.\n");
                                          return RET_FAILED;
                                      }
                                  
                                      for (i=0;i<ARRAY_SIZE(g_attr_baud);i++)
                                      {
                                          if (baud == g_attr_baud[i].lable)
                                          {
                                              ret = tcflush(fd, TCIOFLUSH);
                                              if (RET_SUCCESS != ret)
                                              {
                                               //   DEBUG_ERROR("tcflush failed.\n");
                                                  break;
                                              }
                                  
                                              ret = cfsetispeed(&option, g_attr_baud[i].baudrate);
                                              if (RET_SUCCESS != ret)
                                              {
                                               //   DEBUG_ERROR("cfsetispeed failed.\n");
                                                  ret = RET_FAILED;
                                                  break;
                                              }
                                  
                                              ret = cfsetospeed(&option, g_attr_baud[i].baudrate);
                                              if (RET_SUCCESS != ret)
                                              {
                                                  //DEBUG_ERROR("cfsetospeed failed.\n");
                                                  ret = RET_FAILED;
                                                  break;
                                              }
                                  
                                              ret = tcsetattr(fd, TCSANOW, &option);
                                              if  (RET_SUCCESS != ret)
                                              {
                                                //  DEBUG_ERROR("tcsetattr failed.\n");
                                                  ret = RET_FAILED;
                                                  break;
                                              }
                                  
                                              ret = tcflush(fd, TCIOFLUSH);
                                              if (RET_SUCCESS != ret)
                                              {
                                              //    DEBUG_ERROR("tcflush failed.\n");
                                                  break;
                                              }
                                          }
                                      }
                                  
                                      return ret;
                                  }
                                  
                                  static int attr_other_set(int fd, SERIAL_ATTR_ST *serial_attr)
                                  {
                                      struct termios option;
                                  
                                      /* get old serial attribute */
                                      memset(&option, 0, sizeof(option));
                                      if (RET_SUCCESS != tcgetattr(fd, &option))
                                      {
                                          DEBUG_ERROR("tcgetattr failed.\n");
                                          return RET_FAILED;
                                      }
                                  
                                      option.c_iflag = CLOCAL | CREAD;
                                  
                                      /* set datas size */
                                      option.c_cflag &= ~CSIZE;
                                      option.c_iflag = 0;
                                  
                                      switch (serial_attr->databits)
                                      {
                                          case 7:
                                              option.c_cflag |= CS7;
                                              break;
                                  
                                          case 8:
                                              option.c_cflag |= CS8;
                                              break;
                                  
                                          default:
                                              DEBUG_ERROR("invalid argument, unsupport datas size.\n");
                                              return RET_FAILED;
                                      }
                                  
                                      /* set parity */
                                      switch (serial_attr->parity)
                                      {
                                          case 'n':
                                          case 'N':
                                              option.c_cflag &= ~PARENB;
                                              option.c_iflag &= ~INPCK;
                                              break;
                                  
                                          case 'o':
                                          case 'O':
                                              option.c_cflag |= (PARODD | PARENB);
                                              option.c_iflag |= INPCK;
                                              break;
                                  
                                          case 'e':
                                          case 'E':
                                              option.c_cflag |= PARENB;
                                              option.c_cflag &= ~PARODD;
                                              option.c_iflag |= INPCK;
                                              break;
                                  
                                          case 's':
                                          case 'S':
                                              option.c_cflag &= ~PARENB;
                                              option.c_cflag &= ~CSTOPB;
                                              break;
                                  
                                          default:
                                              DEBUG_ERROR("invalid argument, unsupport parity type.\n");
                                              return RET_FAILED;
                                      }
                                  
                                      /* set stop bits  */
                                      switch (serial_attr->stopbits)
                                      {
                                          case 1:
                                              option.c_cflag &= ~CSTOPB;
                                              break;
                                  
                                          case 2:
                                              option.c_cflag |= CSTOPB;
                                              break;
                                  
                                          default:
                                              DEBUG_ERROR("invalid argument, unsupport stop bits.\n");
                                              return RET_FAILED;
                                      }
                                  
                                      option.c_oflag = 0;
                                      option.c_lflag = 0;
                                      option.c_cc[VTIME] = TIMEOUT;
                                      option.c_cc[VMIN] = MIN_LEN;
                                  
                                      if (RET_SUCCESS != tcflush(fd,TCIFLUSH))
                                      {
                                          DEBUG_ERROR("tcflush failed.\n");
                                          return RET_FAILED;
                                      }
                                  
                                      if (RET_SUCCESS != tcsetattr(fd, TCSANOW, &option))
                                      {
                                          DEBUG_ERROR("tcsetattr failed.\n");
                                          return RET_FAILED;
                                      }
                                  
                                  #if 0
                                      tcgetattr(fd, &option);
                                      DEBUG_INFO("c_iflag: %x\rc_oflag: %x\n", option.c_iflag, option.c_oflag);
                                      DEBUG_INFO("c_cflag: %x\nc_lflag: %x\n", option.c_cflag, option.c_lflag);
                                      DEBUG_INFO("c_line: %x\nc_cc[VTIME]: %d\nc_cc[VMIN]: %d\n", option.c_line, option.c_cc[VTIME], option.c_cc[VMIN]);
                                  #endif
                                  
                                      return RET_SUCCESS;
                                  }
                                  
                                  static int attr_set(int fd, SERIAL_ATTR_ST *serial_attr)
                                  {
                                      int ret = RET_SUCCESS;
                                  
                                      if (NULL == serial_attr)
                                      {
                                          DEBUG_ERROR("invalid argument.\n");
                                          return RET_FAILED;
                                      }
                                  
                                      if (RET_SUCCESS == ret)
                                      {
                                          ret = attr_baud_set(fd, serial_attr->baud);
                                          if (RET_SUCCESS == ret)
                                          {
                                              ret = attr_other_set(fd, serial_attr);
                                          }
                                      }
                                  
                                      return ret;
                                  }
                                  
                                  
                                  static void *datas_read(void *arg)
                                  {
                                      int ret=0,i=0,n_data=0;
                                      int fd = *(int *)arg;
                                       printf("Reading the data...\n");
                                  
                                          memset(read_buf, 0, sizeof(read_buf));
                                      for(i=0;i<9;i++)
                                      {
                                              ret= read(fd, (read_buf+i), sizeof(char));
                                              if(ret){ n_data++; }
                                  
                                      }
                                  
                                      DEBUG_INFO("receive %d datas: %s\n", n_data, read_buf);
                                  
                                  }
                                  
                                  void thread_DataRead(int fd)
                                  {
                                      int ret = RET_SUCCESS;
                                      pthread_t pid;
                                      printf("Creating the Thread for Reading the Data.\n");
                                      ret = pthread_create(&pid, NULL, datas_read, (void *)&fd);
                                      if (RET_SUCCESS != ret)
                                      {
                                          DEBUG_ERROR("can't create thread:%s\n",(char*) strerror);
                                      }
                                      else
                                      printf("Thread created for reading the data.\n");
                                  
                                      pthread_cancel(pid);
                                  }
                                  
                                  
                                  
                                  int rs232_transfer(int fd,unsigned char data[],int buf_size)
                                  {
                                      unsigned char i;
                                      int n_data=0;
                                      int ret = RET_SUCCESS;
                                      //printf("Writing the data to R305...\n");
                                  
                                      for(i=0;i<(buf_size+1);i++)
                                      {
                                              ret = write(fd, (data+i) , sizeof(char));
                                          //printf("Data Sent:%x\n",data[i]);
                                          n_data++;
                                      }
                                  
                                          if ((buf_size+1) != n_data)
                                              {
                                                  printf("Data Write Failed\n");
                                                      ret = RET_FAILED;
                                  
                                              }
                                          else
                                          //printf("Done.\n");
                                              sleep(1);
                                  
                                      return ret;
                                  }
                                  
                                  
                                  int UART4_Attr_set(int fd,SERIAL_ATTR_ST serial_attr)
                                  {
                                       int ret;
                                      //SERIAL_ATTR_ST serial_attr;
                                      //printf("Setting Up the Attributes.\n");
                                  
                                      memset(&serial_attr, 0, sizeof(serial_attr));
                                      serial_attr.baud = SERIAL_ATTR_BAUD;
                                      serial_attr.databits = SERIAL_ATTR_DATABITS;
                                      serial_attr.stopbits = SERIAL_ATTR_STOPBITS;
                                      serial_attr.parity = SERIAL_ATTR_PARITY;
                                  
                                      ret = attr_set(fd, &serial_attr);
                                      return ret;
                                  }
                                  
                                  int UART4_Port_RDWR(int channel)
                                  {
                                      int fd;
                                      //int bufsize;
                                      //char write_buf[BUF_SIZE];
                                  
                                      //DEBUG_INFO("QY-IMX6S-V1.2 RS232 Com channe ss%d \n Start Testing ...\n", channel);
                                      fd = open(g_dev_serial[channel], O_RDWR);
                                      if (fd < 0)
                                      {
                                          DEBUG_ERROR("open serial device %s error!\n", g_dev_serial[channel]);
                                          return RET_FAILED;
                                      }
                                  
                                      /*memset(write_buf,0,sizeof(write_buf));
                                      snprintf(write_buf, sizeof(write_buf), "Com%d RS232 test string!\n", channel);
                                      bufsize = strlen(write_buf) + 1;
                                  
                                      serial_rs232_test(fd, write_buf, bufsize);*/
                                  
                                      return fd;
                                  }
                                  
                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Why not use QSerialPort ?

                                    In any case, I don't see any call to close the file descriptor after use.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    S 1 Reply Last reply
                                    0
                                    • SGaistS SGaist

                                      Why not use QSerialPort ?

                                      In any case, I don't see any call to close the file descriptor after use.

                                      S Offline
                                      S Offline
                                      sachin786
                                      wrote on last edited by
                                      #18

                                      @SGaist ok i will check that file and get back to u...and i am using QT creator 4.8.5 and in that there is no proper documentation for Qserialport thats why i am using normal serial port terminal available in ubuntu 12.04.

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        You'll have to build the module yourself (there's a Qt 4 dedicated branch) but otherwise. The documentation is the same.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        S 1 Reply Last reply
                                        0
                                        • SGaistS SGaist

                                          You'll have to build the module yourself (there's a Qt 4 dedicated branch) but otherwise. The documentation is the same.

                                          S Offline
                                          S Offline
                                          sachin786
                                          wrote on last edited by
                                          #20

                                          @SGaist :: as far now problem is get fixed and my application running fine. as you help me to point out i am not closing file descriptor, i fix this by simply closing file descriptor and keeping track that no device peripheral left open more than once, at the moment i am using with regular serial port communication, thanx for all your suggestion it really help me alot.
                                          once i finish this application surely i will try to work with QSerialPort :) Thank you once again and have great day ☺

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved