You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
3.7 KiB
C++
174 lines
3.7 KiB
C++
#include "QPLCDevice.h"
|
|
|
|
QPLCDevice::QPLCDevice(QObject *parent)
|
|
: QObject(parent)
|
|
{
|
|
TaskFunc_Bool func2 = std::bind(&QPLCDevice::onRunTask, this);
|
|
m_pTask = new lpThread(func2);
|
|
m_pTask->setPriority(QThread::HighestPriority);
|
|
m_pTask->StartThread();
|
|
}
|
|
|
|
QPLCDevice::~QPLCDevice()
|
|
{
|
|
if (m_pTask)
|
|
{
|
|
m_pTask->EndThread();
|
|
delete m_pTask;
|
|
m_pTask = nullptr;
|
|
}
|
|
}
|
|
|
|
void QPLCDevice::onInitDevice()
|
|
{
|
|
if (!m_rawTcpServer) {
|
|
m_rawTcpServer = new lptkRawTcpServer();
|
|
connect(m_rawTcpServer, &lptkRawTcpServer::client_status_changed, this, &QPLCDevice::on_serv_status_changed);
|
|
connect(m_rawTcpServer, &lptkRawTcpServer::client_connect_status, this, &QPLCDevice::on_client_connect_status);
|
|
RecvDataFunc func = std::bind(&QPLCDevice::onRecvFunc, this, std::placeholders::_1);
|
|
m_rawTcpServer->setRecvCallFunc(func);
|
|
}
|
|
}
|
|
|
|
void QPLCDevice::onStartServer(int port)
|
|
{
|
|
if (m_rawTcpServer) {
|
|
m_rawTcpServer->server_begin("*", port);
|
|
}
|
|
}
|
|
|
|
void QPLCDevice::onStopServer()
|
|
{
|
|
if (m_rawTcpServer) {
|
|
m_rawTcpServer->server_end();
|
|
}
|
|
}
|
|
|
|
void QPLCDevice::onRecvFunc(QByteArray data)
|
|
{
|
|
mMtLock.lock();
|
|
m_recvData.append(data);
|
|
mMtLock.unlock();
|
|
emit sgRecvDataCallBack(data);
|
|
}
|
|
|
|
void QPLCDevice::sendByteData(QByteArray byteData)
|
|
{
|
|
QSharedPointer<QByteArray> sendData = QSharedPointer<QByteArray>(new QByteArray);
|
|
|
|
sendData->append(byteData);
|
|
if (m_rawTcpServer)
|
|
{
|
|
m_rawTcpServer->broadcast_send_data(sendData);
|
|
}
|
|
emit sgSendDataCallBack(byteData);
|
|
}
|
|
|
|
Q_SLOT void QPLCDevice::on_serv_status_changed(int id, ServerStatus status)
|
|
{
|
|
switch (status) {
|
|
case TCPSERVER_ON_CLIENT_CONNECTED:
|
|
//connected_sess_list_.insert(id);
|
|
break;
|
|
case TCPSERVER_ON_CLIENT_DISCONNECTED:
|
|
//connected_sess_list_.remove(id);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool QPLCDevice::onRunTask()
|
|
{
|
|
mMtLock.lock();//Êý¾Ý½âÎö
|
|
if (m_recvData.size() > 0)
|
|
{
|
|
if (m_recvData.startsWith("CA"))
|
|
{
|
|
int index = m_recvData.indexOf("#");
|
|
QByteArray b = m_recvData.left(index + 1);
|
|
// Êý¾Ý°ü
|
|
onParaster(b);
|
|
m_recvData.remove(0, index + 1);
|
|
}
|
|
else {
|
|
m_recvData.remove(0, 1);
|
|
}
|
|
}
|
|
mMtLock.unlock();
|
|
return true;
|
|
}
|
|
|
|
void QPLCDevice::onParaster(QByteArray recvData)
|
|
{
|
|
QString strPack = QString(recvData);
|
|
QStringList lst = strPack.split(";");
|
|
if (lst.size() >= 4) {
|
|
QString header = lst[0];
|
|
QString cmd = lst[1];
|
|
QString data = lst[2];
|
|
QString trailer = lst[3];
|
|
|
|
if (cmd.toInt() == 1)//Ïà»ú´¥·¢
|
|
{
|
|
int stationID = data.toInt();
|
|
emit sgTrigerCam(stationID);
|
|
}
|
|
else if (cmd.toInt() == 2) //ÐÄÌøÓ¦´ð
|
|
{
|
|
onSendHeartBit();
|
|
}
|
|
else if (cmd.toInt() == 3)
|
|
{
|
|
int stationID = data.toInt();
|
|
emit sgSendAlgResult(stationID);
|
|
}
|
|
}
|
|
}
|
|
|
|
void QPLCDevice::onSendValueRlt(const ValueResult& rlt)
|
|
{
|
|
QString sdata = QString("CA;3;%1;%2;%3;%4;%5;#").arg(rlt.stationID).arg(rlt.strModel).arg(rlt.angle).arg(rlt.center.x()).arg(rlt.center.y());
|
|
sendByteData(sdata.toLatin1());
|
|
}
|
|
|
|
void QPLCDevice::onSendHeartBit()
|
|
{
|
|
QString sdata = QString("CA;2;1;#");
|
|
sendByteData(sdata.toLatin1());
|
|
}
|
|
|
|
void QPLCDevice::onAckTrigerCam(int stationID)
|
|
{
|
|
QString sdata = QString("CA;1;%1;#").arg(stationID);
|
|
sendByteData(sdata.toLatin1());
|
|
}
|
|
|
|
Q_SLOT void QPLCDevice::on_client_connect_status(QString strID, ServerStatus status)
|
|
{
|
|
QString clientName = strID;
|
|
switch (status) {
|
|
case TCPSERVER_ON_CLIENT_CONNECTED:
|
|
if (!m_clientList.contains(clientName))
|
|
{
|
|
m_clientList.append(clientName);
|
|
}
|
|
break;
|
|
case TCPSERVER_ON_CLIENT_DISCONNECTED:
|
|
if (m_clientList.contains(clientName))
|
|
m_clientList.removeAll(clientName);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
bool QPLCDevice::getClinetInfo(QString &client)
|
|
{
|
|
if (m_clientList.size() > 0)
|
|
{
|
|
client = QString("%1[%2]").arg(m_clientList.last()).arg(m_clientList.size());
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|