PDA

View Full Version : Way to check for input data from serial port without polling in main



Kammersgaard
13th July 2012, 09:36
Hey!

I'm trying to make a class in Qt to handle communication with a turnable button with programable resistens. The button came with some functions for communicating with it. I can communicate with it just fine. Every time it is turn it sends a report to the serial port it's connected to. Untill now I've just been polling a function that came with the device for reading these reports. Now I was wondering if I somehow could check for input outside the main loop. Then when there's input call this function to read it.

I've tried solving this problem by overwriting the winEventFilter but I only see when the device is plugged-in or removed. No event goes through here when the device sends a report. Is there a good reason for this?
Then I found something about SerialPort::DataReceived Event but this seem only to be useful if I myself had initialized the port and not through the function I got with the device. Am I wrong?

Hope someone got a good advice on how to solve this!

kuzulis
13th July 2012, 09:53
Use ready-made library as QtSerialPort or QextSerialPort and use the signal is readyRead().

I would suggest taking QtSerialPort (http://qt-project.org/wiki/QtSerialPort)

hezbon
13th July 2012, 11:08
Use ready-made library as QtSerialPort or QextSerialPort and use the signal is readyRead().

I would suggest taking QtSerialPort (http://qt-project.org/wiki/QtSerialPort)

i suggest this same solution and use the readyRead to fire up a slot function in your class

Kammersgaard
16th July 2012, 08:12
I've been trying to install the library by following the instructions in the link you gave. I've not tried running qt projects in the command propt before so it might be a simple mistake from my side. I get this:

SerialPortd_resource.rc(4):fatal error RC1015: cannot open include file 'winver.h'

I think maybe I have to include something in my path but I don't know what.

ChrisW67
16th July 2012, 08:57
You need to be using a command prompt with both the Qt build and Microsoft compiler environments established. If you are using the Qt SDK then you can get a Qt Command Prompt using the option in your Start Menu. Then you need the Microsoft environment: http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx

Kammersgaard
16th July 2012, 09:33
I've now tried openning the serialport.pro in Qt and then ran it there. A window open saying "Press <Return> to close this window". The window was named C:\QtSDK\QtCreator\bin\qtcreator_process_stub.exe. Does this mean that the library was installed?

I then tried running my project but it didn't compile saying:
main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QtAddOn::SerialPort::SerialPort::~SerialPort(void)" (__imp_??1SerialPort@0QtAddOn@@UAE@XZ)
main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall QtAddOn::SerialPort::SerialPort::SerialPort(class QObject *)" (__imp_??0SerialPort@0QtAddOn@@QAE@PAVQObject@@@Z)
release\test2.exe : fatal error LNK1120: 2 unresolved externals

It seems I need to include some library related to SerialPort

Added after 34 minutes:

Thanks. Ran it in the command prompt for MSVC and it worked! I also got a qt project where a SerialPort is initialized to work. But only the debug version. No release was installed. Tried writing CONFIG += release as the guide says but it just says Cannot find file: CONFIG Cannot find file: release.

Besides that I need some documentation. Can't find it on the internet. Something about qdoc but don't know what that means exactly..

kuzulis
16th July 2012, 10:02
Tried writing CONFIG += release as the guide says but it just says Cannot find file: CONFIG Cannot find file: release.
Oops.. Sorry.. My mistake.
Should be written without spaces:


CONFIG+=release

I'll correct the WiKi.

Kammersgaard
16th July 2012, 10:17
Thanks that worked but now it says cannot open input file 'SerialPort.lib'. I emptied the build folder before running it. Should I remove anything else?

kuzulis
16th July 2012, 11:25
Can't find it on the internet. Something about qdoc but don't know what that means exactly..
The documentation is not finished yet, but you can use, need build:
1. Check in installed Qt for file <path to qt directory>\doc\html\qt.index
2. Set path to Qt directory:


set QT5DOC=<path to qt directory>

3. Make QDoc, example:


qdoc3 <path to serialport source>\doc\serialport.qdocconf

4. The result will appear the documentation in:


<path to serialport source>\doc\html\



Thanks that worked but now it says cannot open input file 'SerialPort.lib'. I emptied the build folder before running it. Should I remove anything else?
Before assembling the Release should be completely clean build directory, and then (after assembly) - install the library.
Then in the Qt /lib and /bin directory will contain both Debug and Release serial port.

Added after 39 minutes:

UPD:

Thanks that worked but now it says cannot open input file 'SerialPort.lib'.

To solve this problem there are 3 way:

1) Build Debug and Release in separate directories, for example:
serialport-release
serialport-debug
and then, after build, turn run "make install" for each target ( from serialport-release and serialport-debug )

2) After build and install Debug target, need delete /QtAddOnSerialPort directory from <qt path>\include, and next,
restart Command promt and build and install Release target.

3) Just comment out the build of examples and tests in serialport.pro:


SUBDIRS = src #examples tests

Kammersgaard
16th July 2012, 11:35
With path to qt directory do you mean something like C:\QtSDK\Desktop\Qt\4.8.1\msvc2008? I didn't find any doc folder there. Checked the entire computer and no qt.index file. The closest was qt-sdk.index.

With the release installation I first installed the debug. Then I emptied the "serialport-build" folder where I had build the debug. Then I moved ahead with the release installation. First ran the qmake. That went fine. Then during the nmake I got the "cannot open input file 'SerialPort.lib'" error. What did I do wrong?

Added after 7 minutes:

Got the release installed now.

kuzulis
16th July 2012, 13:38
With path to qt directory do you mean something like C:\QtSDK\Desktop\Qt\4.8.1\msvc2008?
Yes, something like that.


I didn't find any doc folder there. Checked the entire computer and no qt.index file. The closest was qt-sdk.index.
So you need to build the documentation will take the file qt.index from source Qt4.


What did I do wrong?
I wrote to you three ways to solve this problem.

Added after 9 minutes:

UPD: Try download documentation as html.zip here: http://www.freeupload.cn/download.php?file=56885

Kammersgaard
19th July 2012, 13:12
Got it all working now so thanks a lot for the help! Now I'm trying to run the program on another computer. Should I install the library again in the same way or is it possible to just copy some of the files that the first installation made? In that case where to?

kuzulis
24th July 2012, 08:19
Need copy to another computer necessary *.dll libraries Qt and QtSerialPort, put them in a folder with your *.exe file, provided that you work in Windows.
If you work in Linux - a little differently.

In any case - this topic is not relevant to this post. Look for a solution in Google.