[solved] incomplete data read by qserialdevice
-
dear abhijsj
thank u so much u really helped me. my problem was that I created the ports in the constructor of my classes and so i couldn't get access to the close slot anywhere else in the same class but now that i create the port and open it outside of the constructor, everything works fine with closing the port:)
about your question, it's absolutely possible to communicate with two different ports at the same time so easily. the program I'm working on now is also doing this and i have never faced any problem with having two ports opened and i think your program can also do it correctly if you pay attention to some points.
as my experience says,if u press sstartButton or sstartButton2 two times your port wont get opened again i handled this problem by opening it the third time if it isnt opened yet as this:
@port = new AbstractSerial(); port->setDeviceName("/dev/ttyS1"); port->open(AbstractSerial::ReadWrite); if (!port->openMode()) port->open(AbstractSerial::ReadWrite ) }
@
this way you will get sure each time you call the slot to open the port your port will be opened.
next i think its a bit important that you first creat the port, then assign a name to it, then assign its properties and at last connect its signal to a slot. i think u should be careful about the order(im not sure).
I cant find any other problem that prevents u from getting data from both ports at the same time because i'm doing the same thing and my two ports work simultanously and i havent seen any problem in it. u can also debug your program and say where it doesnt respond correctly. -
Use QExtSerialPort, download it and just build the library and link it to your application
Heres how I read the serial port instead of events (which can get hairy when parsing a lot of data)
Start a while loop
Set and start a timer for X milliseconds so when it fires, it changes the state of the while loop variable so you can exit the while loop
Check for data on the port by using bytesavailable()
Sit in while loop for a little while and keep reading the port until no more data is available or until a delimiter is reached
use QCoreApplication::processEvents() while in the while loop so the gui doesn't freeze upBoom, timer fires, you delayed X mS to get all the data from where ever
Carry on with what you want to do.
-
What u say about events making problem is quite correct ive even seen some cases in my own application but the method you are talking about makes lots of time loss which is very important in my case and i cant ignore it. i should write the program in a way that it works as fast as possible and i guess that i can achieve it via signals and events but maybe i'm wrong and using a while loop when needed to read data till its complete is better? Im getting curious about which way will make the program work faster???????
-
I guess it depends, do you know the size of the data that your expecting? Is there a delimiter on your string that you are expecting through the serial? You could do some simple timing tests to optimize your program.
Either that, or when you get an event that data is on your port, then sit in a while and read, and repeat my previous suggestion.
-
We strongly recommend using QSerialDevice 2.0.
People, you have ever read that thread "link":http://developer.qt.nokia.com/forums/viewthread/11634/#67246, which showed by Mr. koahnig?
I recommend to read.
-
yea I'm using QSerialDevice and i hope to get better in working with this class because 90% of my application is about working with serial ports and its so important for me. dvez yes i can find the size of coming data by its third byte coming (if i get the first third bytes exactly as theyre sent) and i can stay in the while loop till i get as much data as i need and i can also call it when i send something and im waiting to get answer.do u think it'll work faster and ill be able to read data without getting it damaged?
-
[quote author="arianoo" date="1323964918"]yea I'm using QSerialDevice and i hope to get better in working with this class because 90% of my application is about working with serial ports and its so important for me. dvez yes i can find the size of coming data by its third byte coming (if i get the first third bytes exactly as theyre sent) and i can stay in the while loop till i get as much data as i need and i can also call it when i send something and im waiting to get answer.do u think it'll work faster and ill be able to read data without getting it damaged? [/quote]
Did you update to QSerialdevice 2.0 ?
-
wow thanks alot for mentioning it i thought that im using the latest version but i wasnt! i did it recently and havent faced anything different up to now just a question why do u think the "setcharintervaltimeout" slot which was i think effective on reading data omitted in this version do u believe that it wont get me into any trouble?
-
I had updated to qserialdevice 2.0 and i am able to open multiple ports through this library.
While reading data i faced a problem that if i am sending 10 bytes of data it is read as 8 bytes,2 bytes. Data is always received in fragments of 8 bytes only. I have checked all the examples of qserialdevice 2.0 all of them have the same problem. I dont know how to overcome this problem .Someone please guide me.
Thanks in Advance
-
Abhishek,
Do not torture the brain, I have already explained to you by e-mail the cause, and that in this situation needs to be done, what approaches to use.Final decision of your problems, no one will give you.
[quote]
While reading data i faced a problem that if i am sending 10 bytes of data it is read as 8 bytes,2 bytes.
[/quote]
This is not a problem. This is normal behavior for non-blocking data is received.Is it hard to think of, like this to such a:
[code]
class Reader : public QObject
{
Q_OBJECT
signals:
void onePacketReceivedComplete(const QByteArary &packet);public:
enum { ExpectedResponseSize = 78 // Your expected size of the incoming packet }; Reader() { m_port = new SerialPort(this); connect(m_port, SIGNAL(readyRead(), this, SLOT(checkAvailable())); ... // Open and configure port ... m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(processRead())); m_timer->setInterval(10); // Maximum interval a wait end of packet, 10 msec (for example) ... }
private slots:
void checkAvailable() { if (m_port->bytesAvailable() < ExpectedResponseSize) { if (!m_timer->isActive()) m_timer->start(); } else { processRead(); } void processRead() { m_timer->stop(); // Here search a marker/header field in beginning of the packet // (use peek(), example) or etc, // ie, is synchronized with the input packets // (looking for begin a valid package from stream). ... ... QByteArray incomingData = m_port->read(ExpectedResponseSize); // Here check CRC and etc ... ... // If all checks are successful - it returns the packet. if (isOk) { emit onePacketReceivedComplete(incomingData); } else { // do } }
private:
SerialPort *m_port;
QTimer *m_timer;
};
[/code]???