Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. [solved] incomplete data read by qserialdevice
Forum Updated to NodeBB v4.3 + New Features

[solved] incomplete data read by qserialdevice

Scheduled Pinned Locked Moved 3rd Party Software
39 Posts 9 Posters 25.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #8

    dear arianoo
    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.

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      arianoo
      wrote on last edited by
      #9

      hi Koahnig
      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.
      thanks again
      wish to get more answers from you;)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #10

        Hi arianoo
        out of curiosity what is the baud rate of your serial device?

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          arianoo
          wrote on last edited by
          #11

          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.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #12

            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.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              arianoo
              wrote on last edited by
              #13

              yeah you're right and I'm still looking for a reliable way for reading correct data from serial ports i really need it

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #14

                Are you doing the reading of the device in a separate thread?

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  arianoo
                  wrote on last edited by
                  #15

                  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?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #16

                    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.

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      arianoo
                      wrote on last edited by
                      #17

                      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

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #18

                        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 ;-)

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          abhijsj
                          wrote on last edited by
                          #19

                          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..

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            arianoo
                            wrote on last edited by
                            #20

                            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

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              abhijsj
                              wrote on last edited by
                              #21

                              Hey Arianoo i had already mailed a copy of my code to you you can check that out.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                abhijsj
                                wrote on last edited by
                                #22

                                @
                                #include "dialog.h"
                                #include "ui_dialog.h"
                                #include <QDebug>
                                #include "qserialdevice_global.h"
                                #include "abstractserial.h"
                                QByteArray ba;
                                QByteArray ba1;
                                AbstractSerial *port;
                                AbstractSerial *port1;
                                Dialog::Dialog(QWidget *parent) :
                                QDialog(parent),
                                ui(new Ui::Dialog)
                                {

                                timer = new QTimer(this);
                                

                                // timer1 = new QTimer(this);
                                udpsocket = new QUdpSocket(this);
                                messageNo = 1;
                                ui->setupUi(this);
                                connect(ui->sstartButton, SIGNAL(clicked()), this, SLOT(startserialrecv()));
                                connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
                                connect(ui->sstartButton_2, SIGNAL(clicked()),this, SLOT(ttlopen()));
                                connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(close()));
                                connect(ui->closebutton,SIGNAL(clicked()),this,SLOT(closeport()));
                                connect(ui->closebutton_2,SIGNAL(clicked()), this, SLOT(ttlclose()));
                                connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
                                //connect(timer1, SIGNAL(timeout()), this, SLOT(startserialrecv()));

                                }
                                void Dialog::startBroadcasting()
                                {
                                ui->startButton->setEnabled(false);
                                timer->start(100);
                                }

                                void Dialog::startserialrecv()
                                {
                                port = new AbstractSerial(this);
                                connect( port, SIGNAL(readyRead()), this, SLOT(slotread()));
                                int rrto = 10;
                                int len = 100;
                                char *dn="/dev/ttyS0";
                                port->setDeviceName("/dev/ttyS0");
                                bool ret = port->open(AbstractSerial::ReadOnly);
                                if(ret)
                                {
                                qDebug("Port Opened Successfully");
                                if (!port->setBaudRate(AbstractSerial::BaudRate19200))
                                {
                                qDebug() << "Set baud rate " << AbstractSerial::BaudRate19200 << " error.";

                                   };
                                
                                   if (!port->setDataBits(AbstractSerial::DataBits8))
                                   {
                                       qDebug() << "Set data bits " <<  AbstractSerial::DataBits8 << " error.";
                                
                                   }
                                
                                   if (!port->setParity(AbstractSerial::ParityEven))
                                   {
                                       qDebug() << "Set parity " <<  AbstractSerial::ParityEven << " error.";
                                
                                   }
                                
                                   if (!port->setStopBits(AbstractSerial::StopBits1))
                                   {
                                       qDebug() << "Set stop bits " <<  AbstractSerial::StopBits1 << " error.";
                                
                                   }
                                
                                   if (!port->setFlowControl(AbstractSerial::FlowControlOff))
                                   {
                                       qDebug() << "Set flow " <<  AbstractSerial::FlowControlOff << " error.";
                                
                                   }
                                    if (port->waitForReadyRead(rrto))
                                        {
                                           /* ba = port->readAll();
                                            qDebug(ba);
                                            ui->textEdit1->setText(ba);
                                            ui->textEdit1->setText("Data Transmitted is: \"%1\"");
                                            qDebug(port->readAll());
                                         */}
                                         else
                                         {
                                                qDebug() << "Timeout read data in time : " << QTime::currentTime();
                                         }
                                     }
                                else
                                {
                                    qDebug("Serial Port Opening Failed");
                                }
                                

                                }
                                void Dialog::slotread()
                                {
                                ba = port->readAll();
                                qDebug()<<"Data Rcvd :"<<ba;
                                ui->textEdit1->setText(ba);
                                qDebug() << "Readed is : " << ba.size() << " bytes";

                                }
                                void Dialog::closeport()
                                {
                                port->close();
                                qDebug() << "Serial device " << port->deviceName() << " is closed";
                                delete port;
                                port = 0;
                                }
                                void Dialog::ttlopen()
                                {
                                port1 = new AbstractSerial(this);
                                connect( port1, SIGNAL(readyRead()), this, SLOT(slotttlread()));

                                int rrto = 10;
                                int len = 100;
                                char *dn="/dev/ttyS1";
                                port1->setDeviceName("/dev/ttyS1");
                                

                                // connect( port, SIGNAL(readyRead()), this, SLOT(readDataSlot()) );

                                //port->openMode(QIODevice::OpenMode);
                                bool ret = port1->open(AbstractSerial::ReadOnly);
                                   if(ret)
                                    {
                                   qDebug("Port Opened Successfully");
                                
                                   //Here, the default current parameters (for example)
                                   if (!port1->setBaudRate(AbstractSerial::BaudRate115200))
                                       {
                                       qDebug() << "Set baud rate " <<  AbstractSerial::BaudRate115200 << " error.";
                                
                                   };
                                
                                   if (!port1->setDataBits(AbstractSerial::DataBits8))
                                   {
                                       qDebug() << "Set data bits " <<  AbstractSerial::DataBits8 << " error.";
                                
                                   }
                                
                                   if (!port1->setParity(AbstractSerial::ParityNone))
                                   {
                                       qDebug() << "Set parity " <<  AbstractSerial::ParityNone << " error.";
                                
                                   }
                                
                                   if (!port1->setStopBits(AbstractSerial::StopBits1))
                                   {
                                       qDebug() << "Set stop bits " <<  AbstractSerial::StopBits1 << " error.";
                                
                                   }
                                
                                   if (!port1->setFlowControl(AbstractSerial::FlowControlOff))
                                   {
                                       qDebug() << "Set flow " <<  AbstractSerial::FlowControlOff << " error.";
                                
                                   }
                                
                                   /* if (port1->waitForReadyRead(rrto))
                                        {
                                            ba = port->readAll();
                                            qDebug(ba);
                                            ui->textEdit1->setText(ba);
                                            ui->textEdit1->setText("Data Transmitted is: \"%1\"");
                                            qDebug(port->readAll());
                                         }
                                         else
                                         {
                                                qDebug() << "Timeout read data in time : " << QTime::currentTime();
                                         }*/
                                     }
                                
                                else
                                {
                                    qDebug("Serial Port Opening Failed");
                                }
                                

                                }
                                void Dialog::slotttlread()
                                {
                                ba1 = port1->readAll();
                                qDebug()<<"Data Rcvd :"<<ba1;
                                ui->textEdit1_2->setText(ba1);
                                qDebug() << "Readed is : " << ba1.size() << " bytes";
                                }

                                   void Dialog::ttlclose()
                                   {
                                       port1->close();
                                       qDebug() << "Serial device " << port1->deviceName() << " is closed";
                                       delete port1;
                                       port1 = 0;
                                
                                   }
                                

                                void Dialog::broadcastDatagram()
                                {

                                 QByteArray datagram = ba+ QByteArray::number(messageNo);
                                // QByteArray datagram = ba+ "123456";
                                 udpsocket->writeDatagram(datagram.data(), datagram.size(),
                                                          QHostAddress::Broadcast, 5000);
                                 ++messageNo;
                                 ui->textEdit->setText(tr("Sending data: \"%1\"").arg(datagram.data()));
                                 qDebug("Data is being transmitted");
                                

                                }
                                @

                                [EDIT: code formatting, please wrap in @-tags, Volker]

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  koahnig
                                  wrote on last edited by
                                  #23

                                  [quote author="abhijsj" date="1323510423"]Hey Arianoo i had already mailed a copy of my code to you you can check that out.
                                  [/quote]

                                  You need to use '@' as tag for starting and ending a section with source code. Otherwise it is not readable (see "here":http://developer.qt.nokia.com/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 ) .

                                  In addition it would have been better to start a new thread for your issue. This would attract more likely others to help you. Unfortunately, I cannot split threads.

                                  Vote the answer(s) that helped you to solve your issue(s)

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    abhijsj
                                    wrote on last edited by
                                    #24

                                    @#include "dialog.h"
                                    #include "ui_dialog.h"
                                    #include <QDebug>
                                    #include "qserialdevice_global.h"
                                    #include "abstractserial.h"
                                    QByteArray ba;
                                    QByteArray ba1;
                                    AbstractSerial *port;
                                    AbstractSerial *port1;
                                    Dialog::Dialog(QWidget *parent) :
                                    QDialog(parent),
                                    ui(new Ui::Dialog)
                                    {

                                    timer = new QTimer(this);
                                    

                                    // timer1 = new QTimer(this);
                                    udpsocket = new QUdpSocket(this);
                                    messageNo = 1;
                                    ui->setupUi(this);
                                    connect(ui->sstartButton, SIGNAL(clicked()), this, SLOT(startserialrecv()));
                                    connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
                                    connect(ui->sstartButton_2, SIGNAL(clicked()),this, SLOT(ttlopen()));
                                    connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(close()));
                                    connect(ui->closebutton,SIGNAL(clicked()),this,SLOT(closeport()));
                                    connect(ui->closebutton_2,SIGNAL(clicked()), this, SLOT(ttlclose()));
                                    connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
                                    //connect(timer1, SIGNAL(timeout()), this, SLOT(startserialrecv()));

                                    }
                                    void Dialog::startBroadcasting()
                                    {
                                    ui->startButton->setEnabled(false);
                                    timer->start(100);
                                    }

                                    void Dialog::startserialrecv()
                                    {
                                    port = new AbstractSerial(this);
                                    connect( port, SIGNAL(readyRead()), this, SLOT(slotread()));
                                    int rrto = 10;
                                    int len = 100;
                                    char *dn="/dev/ttyS0";
                                    port->setDeviceName("/dev/ttyS0");
                                    bool ret = port->open(AbstractSerial::ReadOnly);
                                    if(ret)
                                    {
                                    qDebug("Port Opened Successfully");
                                    if (!port->setBaudRate(AbstractSerial::BaudRate19200))
                                    {
                                    qDebug() << "Set baud rate " << AbstractSerial::BaudRate19200 << " error.";

                                       };
                                    
                                       if (!port->setDataBits(AbstractSerial::DataBits8))
                                       {
                                           qDebug() << "Set data bits " <<  AbstractSerial::DataBits8 << " error.";
                                    
                                       }
                                    
                                       if (!port->setParity(AbstractSerial::ParityEven))
                                       {
                                           qDebug() << "Set parity " <<  AbstractSerial::ParityEven << " error.";
                                    
                                       }
                                    
                                       if (!port->setStopBits(AbstractSerial::StopBits1))
                                       {
                                           qDebug() << "Set stop bits " <<  AbstractSerial::StopBits1 << " error.";
                                    
                                       }
                                    
                                       if (!port->setFlowControl(AbstractSerial::FlowControlOff))
                                       {
                                           qDebug() << "Set flow " <<  AbstractSerial::FlowControlOff << " error.";
                                    
                                       }
                                        if (port->waitForReadyRead(rrto))
                                            {
                                               /* ba = port->readAll();
                                                qDebug(ba);
                                                ui->textEdit1->setText(ba);
                                                ui->textEdit1->setText("Data Transmitted is: \"%1\"");
                                                qDebug(port->readAll());
                                             */}
                                             else
                                             {
                                                    qDebug() << "Timeout read data in time : " << QTime::currentTime();
                                             }
                                         }
                                    else
                                    {
                                        qDebug("Serial Port Opening Failed");
                                    }
                                    

                                    }
                                    void Dialog::slotread()
                                    {
                                    ba = port->readAll();
                                    qDebug()<<"Data Rcvd :"<<ba;
                                    ui->textEdit1->setText(ba);
                                    qDebug() << "Readed is : " << ba.size() << " bytes";

                                    }
                                    void Dialog::closeport()
                                    {
                                    port->close();
                                    qDebug() << "Serial device " << port->deviceName() << " is closed";
                                    delete port;
                                    port = 0;
                                    }
                                    void Dialog::ttlopen()
                                    {
                                    port1 = new AbstractSerial(this);
                                    connect( port1, SIGNAL(readyRead()), this, SLOT(slotttlread()));

                                    int rrto = 10;
                                    int len = 100;
                                    char *dn="/dev/ttyS1";
                                    port1->setDeviceName("/dev/ttyS1");
                                    

                                    // connect( port, SIGNAL(readyRead()), this, SLOT(readDataSlot()) );

                                    //port->openMode(QIODevice::OpenMode);
                                    bool ret1 = port1->open(AbstractSerial::ReadOnly);
                                       if(ret1)
                                        {
                                       qDebug("Port Opened Successfully");
                                    
                                       //Here, the default current parameters (for example)
                                       if (!port1->setBaudRate(AbstractSerial::BaudRate115200))
                                           {
                                           qDebug() << "Set baud rate " <<  AbstractSerial::BaudRate115200 << " error.";
                                    
                                       };
                                    
                                       if (!port1->setDataBits(AbstractSerial::DataBits8))
                                       {
                                           qDebug() << "Set data bits " <<  AbstractSerial::DataBits8 << " error.";
                                    
                                       }
                                    
                                       if (!port1->setParity(AbstractSerial::ParityNone))
                                       {
                                           qDebug() << "Set parity " <<  AbstractSerial::ParityNone << " error.";
                                    
                                       }
                                    
                                       if (!port1->setStopBits(AbstractSerial::StopBits1))
                                       {
                                           qDebug() << "Set stop bits " <<  AbstractSerial::StopBits1 << " error.";
                                    
                                       }
                                    
                                       if (!port1->setFlowControl(AbstractSerial::FlowControlOff))
                                       {
                                           qDebug() << "Set flow " <<  AbstractSerial::FlowControlOff << " error.";
                                    
                                       }
                                    
                                       /* if (port1->waitForReadyRead(rrto))
                                            {
                                                ba = port->readAll();
                                                qDebug(ba);
                                                ui->textEdit1->setText(ba);
                                                ui->textEdit1->setText("Data Transmitted is: \"%1\"");
                                                qDebug(port->readAll());
                                             }
                                             else
                                             {
                                                    qDebug() << "Timeout read data in time : " << QTime::currentTime();
                                             }*/
                                         }
                                    
                                    else
                                    {
                                        qDebug("Serial Port Opening Failed");
                                    }
                                    

                                    }
                                    void Dialog::slotttlread()
                                    {
                                    ba1 = port1->readAll();
                                    qDebug()<<"Data Rcvd :"<<ba1;
                                    ui->textEdit1_2->setText(ba1);
                                    qDebug() << "Readed is : " << ba1.size() << " bytes";
                                    }

                                       void Dialog::ttlclose()
                                       {
                                           port1->close();
                                           qDebug() << "Serial device " << port1->deviceName() << " is closed";
                                           delete port1;
                                           port1 = 0;
                                    
                                       }
                                    

                                    void Dialog::broadcastDatagram()
                                    {

                                     QByteArray datagram = ba+ QByteArray::number(messageNo);
                                    // QByteArray datagram = ba+ "123456";
                                     udpsocket->writeDatagram(datagram.data(), datagram.size(),
                                                              QHostAddress::Broadcast, 5000);
                                     ++messageNo;
                                     ui->textEdit->setText(tr("Sending data: \"%1\"").arg(datagram.data()));
                                     qDebug("Data is being transmitted");
                                    

                                    }@

                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      koahnig
                                      wrote on last edited by
                                      #25

                                      There has been a response in another "thread concerning QSerialDevice":http://developer.qt.nokia.com/forums/viewthread/11634/#67246

                                      Vote the answer(s) that helped you to solve your issue(s)

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        arianoo
                                        wrote on last edited by
                                        #26

                                        hi abhijsj
                                        ive looked at your code and im not sure what wxactly makes your problem but there are somethings i can mention:

                                        @ if (port->waitForReadyRead(rrto))
                                        {
                                        /* ba = port->readAll();
                                        qDebug(ba);
                                        ui->textEdit1->setText(ba);
                                        ui->textEdit1->setText("Data Transmitted is: "%1"");
                                        qDebug(port->readAll());
                                        */} @

                                        first of all i think this part on line 78 should be ommited.
                                        also i think theres no need to lines 11-15 in your main.cpp code.
                                        another point is, its better to put line 114 after opening your port in line 119 of the code.
                                        try this and tell the result and ill explore with more attention in your code maybe i can find the problem.

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          arianoo
                                          wrote on last edited by
                                          #27

                                          now there's a question i have:
                                          how did u use the close() in a method different from the one that created port?
                                          I mean i can only access port1->close() if im in the slot that i created port = new abstractserial(); i dont know how u got access to that from another method im so keen to know it plz answer me because i get segmentation fault when calling close() from another method in the same class ???

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved