Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. serial port in dll
Forum Updated to NodeBB v4.3 + New Features

serial port in dll

Scheduled Pinned Locked Moved General and Desktop
dllserial port
50 Posts 4 Posters 36.5k Views 3 Watching
  • 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.
  • SGaistS SGaist

    Ok, then just create on minimal application that opens that serial port and tries to write to / read from it. That will at least help check that your serial port is working correctly

    C Offline
    C Offline
    chr_sch
    wrote on last edited by
    #28

    @SGaist
    My first project was a GUI that communicates with that serial port.
    This still works fine. I used the same code to implement the serial connection in the dll file. Only the parent object, if i create the new QSerialPort, in thr parenthesis is empty. And i do not use the connect () function. If i change the code in the GUI it still works.
    In the GUI as well as in the dll the serial connection us only used to send a few variables to a microcontroller. So it should be a minimal requirement to the serial. Maybeit it is not possible to write a dll that opens a serial port at runtime ?

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

      There's no such restriction.

      Since you had trouble with the library in the first place, the next step is to create a minimal library that will provide some dummy function and link to it. Don't use QLibrary to load, just make your application link to it properly.

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

      C 1 Reply Last reply
      0
      • SGaistS SGaist

        There's no such restriction.

        Since you had trouble with the library in the first place, the next step is to create a minimal library that will provide some dummy function and link to it. Don't use QLibrary to load, just make your application link to it properly.

        C Offline
        C Offline
        chr_sch
        wrote on last edited by chr_sch
        #30

        @SGaist

        Ok I just tried to create a new one.
        My steps:
        *Qt Creator -> new -> dll
        *add a dummy function under public in my header
        *dummy function in the cpp file: qDebug() << dll works

        *create a new console application
        *right click add existing file: mark the two header files from my dll
        *set the paths: -> INCLUDEPATH += folder from my dll where the headers are
        ->DEPENDPATH+= same folder as above
        ->LIBS+= -Ldebug folder of my dll file -lname of my dll file
        *copy the dll in the debug folder of the test program
        *in the test programm i add in the cpp file:
        -> include the header
        ->create new object
        -> call the function from the dll

        All works well. But if I try to pass an integer through the function to my dll I get an unresolved symbol error.
        After I changed my dll I re-build it and changed the copied files through the new ones.

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

          Can you show the code where you pass that integer ?

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

          C 1 Reply Last reply
          0
          • SGaistS SGaist

            Can you show the code where you pass that integer ?

            C Offline
            C Offline
            chr_sch
            wrote on last edited by chr_sch
            #32

            @SGaist

            In the dll i specified the following function:
            -header
            public:
            void Test(int a);

            -cpp
            qDebug()<< a;

            in the Test program:
            int b=1;
            MyLib c ;
            c.test(b);

            The exact error message is:
            main.obj:-1: Error: LNK2019: unresolved symbol ""__declspec(dllimport) public: void __cdecl MyLib::Test(int)" (_imp?Test@MyLib@@QEAAXH@Z)" in function "main".

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

              You are not exporting Test properly

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

              C 1 Reply Last reply
              0
              • SGaistS SGaist

                You are not exporting Test properly

                C Offline
                C Offline
                chr_sch
                wrote on last edited by
                #34

                @SGaist
                How to do so ?
                Where is my fault ?

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

                  You have a nice explanation in the Creating shared libraries chapter of Qt's documentation

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

                  C 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    You have a nice explanation in the Creating shared libraries chapter of Qt's documentation

                    C Offline
                    C Offline
                    chr_sch
                    wrote on last edited by
                    #36

                    @SGaist
                    I red this chapter already and my header files as well as the pro file are as specified in the explanation:
                    dll.h:

                    #ifndef MESSWERT_ZUORDNUNG_DLL_H
                    #define MESSWERT_ZUORDNUNG_DLL_H
                    
                    #include "messwert_zuordnung_dll_global.h"
                    
                    MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT void Test(int a);
                    
                    class MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Messwert_zuordnung_dll
                    {
                    
                    public:
                        Messwert_zuordnung_dll();
                    
                        void Test(int a);
                    };
                    
                    #endif // MESSWERT_ZUORDNUNG_DLL_H
                    

                    dll_global.h

                    #ifndef MESSWERT_ZUORDNUNG_DLL_GLOBAL_H
                    #define MESSWERT_ZUORDNUNG_DLL_GLOBAL_H
                    
                    #include <QtCore/qglobal.h>
                    
                    #if defined(MESSWERT_ZUORDNUNG_DLL_LIBRARY)
                    #  define MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Q_DECL_EXPORT
                    #else
                    #  define MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Q_DECL_IMPORT
                    #endif
                    
                    #endif // MESSWERT_ZUORDNUNG_DLL_GLOBAL_H
                    

                    I cant see a big difference.

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

                      Following your various piece of code, you don't have any class called MyLib containing Test(int) nor any namespace containing such a function

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

                      C 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Following your various piece of code, you don't have any class called MyLib containing Test(int) nor any namespace containing such a function

                        C Offline
                        C Offline
                        chr_sch
                        wrote on last edited by
                        #38

                        @SGaist
                        I changed the name, because the actual is a bit long. My class is Messwert_zuordnung_dll.
                        The function for the export is Test.

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

                          Ok so your

                          MyLib c;
                          c.test(b);
                          

                          should rather be:

                          Messwert_zuordnung_dll mzd;
                          mzd.Test(b);
                          

                          correct ?

                          So, when you are building your dll, do you get both the dll and corresponding lib file ?

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

                          C 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Ok so your

                            MyLib c;
                            c.test(b);
                            

                            should rather be:

                            Messwert_zuordnung_dll mzd;
                            mzd.Test(b);
                            

                            correct ?

                            So, when you are building your dll, do you get both the dll and corresponding lib file ?

                            C Offline
                            C Offline
                            chr_sch
                            wrote on last edited by
                            #40

                            @SGaist
                            Yes that´s correct. Sorry at this point for the confusion with the names.

                            Yes the build creates a .dll/.lib and .obj file.
                            At the beginning I had some trouble with the link I set as dependent path and libs in the pro file because there where a space in it. The error said "could not open .obj" so maybe it the program tries to open the .obj instead of the .dll ?

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

                              No, you should link to the lib file. However having space in your path on Windows is generally a Bad Thing (™) so just avoid them altogether

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

                              C 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                No, you should link to the lib file. However having space in your path on Windows is generally a Bad Thing (™) so just avoid them altogether

                                C Offline
                                C Offline
                                chr_sch
                                wrote on last edited by
                                #42

                                @SGaist
                                Yes I realized that this is a problem and renamed the folder. But this was just at the beginning, now it has to be an other problem

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

                                  It still can't find it ? Did you implement Test(int a) ?

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

                                  C 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    It still can't find it ? Did you implement Test(int a) ?

                                    C Offline
                                    C Offline
                                    chr_sch
                                    wrote on last edited by
                                    #44

                                    @SGaist
                                    What exactly do you mean with implement ?
                                    Under the exportet class in "public" I added "void Test(int a)" and right clicked refractor and added the function to the cpp file.

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

                                      Then it's been implemented

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

                                      C 1 Reply Last reply
                                      0
                                      • SGaistS SGaist

                                        Then it's been implemented

                                        C Offline
                                        C Offline
                                        chr_sch
                                        wrote on last edited by chr_sch
                                        #46

                                        @SGaist
                                        I created an other time a new project and did all step by step. After all the dll works. I wrote the dll and the test program exactly as in the files before. I don´t know where the difference is.

                                        I am able to pass an integer through the function of my dll.
                                        In the next step I would like to establish an serial port and send the passed integer to my mikrocontroller.
                                        The code whrer i implement the serial port looks like this:

                                        QSerialPort *serial;
                                        
                                        void Messwert_zuordnung_dll::Test(int a){
                                            serial = new QSerialPort();
                                        
                                        
                                            //+++++++++++++++++++++++++++++++++++
                                            //+++++++++++++++++++++++++++++++++++
                                            // COM-Port, checked in device manager!
                                        
                                            serial->setPortName("COM12");
                                        
                                            //+++++++++++++++++++++++++++++++++++
                                            //+++++++++++++++++++++++++++++++++++
                                        
                                        
                                            serial->setBaudRate(QSerialPort::Baud115200);
                                            serial->setDataBits(QSerialPort::Data8);
                                            serial->setParity(QSerialPort::NoParity);
                                            serial->setStopBits(QSerialPort::OneStop);
                                            serial->setFlowControl(QSerialPort::SoftwareControl);
                                        
                                            serial->open(QIODevice::ReadWrite);
                                            serial -> write(c);
                                            serial->close();
                                        

                                        The connection can´t be opened and in the console of the test program I get the following error:
                                        "QObject::start timer: Timers can only be used with threads started with QThread"

                                        What means this? In my GUI i did not start a Thread seperatly. How can I solve this error?

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

                                          Are you calling that function from your application main thread ?

                                          Note that you have a memory leak since you don't delete serial

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

                                          C 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