Results 1 to 11 of 11

Thread: how to i synchronize serial read and write without using thread?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to i synchronize serial read and write without using thread?

    here are some portion of my code:

    //MaiWindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. timer_for_read=new QTimer(this); //this timer is used for calling continuous_monitor()
    7. QObject::connect(timer_for_read,SIGNAL(timeout()),this,SLOT(continuous_monitor()));
    8. QObject::connect(timer_for_read,SIGNAL(timeout()),timer_for_read,SLOT(start()));//didnt know ways to restart timer again.
    9. start_check[0]=0;
    10.  
    11. }
    12. void MainWindow::on_Start1_clicked()
    13. {
    14. if(!modbus_master.port_init)
    15. modbus_master.init_port();
    16. modbus_master.data_buffer.Reset();//these are for data
    17. modbus_master.data_buffer.Append(50);
    18. while (modbus_master.CheckBusy());
    19. modbus_master.WriteOutputReg(10,1000);
    20. start_check[0]=1;
    21. qDebug()<<timer_for_read->isActive();
    22. if(!timer_for_read->isActive())
    23. timer_for_read->start(5000);
    24. }
    25. void MainWindow::continuous_monitor()
    26. {
    27. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);//dint know where to do this
    28. if(start_check[0]){
    29. while(modbus_master.CheckBusy());// this checks whether the code is listening to response, returns 1 when listening is done
    30. modbus_master.ReadInputReg(10,1000,4);// reads input register// this writes a message to serial port and listens to response
    31. }
    32. if(start_check[1]){
    33. while(modbus_master.CheckBusy());
    34. modbus_master.ReadInputReg(10,1000,4);
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    //ModbuMaster.cpp
    Qt Code:
    1. ModbusMaster::ModbusMaster()
    2. {
    3. timer=new QTimer(this);
    4. QObject::connect(timer,SIGNAL(timeout()),this,SLOT(onTimeOut()));// onTimeout asserts that the response is invalid
    5. QObject::connect(this,SIGNAL(WriteEmit(char*)),this,SLOT(writeData(char*)));
    6. port=new QextSerialPort();
    7. QObject::connect(port,SIGNAL(readyRead()),this,SLOT(readData()));
    8. }
    9.  
    10. void ModbusMaster::readData()
    11. {
    12. qDebug("read data");
    13. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
    14. port->flush();
    15. // char buff[1024];
    16. // qDebug("read data");
    17. QByteArray response=port->readAll();
    18. for(int i=0;i<response.length();i++)
    19. {
    20. ReadCharacterCB((uint8_t)response[i]); //this function has intrepretation of data
    21. }
    22. }
    23. void ModbusMaster::writeData(char* msg)
    24. {
    25. //qDebug("Writing data");
    26. port->write(msg,1);
    27. port->flush();
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 24th January 2014 at 09:02. Reason: add code tags

Similar Threads

  1. qext-serial-port write problem.
    By rex in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2013, 08:18
  2. Replies: 2
    Last Post: 2nd November 2010, 06:15
  3. Replies: 4
    Last Post: 10th July 2010, 18:34
  4. Replies: 1
    Last Post: 16th June 2009, 10:09
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.