PDA

View Full Version : Using QSerialPort in a dynamic library to be used with Lua



Siapran
18th May 2015, 10:05
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:
QObject::startTimer: Timers can only be used with threads started with QThread

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:


class SerialIO {

public:
SerialIO(std::string deviceName);
~SerialIO();

public:
virtual bool open();
virtual void close();
virtual std::string read(int len);
virtual std::string readLine();
virtual bool write(std::string data);

private:
QSerialPort m_port;
};


bool SerialIO::write(std::string data) {
if (m_port.isOpen()) {
return m_port.write(data.c_str(), data.length()) == data.length();
}
return false;
}

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


require "siema_cnx"
port = cnx.Serial("COM4")
port:open()
port:write(":RDREG 0\r\n")
answer = port:readLine()
print(answer)
port:close()

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.

Siapran
21st May 2015, 11:21
I guess I really got into a niche case because no one even dared to say anything about it.

Lesiok
21st May 2015, 11:44
Are You using threads ?

Siapran
21st May 2015, 13:36
no, or at least I'm not aware of using any.

d_stranz
27th May 2015, 00:14
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.

Siapran
1st June 2015, 09:10
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?