[solved] incomplete data read by qserialdevice
im using QSerialDevice to read some data on my serial port in linux in unbuffered mode because when i use buffered mode the readall() function returns 2 or 3 times of the answer sometimes,and i cant handle this repeating.but in the unbuffered mode when i use readall() function (as i dont know how much data im gonna get) the data is sometimes incomplete and most of the times changed and damaged and i cant retrieve the correct data.I have searched through threads and havent found any answer.do u think using qextserial port will help me better in linux or does anybody have any idea about what i can do to get the correct data when it is on my serial port?
another issue to add is, when i run my program in debugging mode and just wait before reading the port right when the readyread signal is emitted, i can get the correct data.also if I put a 3 second delay befor reading the data when signal is emitted, most of the times the correct data is read correctly. what can u get from this point? what do u think will help me?
So, AFAIK trhere is no general way to know, whetrher all data is available in a serial port. If you call readAll, you will get all data, that is available now. but some bits might not be retreived now, so no guarantee for fully available data. A sleep just blocks you from reading, so data send might be finished. It totally depends on the device you put on a serial port to get to a solution on how to know, data is complet.
yes i have put some interface devices and i know the data on the port is complete and as I said most of the times the data is damaged and has even the correct size.it means that when i get notified that data is available on the port by ready read and call readAll immediately,it reads the data incorrectly while i see what there is on my port the function has changed it.what should i do?
If your data is damaged, are you sure you are using the right connection flags & speed?
check with another application to test your device. maybe your device is sending incorrect data or as Andre said baudrates are not same as each other.
[quote author="arianoo" date="1321504770"]it means that when i get notified that data is available on the port by ready read and call readAll immediately,it reads the data incorrectly while i see what there is on my port the function has changed it.what should i do?[/quote]
The readyRead signal is issued when some data ist available. This does not mean the sequence is already completed. As a matter of fact with low baud rates it might take considerable time to receive even short sequences.
As I understand your data is partially corrupted, but not completely. This does not sound like a wrong baud rate, but may be other settings as suggestesd above.
The question is what you are trying to achieve. If you want to communicate as in a dialog with a device, the approach with readAll may be feasible. Providing that the sequences send by the other device are reasonably short. You may wait some time after you have received readyRead and then start reading (e.g. readAll).
However, in my opinion this approach will be very fast at its limits. If you have varying length of information which cannot be determined beforehand. Especially, if you are doing some real-time communication with some sensor sending information at specific rate but different information length, you will run into trouble.
I would read the device in smaller chunks and store the data in a buffer myself. You may do this bytewise or in chunks based on the return of bytesAvailable.
thank u all so much for considering my question and answering it. The settings of the port are all correct as i am able to communicate with devices by the commands i send to them and i also monitor the port by an interface device with the same settings and it works correctly and the problem is absolutely the way i read data from the port.
dear koahnig u have mentioned many points that can help me understand how serial ports work ,the bytesAvailable function always returns exactly the number of bytes that readAll would read and i have tried to read that size of data each time with read() function and it hasnt helped either.my problem with waiting is that for a good result and a high percentage of getting correct data i have to wait 3 seconds every time something is available on my port and it makes my program run too slow.i was using this approach up to last week but its really impossible to have this much delay. the way that has answered for me by now and is not yet reliable at all, is that i am opening the port in buffered mode and calling readall. most of the times it returns the exact data which is available on the port but this also has its own problems.
Im truely confused with this issue i have to work with two different devices through serial port and getting correct data from them is so important for me:(
ill be thankfull if u tell me the ideas that come to ur mind so that my problem gets fixed.
thanks alot for your attention
at first I like to mention two major differences. The experiences I collected on windows and wince. Futhermore, I had retrieved an old version of QExtSerialDevice. The project has been part of SourceForge at a time. Later I have found out that the project has been moved to google.
I just saw that you are refering to QSerialDevice. I do not know if the roots are the same. However, it seem to be newer than QExtSerialDevice.
I have a program reading data from a device in real-time. The information is of the form prefix-data-suffix. Initially the program used only TCP/IP for reading the data. It basically scans the input for the starting bytes of the prefix where also the also the complete record length can be found. This led to reading the input bytewise until the starting character found. The remainder will be read bytewise as well until bytesAvailable are read or the complete record has been received. The collected record will be processed immediately. Initially QExtSerialDevice could substitute the QTcpSocket since both are derived from QIODevice. The only difference was the opposite definition of atEnd() in QExtSerialDevice. Later I ported to WinCE where the API functions are completely different. Nevertheless, I maintained the bytewise copying process. A port to linux never happened and probably will never.
Since the information records are rather short, it was never an issue with TCP/IP. Depending on the baudrate it becomes an issue with serial comms. I personally would prefer the bytewise reading especialyl for my application. I need to buffer anyway, because half a record is a lost record.
I have no experience with QSerialDevice. Probably the readAll implementation is save, but I would be careful until I know. Only the developers working on it can tell what is happening during readAll and additional information is received. Or you have to dig into that yourself.
I'm really thankful for your post i read it 10 days ago but didn't have time to answer.as your advice I started using buffered mode but I still have to add some delay before reading long data because if i read it immediately it gets damaged I also increased the time of setcharintervaltimeout which i don't know what exactly it does and i didn't realize if it changed anything.I want to thank u again cuz your ideas and information u wrote were truely helpful for me.
I also want to ask people who have worked with serial port about the best way to read data from it and about the meaning of setcharintervaltimeout and such settings.
wish to get more answers from you;)
out of curiosity what is the baud rate of your serial device?
I'm connecting to two devices at the same time via 2 ports. one with baud rate 115200 which is most of the times w/o problems and the other one with baud rate 9600 that's killing me;) that device was a little different before and I had to communicate with it with 2400 baud and it was much more suffering but now with 9600 problems are less than before but still in getting long messages I'm having problem and haven't found any solutions so far.
Higher baud rates seem to be easier to handle.
At first I used only fast baud rates. there was not an issue with reading a complete record at a time.
Suddenly another device provided "only" 9600 baud. That was the point when buffering was getting important.
yeah you're right and I'm still looking for a reliable way for reading correct data from serial ports i really need it
Are you doing the reading of the device in a separate thread?
I'm reading it by events and also i stay in the current event to get my answer by waitforreadyread() do u think it might be a problem?
I would have to know the implementation in detail to give a proper answer. And I do not. Also I am using a different implementation (QExtSerialDevice).
However, my application is reading from the serial port in a different thread.
i think the event driven application im using is working the same as if it was on another thread and i think im aware of where my program is living while executing the problem is that sometimes the signal of the readyread event is raised before it truly ready i guess
You might be right. I saw an implementation with different threads for reading and writing on the device. Since I have used it as a guideline, I have implemented in a separate thread as well. It could well be over-engineering, but it does not have the problems on lower baud rates. At least I do not know of such problems ;-)
Hi arianoo even i am using qserialdevice for serial data communication.
I am connecting two devices on two diff ports one is running at 19200 baud and another at 4800 baud.
but i am facing a serious problem ie when one port is open and at the same time if i am opening the other port it is unable to open it.
I have taken two different instances of abstractserial class but i am unable to open both the ports at the same time..
if i close the second port and then i am opening the other port then i am able to read the data successfully..
i just caunt get where i am stuck. if you could guide me then i would be able to sort this problem out..
i am doing the same way also im openning two ports by two instances of qabstractserial and i have never had such problem maybe in some settings of your system there's a limitation for the number of open serial ports or maybe ur using the same name for both instances u create(???) or maybe something else is the problem because i never faced this issue u can also put a piece of ur program so that people on the forum see it maybe the answer will be found