Results 1 to 6 of 6

Thread: Using QSerialPort in a dynamic library to be used with Lua

  1. #1
    Join Date
    May 2015
    Location
    Lyon - France
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Using QSerialPort in a dynamic library to be used with Lua

    Hello,

    I've been trying for the past few days to create a Lua library that uses QSerialPort.
    It works, but I've run into the following problem. Every time I call QSerialPort::write, I get the following warning:
    Qt Code:
    1. QObject::startTimer: Timers can only be used with threads started with QThread
    To copy to clipboard, switch view to plain text mode 

    My guess is that Lua uses its own threads, so I don't really know how to fix that.
    For reference, here is a sample of what the code looks like:

    Qt Code:
    1. class SerialIO {
    2.  
    3. public:
    4. SerialIO(std::string deviceName);
    5. ~SerialIO();
    6.  
    7. public:
    8. virtual bool open();
    9. virtual void close();
    10. virtual std::string read(int len);
    11. virtual std::string readLine();
    12. virtual bool write(std::string data);
    13.  
    14. private:
    15. QSerialPort m_port;
    16. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool SerialIO::write(std::string data) {
    2. if (m_port.isOpen()) {
    3. return m_port.write(data.c_str(), data.length()) == data.length();
    4. }
    5. return false;
    6. }
    To copy to clipboard, switch view to plain text mode 

    The class is then registered into a Lua state, so that I can use it as a Lua module:

    Qt Code:
    1. require "siema_cnx"
    2. port = cnx.Serial("COM4")
    3. port:open()
    4. port:write(":RDREG 0\r\n")
    5. answer = port:readLine()
    6. print(answer)
    7. port:close()
    To copy to clipboard, switch view to plain text mode 

    The code works fine and I get the expected results, but Qt prints a warning I have no idea of how to get rid of.

  2. #2
    Join Date
    May 2015
    Location
    Lyon - France
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QSerialPort in a dynamic library to be used with Lua

    I guess I really got into a niche case because no one even dared to say anything about it.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,459
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QSerialPort in a dynamic library to be used with Lua

    Are You using threads ?

  4. #4
    Join Date
    May 2015
    Location
    Lyon - France
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QSerialPort in a dynamic library to be used with Lua

    no, or at least I'm not aware of using any.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,113
    Thanks
    235
    Thanked 655 Times in 645 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Using QSerialPort in a dynamic library to be used with Lua

    I know this is a few days late, but I suspect the warning is because you do not have a Qt event loop running. QApplication::exec() starts an event loop (which is a QThread). If you don't have a QApplication in your Lua environment, then it is likely you aren't running from within a QThread.

  6. #6
    Join Date
    May 2015
    Location
    Lyon - France
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QSerialPort in a dynamic library to be used with Lua

    Yes, because Lua runs on a non-Qt thread. Now my question is: how can I fix it so that I have an event loop running?

Similar Threads

  1. Dynamic library on Mac, Library not loaded
    By grayfox in forum Newbie
    Replies: 2
    Last Post: 2nd July 2011, 02:42
  2. Dynamic library with GUI for Mac
    By mouse_sonya in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2010, 12:23
  3. QTForm in QT Dynamic Library
    By Kokos in forum Newbie
    Replies: 1
    Last Post: 17th February 2010, 15:56
  4. Linking Qt in a dynamic library
    By dave_mm0 in forum Qt Programming
    Replies: 4
    Last Post: 18th July 2009, 16:28
  5. How to use a Dynamic Link Library with QT / C++.
    By nivaldonicolau in forum Newbie
    Replies: 5
    Last Post: 29th April 2009, 14:05

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.