I created an instance of RS class inside MainWindow. when fd is opened, it gives the value of 19 inside debugger. But, when I access it from inside of the Thread, it always gives value 0.
this is my main file: it initializes a thread that regularly generates packets inside queue as producer, and consumer on the other end, writes them to serial.
serial port is opened in Mainwindow constructor:
rs_plc.rs_plcOpenPort((char *)"/dev/ttyS0"); /*/dev/ttyS3*/
rs_plc.rs_plcOpenPort((char *)"/dev/ttyS0"); /*/dev/ttyS3*/
To copy to clipboard, switch view to plain text mode
ThreadSafeQueue<QByteArray> UpdateJackQueue;
PlcUpdateThread *plcUpdateProd = new PlcUpdateThread(UpdateJackQueue, 1);
plcUpdateProd->start();
PlcUpdateThread *plcUpdateCons = new PlcUpdateThread(UpdateJackQueue, 0);
plcUpdateCons->start();
ThreadSafeQueue<QByteArray> UpdateJackQueue;
PlcUpdateThread *plcUpdateProd = new PlcUpdateThread(UpdateJackQueue, 1);
plcUpdateProd->start();
PlcUpdateThread *plcUpdateCons = new PlcUpdateThread(UpdateJackQueue, 0);
plcUpdateCons->start();
To copy to clipboard, switch view to plain text mode
here is the Thread:
#ifndef PLCUPDATETHREAD_H
#define PLCUPDATETHREAD_H
#include <QByteArray>
#include <QThread>
#include "threadsafequeue.h"
#include "safilanLib.h"
#include "rs485.h"
#include <QDebug>
class PlcUpdateThread
: public QThread{
Q_OBJECT
public:
PlcUpdateThread(ThreadSafeQueue<QByteArray> &q, bool isProducer) : _queue(q), _prod(isProducer) {
if(_prod)
qDebug() << "I am a producer";
else
qDebug() << "I am a consumer";
}
void run() {
if(_prod)
while(1) produce();
else
while(1) consume();
}
void produce() {
const char str[]={UPDATE_xx};
_queue.enqueue(built);
// qDebug() << _queue.count();
msleep(100);
//sleep(qrand()%5);
}
void consume() {
qDebug() << "cosume";
qDebug() << v.toHex();
rs_plc.writeToSerialPort(v);
qDebug() << _queue.count();
msleep(300);
// sleep(qrand()%5);
}
public slots:
private:
ThreadSafeQueue<QByteArray> &_queue;
bool _prod;
};
#endif // PLCUPDATETHREAD_H
#ifndef PLCUPDATETHREAD_H
#define PLCUPDATETHREAD_H
#include <QByteArray>
#include <QThread>
#include "threadsafequeue.h"
#include "safilanLib.h"
#include "rs485.h"
#include <QDebug>
class PlcUpdateThread : public QThread
{
Q_OBJECT
public:
PlcUpdateThread(ThreadSafeQueue<QByteArray> &q, bool isProducer) : _queue(q), _prod(isProducer) {
if(_prod)
qDebug() << "I am a producer";
else
qDebug() << "I am a consumer";
}
void run() {
if(_prod)
while(1) produce();
else
while(1) consume();
}
void produce() {
const char str[]={UPDATE_xx};
QByteArray built((char*)str, 3) ;
_queue.enqueue(built);
// qDebug() << _queue.count();
msleep(100);
//sleep(qrand()%5);
}
void consume() {
qDebug() << "cosume";
QByteArray v = _queue.dequeue();
qDebug() << v.toHex();
rs_plc.writeToSerialPort(v);
qDebug() << _queue.count();
msleep(300);
// sleep(qrand()%5);
}
public slots:
private:
ThreadSafeQueue<QByteArray> &_queue;
bool _prod;
};
#endif // PLCUPDATETHREAD_H
To copy to clipboard, switch view to plain text mode
here is the write to serial function that the above thread calls:
and here is the RS instance created statically write after the RS class:
void rs_flushPort ();
bool rs_plcConfigPort();
bool rs_azmthConfigPort();
};
static RS rs_plc;
void rs_flushPort ();
bool rs_plcConfigPort();
bool rs_azmthConfigPort();
};
static RS rs_plc;
To copy to clipboard, switch view to plain text mode
Bookmarks