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
Qt 6.11 is out! See what's new in the release blog

serial port in dll

Scheduled Pinned Locked Moved General and Desktop
dllserial port
50 Posts 4 Posters 71.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.
  • C Offline
    C Offline
    chr_sch
    wrote on last edited by
    #1

    Hi at all,

    I want to write a dll file , using a serial port.
    I will describe what the dll should do:
    I call the function of the dll in a measurement programm. The function should accept 6 initial values, which should be send to a microcontroller. So in the dll I want to use the QtSerialPort and send the values to my microcontroller.

    I wrote a dll but when I try to run it in a test programm I get a lot of errors:
    " LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: bool __cdecl QSerialPort::setDataBits(enum QSerialPort::DataBits)" (_imp?setDataBits@QSerialPort@@QEAA_NW4DataBits@1@@Z)" in Funktion ""public: void __cdecl Messwert_Zuordnung_DLL::Messwert_zuordnung(double,double,double,double,double,double)" (?Messwert_zuordnung@Messwert_Zuordnung_DLL@@QEAAXNNNNNN@Z)"."

    Here is my dll code:
    <
    #include "messwert_zuordnung_dll.h"
    #include <iostream>
    #include <string>
    #include <math.h>
    #include <QDebug>
    #include <QtCore/QCoreApplication>
    #include <Windows.h>
    #include <QtSerialPort/QSerialPort>
    #include <QtCore/QDebug>

    QSerialPort *serial;

    void Messwert_Zuordnung_DLL::Messwert_zuordnung(double messmeth, double messint, double bauteillaenge, double interpol, double anz_messungen, double abs_messungen)
    {
    //Configuration of the serial port
    serial = new QSerialPort;

    //+++++++++++++++++++++++++++++++++++
    //+++++++++++++++++++++++++++++++++++
    //Adapt the COM-Port !
    
    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);
    
    
         if (messmeth == 1){
    
    
            //Conversion to send (requiered by the send-> function) 
    
    
            QByteArray messint_s = QByteArray::number(messint);
            QByteArray bauteillaenge_s = QByteArray::number(bauteillaenge);
            QByteArray interpol_s = QByteArray::number(interpol);
            QByteArray anz_messungen_s = QByteArray::number(anz_messungen);
            QByteArray abs_messungen_s = QByteArray::number(abs_messungen);
    
            //Sending the variables
            serial->write("1");
            serial->write("\n");
    
            serial->write(bauteillaenge_s);
            serial->write("\n");
    
            serial->write(messint_s);
            serial->write("\n");
    
            serial->write(interpol_s);
            serial->write("\n");
    
            serial->write(anz_messungen_s);
            serial->write("\n");
    
            serial->write(abs_messungen_s);
            serial->write("\n");
    

    Maybe I have to initiate the serial port in an other way ? Can anybody help me ?

    regards

    T 1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by t3685
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • C chr_sch

        Hi at all,

        I want to write a dll file , using a serial port.
        I will describe what the dll should do:
        I call the function of the dll in a measurement programm. The function should accept 6 initial values, which should be send to a microcontroller. So in the dll I want to use the QtSerialPort and send the values to my microcontroller.

        I wrote a dll but when I try to run it in a test programm I get a lot of errors:
        " LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""__declspec(dllimport) public: bool __cdecl QSerialPort::setDataBits(enum QSerialPort::DataBits)" (_imp?setDataBits@QSerialPort@@QEAA_NW4DataBits@1@@Z)" in Funktion ""public: void __cdecl Messwert_Zuordnung_DLL::Messwert_zuordnung(double,double,double,double,double,double)" (?Messwert_zuordnung@Messwert_Zuordnung_DLL@@QEAAXNNNNNN@Z)"."

        Here is my dll code:
        <
        #include "messwert_zuordnung_dll.h"
        #include <iostream>
        #include <string>
        #include <math.h>
        #include <QDebug>
        #include <QtCore/QCoreApplication>
        #include <Windows.h>
        #include <QtSerialPort/QSerialPort>
        #include <QtCore/QDebug>

        QSerialPort *serial;

        void Messwert_Zuordnung_DLL::Messwert_zuordnung(double messmeth, double messint, double bauteillaenge, double interpol, double anz_messungen, double abs_messungen)
        {
        //Configuration of the serial port
        serial = new QSerialPort;

        //+++++++++++++++++++++++++++++++++++
        //+++++++++++++++++++++++++++++++++++
        //Adapt the COM-Port !
        
        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);
        
        
             if (messmeth == 1){
        
        
                //Conversion to send (requiered by the send-> function) 
        
        
                QByteArray messint_s = QByteArray::number(messint);
                QByteArray bauteillaenge_s = QByteArray::number(bauteillaenge);
                QByteArray interpol_s = QByteArray::number(interpol);
                QByteArray anz_messungen_s = QByteArray::number(anz_messungen);
                QByteArray abs_messungen_s = QByteArray::number(abs_messungen);
        
                //Sending the variables
                serial->write("1");
                serial->write("\n");
        
                serial->write(bauteillaenge_s);
                serial->write("\n");
        
                serial->write(messint_s);
                serial->write("\n");
        
                serial->write(interpol_s);
                serial->write("\n");
        
                serial->write(anz_messungen_s);
                serial->write("\n");
        
                serial->write(abs_messungen_s);
                serial->write("\n");
        

        Maybe I have to initiate the serial port in an other way ? Can anybody help me ?

        regards

        T Offline
        T Offline
        t3685
        wrote on last edited by
        #3

        Can you post your pro file?

        Do you have this line in it?

        QT += serialport

        C 1 Reply Last reply
        0
        • sneubertS Offline
          sneubertS Offline
          sneubert
          wrote on last edited by
          #4

          Hi @chr_sch,

          could you clarify if your problem is to build the dll itself, or to include the dll in another project?

          1 Reply Last reply
          0
          • T t3685

            Can you post your pro file?

            Do you have this line in it?

            QT += serialport

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

            @t3685
            that´s my pro file:

            TARGET = Messwert_Zuordnung_DLL
            TEMPLATE = lib
            
            QT += serialport
            QT += core
            QT -= gui
            
            DEFINES += MESSWERT_ZUORDNUNG_DLL_LIBRARY
            
            SOURCES += messwert_zuordnung_dll.cpp
            
            HEADERS += messwert_zuordnung_dll.h\
                    messwert_zuordnung_dll_global.h
            
            
            unix {
                target.path = /usr/lib
                INSTALLS += target
            }
            

            serialport is added.

            @sneubert:

            the dll builds without a problem. But I am not shure that it works correctly in the way I created it.
            In the test programm I added the .h and global .h file and set the reference to the dll file.
            Here is the pro file from my test program:

            QT       += core
            
            QT       -= gui
            QT       += serialport
            
            TARGET = dll_tester
            CONFIG   += console
            CONFIG   -= app_bundle
            LIBS += "C:\Users\Christian\Documents\build-dll_tester-Desktop_Qt_5_4_0_MSVC2013_64bit-Debug\debug\Messwert_Zuordnung_DLL.obj"
            TEMPLATE = app
            
            
            SOURCES += main.cpp
            
            HEADERS += \
                messwert_zuordnung_dll.h \
                messwert_zuordnung_dll_global.h \
                qserialport.h \
                qserialportglobal.h
            
            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

              At first glance it seems you are not linking to your library properly. Have a look at the link below for an example on how to link to a library.

              https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

              C 1 Reply Last reply
              0
              • T t3685

                At first glance it seems you are not linking to your library properly. Have a look at the link below for an example on how to link to a library.

                https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

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

                @t3685

                I changed a few things in my .pro file according to the instructions in the link you send.
                Namely I changed the following three things

                INCLUDEPATH += C:\Users\Christian\Desktop\Studienarbeit\DLL\Messwert_Zuordnung_DLL
                DEPENDPATH += C:\Users\Christian\Desktop\Studienarbeit\DLL\Messwert_Zuordnung_DLL
                LIBS += C:\Users\Christian\Desktop\Studienarbeit\DLL\build-Messwert_Zuordnung_DLL-Desktop_Qt_5_4_0_MSVC2013_64bit-Debug\debug

                But the Problem stayed ! The error refers to the QSerialPort I used.
                I tried to include the DEPENDPATH and INCLUDEPATH where the qserialport.h file is in the Qt folder and added the qserialport.h file to the Header but no succes.

                Any other ideas ?

                regards

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  t3685
                  wrote on last edited by
                  #8

                  @chr_sch

                  Hello,

                  I think you still have an error in your pro file. The correct syntax to link to a library is

                  LIBS += -L<path to your dll> -l<name of your dll>

                  In your pro file you don't have the -l and -L. Please follow the instructions in the wiki more closely

                  C 1 Reply Last reply
                  0
                  • T t3685

                    @chr_sch

                    Hello,

                    I think you still have an error in your pro file. The correct syntax to link to a library is

                    LIBS += -L<path to your dll> -l<name of your dll>

                    In your pro file you don't have the -l and -L. Please follow the instructions in the wiki more closely

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

                    @t3685

                    I changed it :
                    LIBS += -L C:\Users\Christian\Desktop\Studienarbeit\DLL\build-Messwert_Zuordnung_DLL-Desktop_Qt_5_4_0_MSVC2013_64bit-Debug\debug -lMesswert_Zuordnung_Dll

                    Still getting the error.

                    regards

                    T 1 Reply Last reply
                    0
                    • C chr_sch

                      @t3685

                      I changed it :
                      LIBS += -L C:\Users\Christian\Desktop\Studienarbeit\DLL\build-Messwert_Zuordnung_DLL-Desktop_Qt_5_4_0_MSVC2013_64bit-Debug\debug -lMesswert_Zuordnung_Dll

                      Still getting the error.

                      regards

                      T Offline
                      T Offline
                      t3685
                      wrote on last edited by
                      #10

                      @chr_sch

                      Hi,

                      Did you put a export macro in your class like explained in the wiki and on this page:

                      http://doc.qt.io/qt-5/sharedlibrary.html

                      Greetings,

                      t3685

                      C 1 Reply Last reply
                      0
                      • T t3685

                        @chr_sch

                        Hi,

                        Did you put a export macro in your class like explained in the wiki and on this page:

                        http://doc.qt.io/qt-5/sharedlibrary.html

                        Greetings,

                        t3685

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

                        @t3685
                        I added the Qt5SerialPort.lib, found somewhere in the Qt intalling folder, in the pro file under LIBS.
                        This generated some new errors refers to the moc_qserialport.cpp like this:

                        1.)..\debug\moc_qserialport.cpp:352: Fehler: C2027: using
                        undefined Typ "QSerialPortPrivate"
                        c:\users\christian\documents\build-dll_tester-desktop_qt_5_4_0_msvc2013_
                        64bit-debug\debug../../dll_tester/qserialport.h(45): Declaration
                        of 'QSerialPortPrivate'

                        2.)..\debug\moc_qserialport.cpp:352: Fehler: C2227: to the left of
                        "->_q_completeAsyncCommunication" must be an pointer to an class/strucktur/union/generic type

                        regards

                        T 1 Reply Last reply
                        0
                        • C chr_sch

                          @t3685
                          I added the Qt5SerialPort.lib, found somewhere in the Qt intalling folder, in the pro file under LIBS.
                          This generated some new errors refers to the moc_qserialport.cpp like this:

                          1.)..\debug\moc_qserialport.cpp:352: Fehler: C2027: using
                          undefined Typ "QSerialPortPrivate"
                          c:\users\christian\documents\build-dll_tester-desktop_qt_5_4_0_msvc2013_
                          64bit-debug\debug../../dll_tester/qserialport.h(45): Declaration
                          of 'QSerialPortPrivate'

                          2.)..\debug\moc_qserialport.cpp:352: Fehler: C2227: to the left of
                          "->_q_completeAsyncCommunication" must be an pointer to an class/strucktur/union/generic type

                          regards

                          T Offline
                          T Offline
                          t3685
                          wrote on last edited by
                          #12

                          @chr_sch

                          You don't need to add Qt5SerialPort.lib.
                          The error you are getting is that the linker cannot find the implementation of "Messwert_Zuordnung_DLL::Messwert_zuordnung".
                          Like I said this is either due to:

                          a) incorrectly linking to your dll
                          b) not declaring the function/class for export

                          Both situations are explained in:

                          http://doc.qt.io/qt-5/sharedlibrary.html

                          So if the problem persist, please make sure that both conditions are fulfilled exactly as described in the link.

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            chr_sch
                            wrote on last edited by chr_sch
                            #13

                            This is my header file:

                            #if defined(MESSWERT_ZUORDNUNG_DLL_LIBRARY)
                            #  define MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Q_DECL_EXPORT
                            #else
                            #  define MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Q_DECL_IMPORT
                            #endif
                            
                            class MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Messwert_Zuordnung_DLL
                            {
                            
                            public:
                                Messwert_Zuordnung_DLL();
                            
                                void uebergabe(double messmeth,double messint, double bauteillaenge, double interpol, double anz_messungen, double abs_messungen);
                            };
                            

                            Therefore I think it is like mentioned in the link.

                            The linking to my dll consists of the following points:
                            -include the header files in my tets programm (copy them in the same direction)
                            -DEPENDPATH/INCLUDEPATH path to the folder where the .dll/.obj/.lib files are
                            -LIBS -L Path to dll -lname of dll
                            -HEADER name the headers to include

                            So i followed the description in the link but I just cant see where my faults are ?!
                            Should I send you my files ?

                            regards

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

                              Hi and welcome to devnet,

                              Did you re-run qmake after adding QT += serialport ?

                              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

                                Hi and welcome to devnet,

                                Did you re-run qmake after adding QT += serialport ?

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

                                @SGaist

                                Yes I run qmake.
                                I tried to set the paths to the folder where my test program is.
                                Now I get the error:
                                main.obj:-1: Fehler: LNK2019:unresolved external symbol""__declspec(dllimport) public: void __cdecl Messwert_Zuordnung_DLL::uebergabe(double,double,double,double,double,double)" (_imp?uebergabe@Messwert_Zuordnung_DLL@@QEAAXNNNNNN@Z)" in function "main".

                                regards

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

                                  Did you check that you are doing the property symbol exporting ? Define name for activating the export macro etc. ?

                                  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
                                  • C chr_sch

                                    This is my header file:

                                    #if defined(MESSWERT_ZUORDNUNG_DLL_LIBRARY)
                                    #  define MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Q_DECL_EXPORT
                                    #else
                                    #  define MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Q_DECL_IMPORT
                                    #endif
                                    
                                    class MESSWERT_ZUORDNUNG_DLLSHARED_EXPORT Messwert_Zuordnung_DLL
                                    {
                                    
                                    public:
                                        Messwert_Zuordnung_DLL();
                                    
                                        void uebergabe(double messmeth,double messint, double bauteillaenge, double interpol, double anz_messungen, double abs_messungen);
                                    };
                                    

                                    Therefore I think it is like mentioned in the link.

                                    The linking to my dll consists of the following points:
                                    -include the header files in my tets programm (copy them in the same direction)
                                    -DEPENDPATH/INCLUDEPATH path to the folder where the .dll/.obj/.lib files are
                                    -LIBS -L Path to dll -lname of dll
                                    -HEADER name the headers to include

                                    So i followed the description in the link but I just cant see where my faults are ?!
                                    Should I send you my files ?

                                    regards

                                    T Offline
                                    T Offline
                                    t3685
                                    wrote on last edited by
                                    #17

                                    @chr_sch

                                    Do you this macro: MESSWERT_ZUORDNUNG_DLL_LIBRARY defined in the pro file of your library?

                                    1 Reply Last reply
                                    0
                                    • C Offline
                                      C Offline
                                      chr_sch
                                      wrote on last edited by
                                      #18

                                      I choose to use QLibrary instead of implementing the dll by myself. Therefore I used the following code in my main.cpp:

                                       QLibrary library("Messwert_Zuordnung_DLL.dll");
                                          if (!library.load())
                                          qDebug() << library.errorString();
                                          if (library.load())
                                          qDebug() << "library loaded";
                                      
                                          library.resolve("uebergabe");
                                      

                                      I am not really shure, that this is the proper use of QLibrary but it builds without an error.
                                      So I think it might work.
                                      If my dll gives simply the value in qDebug() out which I write in the dll function in my test programm it works.
                                      But if I try to establish a serial connection, my microcontroller does not recognize a input. So there is a fault in the use of the QSerialPort in my code. The same code is used in a GUI where it works. I only did not use the connect() function and in the definition of my port serial* = new QSerialPort (this) I didn´t write this. Because I have no parent object here. Do I have to establish a QObject for this ? Or is there any other problem with the use of QSerialPort in a dll ?

                                      Here is my code:

                                      QSerialPort *serial;
                                       serial = new QSerialPort();
                                      
                                      
                                          //+++++++++++++++++++++++++++++++++++
                                          //+++++++++++++++++++++++++++++++++++
                                          //COM-Port !
                                      
                                          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);
                                      
                                      
                                                  //conversion of the variables for the use of the send function
                                      
                                      
                                                  QByteArray messint_s = QByteArray::number(messint);
                                                  QByteArray bauteillaenge_s = QByteArray::number(bauteillaenge);
                                                  QByteArray interpol_s = QByteArray::number(interpol);
                                                  QByteArray anz_messungen_s = QByteArray::number(anz_messungen);
                                                  QByteArray abs_messungen_s = QByteArray::number(abs_messungen);
                                      
                                                  //Sending the variables to my microcontroller
                                                  serial->write("1");
                                                  serial->write("\n");
                                      

                                      regards

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

                                        Was it dll_tester that you failed to link ? If so, you were not linking to the library.

                                        You need to delete serial by hand when not using it anymore otherwise you'll be leaking memory

                                        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

                                          Was it dll_tester that you failed to link ? If so, you were not linking to the library.

                                          You need to delete serial by hand when not using it anymore otherwise you'll be leaking memory

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

                                          @SGaist
                                          I don´t know where my fault was. I think it was some linking problem but I don´t know how to solve so I use QLibrary.

                                          Ok you mean that I have to close the serialport at the end of the dll ?
                                          Can you see any other fault? Because the serial connection is not working .

                                          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