lemanhtrung / qextserialport

Automatically exported from code.google.com/p/qextserialport
Other
0 stars 0 forks source link

Not reading the serial port data fully using QExserialports readyread of singal slot #156

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
OS:Ubuntu 11
QExtSerialport 1.2 beta 

I am creating an application to write an instruction to a device and receive 
its response from the device. I connected the readyread() singal to 
onReadyRead() slot. but sometime its getting only the partial data from the 
device to the onReadyRead() slot.

I am writing *Read to device 
and getting
*Read<Values>
P=122
C=0.33
sometime I got only the partial result in my onReadyRead()

I tried event driven and Polling methods.Still the issue exist.How I can 
completely read the device input in my onReadyRead(). 
I checked this on putty software also. its working perfectly in there.

my code sample is given below

void Reader::Create()
{
    PortSettings settings = {BAUD19200, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 20};
    port = new QextSerialPort(m_strPortName, settings, QextSerialPort::EventDriven);

    timer = new QTimer(this);
    timer->setInterval(20);

     connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
     connect(port, SIGNAL(readyRead()), SLOT(onReadyRead()));

    if (!port->isOpen())
    {
       port->setPortName(m_strPortName);
        port->open(QIODevice::ReadWrite);
    }
    else {
     //   port->close();
    }

if (port->isOpen() && port->queryMode() == QextSerialPort::Polling)
       timer->start();
    else
        timer->stop();

}

void Reader::onReadyRead()
{
    if (port->bytesAvailable())
    {
       m_strData + port->readAll();
    }
}

What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by arunkuma...@gmail.com on 18 Jan 2013 at 9:46

GoogleCodeExporter commented 9 years ago
apologize me I forgot to remove that last comments

Original comment by arunkuma...@gmail.com on 18 Jan 2013 at 9:49

GoogleCodeExporter commented 9 years ago
Hi, 

do you mean some issue like this  
?http://stackoverflow.com/questions/14109522/why-this-works-like-this

Original comment by dbzhang...@gmail.com on 21 Jan 2013 at 1:19

GoogleCodeExporter commented 9 years ago
Yes..., i am getting the result like partial output

Original comment by arunkuma...@gmail.com on 21 Jan 2013 at 8:34

GoogleCodeExporter commented 9 years ago
Hi, 
could you please open the serial port with your application and then show me 
the output of follow command: 
stty -a -F "your_port_name"

I have the same issue and just curious about your settings. I have some ideas 
how to fix.

Original comment by vsla...@gmail.com on 22 Jan 2013 at 1:23

GoogleCodeExporter commented 9 years ago
I got something like this

arun@arun:~$ sudo stty -a -F "/dev/ttyUSB0" 
[sudo] password for arun: 
speed 19200 baud; rows 0; columns 0; line = 0;
intr = <undef>; quit = <undef>; erase = ^?; kill = ^U; eof = ^A; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = <undef>; stop = <undef>; susp = 
<undef>;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 0; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 
ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop 
-echoprt
-echoctl -echoke

Original comment by arunkuma...@gmail.com on 23 Jan 2013 at 4:42

GoogleCodeExporter commented 9 years ago
First off all, you need to realise that time-out value using this library is 
not what you probably expect. From documentation:
-------------------------------%<------------------------------------------
This is a per-character timeout, i.e. the port will wait this long for each 
individual character, not for the whole read operation.
------------------------------->%-------------------------------------------

The second important thing (also mentioned in documentation):
-------------------------------%<------------------------------------------
POSIX does not support millisecond-level control for I/O timeout values. Any 
timeout set using this function will be set to the next lowest tenth of a 
second for the purposes of detecting read or write timeout
------------------------------->%------------------------------------------

In the library code it looks like below: 
    currentTermios.c_cc[VTIME] = millisec/100;

That's why you have 'time = 0' in your stty output.
Two parameters, VTIME and VMIN are very important settings and there are four 
defined cases which differ in behaviour, let's look on man termios.h, you have 
them both zero:
-------------------------------%<------------------------------------------
 * MIN  ==  0;  TIME  == 0: If data is available, read(2) returns immediately, with the lesser of the number of bytes available, or the number of bytes requested.  If no data is available, read(2) returns 0.
------------------------------->%------------------------------------------

So in your case read returns as many bytes as already transmitted by the moment 
when you doing read. The easy solution for you is adding sleep after you 
transmitted message to wait for some time while reply is being received. You 
need to know what amount of time in you application is reasonable to wait for 
reply, probably it is 20 ms you are putting in port settings.
Please note, that providing higher value for time-out in port setting also 
might not work. In this case read also may return with less data than expected.

The right way to do it actually is using select system call in library for 
waiting data, it allows to implement "true" time-out for whole package like 
usually people expect to have looking to the name of the parameter. And also it 
should be another time-out for inter-char time. Right now it is broken design 
of the library on Linux platform for noncanonical mode :-(

Original comment by vsla...@gmail.com on 23 Jan 2013 at 10:23

GoogleCodeExporter commented 9 years ago
Did you solve this issue?
I am as well using 1.2beta2 and I am suffering of lost bytes sometimes. I have 
set event driven mode (where the timeout value does nothing according doc) with 
115k on Linux. 

Excerpt of my code:

void QTGUI_MainWindow::onComReadyRead()
{
    static QByteArray RawPortData;
  uint iBytesAvailable;

  while(port->bytesAvailable())
  {
    iBytesAvailable = port->bytesAvailable();
    RawPortData = port->read(iBytesAvailable);

    do
    { /* evaluate data */ }
  }
}

Original comment by franz....@gmail.com on 1 Jul 2014 at 1:26

GoogleCodeExporter commented 9 years ago
Tried to move to QSerialPort but this is suffering of delayed emiting of 
readyRead. So, I really need this solved.

Original comment by franz....@gmail.com on 15 Jul 2014 at 9:43