serial port in dll
-
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
-
@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 . -
You have to delete serial. IIRC that should also close it.
In any case, you should recheck your project and make it link properly to your library. Using QLibrary as a workaround is not the proper thing to do.
-
@SGaist
Yes but this should only be a test program to see if the dll works correctly. In the end the dll is used in an othe context, so I want to focus on the function of the dll. Alltough it would be nice to know where my fault was. But I did everything like in the description in the links above nothing works, so I have to move on to loos not more time.But why doesn´t works my serial connection ? That is in the moment my biggest problem.
Do I have to create a QObject as parent when I don´t have a window as parent ?
Is the connect function necessary ?
I thought the use is only to refer the connection fe to a button. But in a dll it should be unnecessary.Is this maybe the troublemaker?
-
Why it doesn't work ? I can't tell, that's Crystal Ball debugging at this point. However, you don't check that that your device opened successfully
-
Yeah that´s kind of diffucult. But I need help because I dont´t know how to figure out where my problem is.
I tried in a working serial port GUI to delete the connect() function and the "this" in new QSerialPort(this). It is still working. So the code should work.With : if(!serial->open(QIODevice::ReadWrite)){qDebug()<<"Connection failed"}. I figured out, that the connection is not opened.
I get the return value: -1073741510. Knows anybody what this means?
regards
-
Your application has been aborted e.g. you stopped it brutally.
Did you check the returned error ? What is it ?
-
@SGaist
How to check the error ? My program runs without showing an error. But there has to be one because the serial connection can´t be opened.I tried to use : qDebug()<< serial->errorString();
But the output was "Unkown error". -
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
-
@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 ? -
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.
-
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 dllAll 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. -
Can you show the code where you pass that integer ?
-
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". -
You are not exporting Test properly
-
@SGaist
How to do so ?
Where is my fault ? -
You have a nice explanation in the Creating shared libraries chapter of Qt's documentation
-
@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.
-
Following your various piece of code, you don't have any class called MyLib containing Test(int) nor any namespace containing such a function
-
@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.