//Base socket thread class
class SocketThread
: public QThread{
Q_OBJECT
protected:
quint32 m_port;
public:
~SocketThread();
void run();
public slots:
virtual void readSocket();
};
void SocketThread::run()
{
if(!m_socket)
{
connect(m_socket,SIGNAL(readyRead()),this,SLOT(readSocket()));
if(m_socket) m_socket->connectToHost(m_host,m_port);
}
exec();
}
//Actual working class
class HPSCSocket : public SocketThread
{
Q_OBJECT
public:
~HPSCSocket();
signals:
void sig_hpscReadout(int,float,float);
public slots:
void sendHPSCCommand(HPSC_Command cmd,long usWait);
void readSocket();
};
void HPSCSocket::sendHPSCCommand(HPSC_Command cmd,long usWait=0)
{
qDebug()<<"HPSCSocket::sendHPSCCommand";
if(m_dataStream)
{
qDebug()<<"sending command";
*m_dataStream<<cmd;
m_socket->flush();
qDebug()<<"about to sleep "<<usWait;
usleep(usWait);
qDebug()<<"woke up";
}
}
//the class (GUI) that communicates through the threaded socket class HPSCSocket
FrmPower
::FrmPower(BUS bus1,BUS bus2,
QWidget *parent
){
m_hpscSocket = new HPSCSocket(m_hpscHost.toString(),m_hpscPort,this);
connect(this,SIGNAL(sig_hpscCommand(HPSC_Command,long)),m_hpscSocket,SLOT(sendHPSCCommand(HPSC_Command,long)),Qt::QueuedConnection);
connect(m_hpscSocket,SIGNAL(sig_hpscReadout(int,float,float)),this,SLOT(hpscReadout(int,float,float)));
}
void FrmPower::bus1()
{
command.cmd = SETCURR1;
command.var = m_src1a;
emit sig_hpscCommand(command,120000);
}
//Base socket thread class
class SocketThread : public QThread
{
Q_OBJECT
protected:
QTcpSocket *m_socket;
QString m_host;
quint32 m_port;
QMutex m_socketMutex;
QDataStream *m_dataStream;
public:
SocketThread(QString host,quint32 port,QObject *parent = 0);
~SocketThread();
const QDataStream & stream();
void run();
public slots:
virtual void readSocket();
};
void SocketThread::run()
{
if(!m_socket)
{
m_socket = new QTcpSocket();
if(m_socket) m_dataStream = new QDataStream(m_socket);
connect(m_socket,SIGNAL(readyRead()),this,SLOT(readSocket()));
if(m_socket) m_socket->connectToHost(m_host,m_port);
}
exec();
}
//Actual working class
class HPSCSocket : public SocketThread
{
Q_OBJECT
public:
HPSCSocket(QString host,quint32 port,QObject *parent = 0);
~HPSCSocket();
signals:
void sig_hpscReadout(int,float,float);
public slots:
void sendHPSCCommand(HPSC_Command cmd,long usWait);
void readSocket();
};
void HPSCSocket::sendHPSCCommand(HPSC_Command cmd,long usWait=0)
{
qDebug()<<"HPSCSocket::sendHPSCCommand";
if(m_dataStream)
{
qDebug()<<"sending command";
*m_dataStream<<cmd;
m_socket->flush();
qDebug()<<"about to sleep "<<usWait;
usleep(usWait);
qDebug()<<"woke up";
}
}
//the class (GUI) that communicates through the threaded socket class HPSCSocket
FrmPower::FrmPower(BUS bus1,BUS bus2,QWidget *parent)
: QWidget(parent)
{
m_hpscSocket = new HPSCSocket(m_hpscHost.toString(),m_hpscPort,this);
connect(this,SIGNAL(sig_hpscCommand(HPSC_Command,long)),m_hpscSocket,SLOT(sendHPSCCommand(HPSC_Command,long)),Qt::QueuedConnection);
connect(m_hpscSocket,SIGNAL(sig_hpscReadout(int,float,float)),this,SLOT(hpscReadout(int,float,float)));
}
void FrmPower::bus1()
{
command.cmd = SETCURR1;
command.var = m_src1a;
emit sig_hpscCommand(command,120000);
}
To copy to clipboard, switch view to plain text mode
Bookmarks