469 lines
12 KiB
C++
469 lines
12 KiB
C++
#include "Hd_SocketModule_PanasonicMC.h"
|
|
#include "qprocess.h"
|
|
|
|
|
|
static QQueue<QByteArray> ReadPack;
|
|
//static QVector<System::String^> Failed_address;
|
|
//static QVector<System::String^> Failed_address;
|
|
static QQueue<QByteArray> Failed_data;
|
|
|
|
QMutex* sendData_Mutex = new QMutex();
|
|
QMutex* recive_Mutex = new QMutex();
|
|
Hd_SocketModule_PanasonicMC* create()
|
|
{
|
|
qDebug() << "Hd_SocketModule_PanasonicMC create success!";
|
|
return new Hd_SocketModule_PanasonicMC;
|
|
}
|
|
|
|
void destory(Hd_SocketModule_PanasonicMC* ptr)
|
|
{
|
|
|
|
if (ptr)
|
|
{
|
|
delete ptr;
|
|
ptr = nullptr;
|
|
}
|
|
qDebug() << " Hd_SocketModule_PanasonicMC destory success!";
|
|
}
|
|
bool ifRun = true;
|
|
Hd_SocketModule_PanasonicMC::Hd_SocketModule_PanasonicMC()
|
|
{
|
|
ifRun = true;
|
|
*m_pPanasonicMcNet = gcnew HslCommunication::Profinet::Panasonic::PanasonicMcNet();
|
|
}
|
|
bool m_IsFinsTcp = false;
|
|
Hd_SocketModule_PanasonicMC::~Hd_SocketModule_PanasonicMC()
|
|
{
|
|
ifRun = false;
|
|
Sleep(100);
|
|
if (m_IsFinsTcp)
|
|
{
|
|
if ((*m_pPanasonicMcNet)->ConnectClose()->IsSuccess)
|
|
{
|
|
qDebug() << " (*m_pPanasonicMcNet)->ConnectClose() success!";
|
|
}else {
|
|
qCritical() << " (*m_pPanasonicMcNet)->ConnectClose() failed!";
|
|
}
|
|
m_IsFinsTcp = false;
|
|
}
|
|
qDebug() << " ~Hd_SocketModule_PanasonicMC success!";
|
|
}
|
|
|
|
// 从类中读数据到实例对象
|
|
bool Hd_SocketModule_PanasonicMC::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
|
{
|
|
Q_UNUSED(vec_mat);
|
|
QMutexLocker locker(sendData_Mutex);
|
|
|
|
QByteArray statusVaule;
|
|
/*if (tcp_Status)
|
|
{
|
|
statusVaule = "truee";
|
|
}
|
|
else
|
|
{
|
|
statusVaule = "false";
|
|
}*/
|
|
|
|
if (ReadPack.isEmpty())
|
|
{
|
|
data = statusVaule;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
data.append(statusVaule);
|
|
data.append(ReadPack.dequeue());
|
|
//qDebug() << " ReadPack len: " << ReadPack.length() << " data: " << data;
|
|
}
|
|
return true;
|
|
}
|
|
bool ifRetryCMD = false;
|
|
// 实例对象把数据写入到类
|
|
bool Hd_SocketModule_PanasonicMC::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
|
{
|
|
Q_UNUSED(vec_mat);
|
|
if(ifRetryCMD==false)
|
|
QMutexLocker locker(recive_Mutex);
|
|
Sleep(sleepvalue);
|
|
QString str = QString(data);
|
|
if ( data.isEmpty() )
|
|
{
|
|
qCritical() << " str is null";
|
|
return false;
|
|
}
|
|
|
|
QList<QByteArray> m_list = data.split('_');
|
|
if (m_list.size() < 4 && m_list[0] != "init")
|
|
{
|
|
qCritical() << data+" m_list.size() != 4;command err";
|
|
return false;
|
|
}
|
|
QString commond = m_list.at(0);
|
|
if ( commond == "init" ){
|
|
System::String^ Code = gcnew System::String("b5f8f9f7-c075-4913-ae6e-d7d2cc6f2de1");
|
|
if (HslCommunication::Authorization::SetAuthorizationCode(Code))
|
|
{
|
|
std::cout << "Register HSLCode Success" << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Register HSLCode Fail" << std::endl;
|
|
}
|
|
|
|
if (m_list.length() < 3)
|
|
{
|
|
qCritical() << " m_list failed! " << m_list;
|
|
return false;
|
|
}
|
|
|
|
m_IP = m_list.at(1);
|
|
m_port = m_list.at(2);
|
|
}
|
|
else if (commond == "read") {
|
|
//"read_byte_D3000_300",
|
|
while (ifRun)
|
|
{
|
|
if (sleepvalue < 2)
|
|
Sleep(2); // 读取速度过快,添加10ms延时
|
|
else
|
|
Sleep(sleepvalue);
|
|
|
|
int length = m_list[3].toInt();
|
|
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
|
|
HslCommunication::OperateResult<cli::array<unsigned char>^>^ result = (*m_pPanasonicMcNet)->Read(address, length);
|
|
if (!result->IsSuccess)
|
|
{
|
|
tcp_Status = false;
|
|
qCritical() << " (*FinsTcp)->ReadInt16 failed! check DA1: " << DA1;
|
|
|
|
QProcess pingProcess;
|
|
QString strArg = "ping " + m_IP + " -n 1 -i 2"; //strPingIP 为设备IP地址
|
|
pingProcess.start(strArg, QIODevice::ReadOnly);
|
|
pingProcess.waitForFinished(-1);
|
|
|
|
QString p_stdout = QString::fromLocal8Bit(pingProcess.readAllStandardOutput());
|
|
|
|
//qDebug() << p_stdout;
|
|
bool bPingSuccess = false;
|
|
|
|
if (p_stdout.contains("TTL=")) //通过特殊字符串进行判断ping是否成功
|
|
{
|
|
//printf("Ping true\n");
|
|
bPingSuccess = true;
|
|
//QMessageBox::information(this, "ping PLC:", "success!");
|
|
//qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ping PLC success! ";
|
|
}
|
|
else
|
|
{
|
|
/*printf("Ping false\n");
|
|
bPingSuccess = false;
|
|
QMessageBox::information(this, "ping PLC:", "failed!");*/
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ping PLC failed! ";
|
|
}
|
|
//
|
|
Failed_data.enqueue(data);
|
|
//if (m_IsFinsTcp)
|
|
{
|
|
//(*m_pPanasonicMcNet)->Dispose();
|
|
//qDebug() << " (*m_pPanasonicMcNet)->Dispose() success!";
|
|
|
|
if ((*m_pPanasonicMcNet)->ConnectClose()->IsSuccess)
|
|
{
|
|
qDebug() << " (*m_pPanasonicMcNet)->ConnectClose() success!";
|
|
}
|
|
else {
|
|
qCritical() << " (*m_pPanasonicMcNet)->ConnectClose() failed!";
|
|
}
|
|
//m_IsFinsTcp = false;
|
|
delete* m_pPanasonicMcNet;
|
|
Sleep(50);
|
|
*m_pPanasonicMcNet = gcnew HslCommunication::Profinet::Panasonic::PanasonicMcNet();
|
|
(*m_pPanasonicMcNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
|
|
(*m_pPanasonicMcNet)->Port = stoi(m_port.toStdString());
|
|
Sleep(10);
|
|
}
|
|
|
|
//if ((*m_pPanasonicMcNet)->ConnectClose()->IsSuccess)
|
|
//{
|
|
// qDebug() << " (*m_pPanasonicMcNet)->ConnectClose() success!";
|
|
//}
|
|
//else {
|
|
// qCritical() << " (*m_pPanasonicMcNet)->ConnectClose() failed!";
|
|
//}
|
|
int xunhuan_num = 20;
|
|
while (xunhuan_num) {
|
|
qDebug() << " TRY xunhuan_num: "<< xunhuan_num;
|
|
(*m_pPanasonicMcNet)->ConnectTimeOut = 1000; // 连接超时2秒
|
|
(*m_pPanasonicMcNet)->ReceiveTimeOut = 500; // 收发超时0.5秒
|
|
if ((*m_pPanasonicMcNet)->ConnectServer()->IsSuccess)
|
|
{
|
|
m_IsFinsTcp = true;
|
|
tcp_Status = true;
|
|
/*QVector<short> Writefailpack;
|
|
for (QVector<short>::iterator ii = WriteFailPack.begin(); ii != WriteFailPack.end(); ii++)
|
|
{
|
|
for (int val = 0; val < 5; val++)
|
|
{
|
|
if (!(*m_pPanasonicMcNet)->Write(address, ii)->IsSuccess)
|
|
{
|
|
if (val == 4) Writefailpack.push_back(*ii);
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
WriteFailPack.clear();
|
|
WriteFailPack = Writefailpack;
|
|
Writefailpack.clear();*/
|
|
qDebug() << "PanasonicMcNet Connect Success! " ;
|
|
|
|
ifRetryCMD = true;
|
|
for (int i = 0; i < Failed_data.size(); i++)
|
|
{
|
|
QByteArray try_data = Failed_data.dequeue();
|
|
std::vector<cv::Mat> my_vec_mat;
|
|
qDebug() << " try_data:: " << try_data;
|
|
writeData(my_vec_mat, try_data);
|
|
Sleep(10);
|
|
}
|
|
ifRetryCMD = false;
|
|
return tcp_Status;
|
|
}
|
|
else
|
|
{
|
|
xunhuan_num--;
|
|
tcp_Status = false;
|
|
qCritical() << "PanasonicMcNet Connect failed! ";
|
|
Sleep(100);
|
|
}
|
|
|
|
}
|
|
//return false;
|
|
continue;
|
|
}
|
|
|
|
unsigned char* valuePtr = new unsigned char[m_list[3].toInt() * 2];
|
|
QByteArray byte;
|
|
for (int i = 0; i < length * 2; i++)
|
|
{
|
|
valuePtr[i] = result->Content[i];
|
|
char v = result->Content[i];
|
|
byte.append(v);
|
|
}
|
|
|
|
ReadPack.append(byte);
|
|
if (valuePtr) {
|
|
delete[] valuePtr;
|
|
valuePtr = nullptr;
|
|
}
|
|
tcp_Status = true;
|
|
break;
|
|
}
|
|
|
|
}
|
|
else if (commond == "write")
|
|
{
|
|
//例"write_short_D1003_1","write_short_D1003_1,1,1,1",
|
|
if (m_list[1] == "short")
|
|
{
|
|
QString vecValue = m_list.at(3);
|
|
QStringList valueList = vecValue.split(",");
|
|
cli::array<short>^ write_list = gcnew cli::array<short>(valueList.length());
|
|
for (int i = 0; i < valueList.length(); i++)
|
|
{
|
|
write_list[i] = valueList[i].toShort();
|
|
}
|
|
|
|
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
|
|
for (auto val = 0; val < 5; val++)
|
|
{
|
|
//Sleep(sleepvalue);
|
|
if (!(*m_pPanasonicMcNet)->HslCommunication::Core::Net::NetworkDeviceBase::Write(address, write_list)->IsSuccess)
|
|
{
|
|
qCritical() << " val: " << val++ << " writeAll failed! m_list: " << m_list;
|
|
Failed_data.enqueue(data);
|
|
Sleep(sleepvalue);
|
|
}
|
|
else {
|
|
qDebug() << "[TCP] " << str << endl;
|
|
return true;
|
|
}
|
|
}
|
|
tcp_Status = false;
|
|
return false;
|
|
}
|
|
if (m_list[1] == "float")
|
|
{
|
|
QString vecValue = m_list.at(3);
|
|
QStringList valueList = vecValue.split(",");
|
|
cli::array<float>^ write_list = gcnew cli::array<float>(valueList.length());
|
|
for (int i = 0; i < valueList.length(); i++)
|
|
{
|
|
write_list[i] = valueList[i].toFloat();
|
|
}
|
|
|
|
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
|
|
for (auto val = 0; val < 5; val++)
|
|
{
|
|
//Sleep(sleepvalue);
|
|
if (!(*m_pPanasonicMcNet)->HslCommunication::Core::Net::NetworkDeviceBase::Write(address, write_list)->IsSuccess)
|
|
{
|
|
qCritical() << " val: " << val++ << " writeAll failed! m_list: " << m_list;
|
|
Failed_data.enqueue(data);
|
|
Sleep(sleepvalue);
|
|
}
|
|
else {
|
|
qDebug() << "[TCP] " << str << endl;
|
|
return true;
|
|
}
|
|
}
|
|
tcp_Status = false;
|
|
return false;
|
|
}
|
|
if (m_list[1] == "double")
|
|
{
|
|
QString vecValue = m_list.at(3);
|
|
QStringList valueList = vecValue.split(",");
|
|
cli::array<double>^ write_list = gcnew cli::array<double>(valueList.length());
|
|
for (int i = 0; i < valueList.length(); i++)
|
|
{
|
|
write_list[i] = valueList[i].toDouble();
|
|
}
|
|
|
|
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
|
|
for (auto val = 0; val < 5; val++)
|
|
{
|
|
//Sleep(sleepvalue);
|
|
if (!(*m_pPanasonicMcNet)->HslCommunication::Core::Net::NetworkDeviceBase::Write(address, write_list)->IsSuccess)
|
|
{
|
|
qCritical() << " val: " << val++ << " writeAll failed! m_list: " << m_list;
|
|
Failed_data.enqueue(data);
|
|
Sleep(sleepvalue);
|
|
}
|
|
else {
|
|
qDebug() << "[TCP] " << str << endl;
|
|
return true;
|
|
}
|
|
}
|
|
tcp_Status = false;
|
|
return false;
|
|
}
|
|
if (m_list[1] == "string")
|
|
{
|
|
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
|
|
System::String^ write_Value = gcnew System::String(m_list.at(3).toStdString().c_str());
|
|
for (auto val = 0; val < 5; val++)
|
|
{
|
|
//Sleep(sleepvalue);
|
|
if (!(*m_pPanasonicMcNet)->Write(address, write_Value)->IsSuccess) {
|
|
qCritical() << " val: " << val++ << " writeString failed! m_list: " << m_list;
|
|
Failed_data.enqueue(data);
|
|
Sleep(sleepvalue);
|
|
}
|
|
else {
|
|
qDebug() << "[TCP] " << str << endl;
|
|
return true;
|
|
}
|
|
}
|
|
tcp_Status = false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Hd_SocketModule_PanasonicMC::initParas(QByteArray& data)
|
|
{
|
|
|
|
if (data.isEmpty())
|
|
{
|
|
qWarning()<< "[TCP] " << " data is null!";
|
|
}
|
|
else
|
|
{
|
|
QString initDat = QString::fromUtf8(data);
|
|
QStringList list = initDat.split("_");
|
|
if (list.size() < 3)
|
|
{
|
|
qCritical() << "[TCP] " << " list size error :" << list.size();
|
|
return false;
|
|
}
|
|
|
|
QString sleepname = list.at(0);
|
|
sleepvalue = list.at(1).toInt();
|
|
if (sleepvalue == 0)sleepvalue = 1;
|
|
/*if (sleepvalue <= 5)
|
|
sleepvalue = 30;*/
|
|
QString readname = list.at(2);
|
|
QString readvalue = list.at(3);
|
|
QString write_type = list.at(4);
|
|
QString write_value = list.at(5);
|
|
}
|
|
|
|
// 连接通讯
|
|
*m_pPanasonicMcNet = gcnew HslCommunication::Profinet::Panasonic::PanasonicMcNet();
|
|
(*m_pPanasonicMcNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
|
|
(*m_pPanasonicMcNet)->Port = stoi(m_port.toStdString());
|
|
int xunhuan_num = 20;
|
|
while (xunhuan_num) {
|
|
(*m_pPanasonicMcNet)->ConnectTimeOut = 500; // 连接超时0.5秒
|
|
(*m_pPanasonicMcNet)->ReceiveTimeOut = 500; // 收发超时0.5秒
|
|
if ((*m_pPanasonicMcNet)->ConnectServer()->IsSuccess)
|
|
{
|
|
m_IsFinsTcp = true;
|
|
tcp_Status = true;
|
|
qDebug() << "PanasonicMcNet Connect Success! " ;
|
|
return tcp_Status;
|
|
}
|
|
else
|
|
{
|
|
xunhuan_num--;
|
|
tcp_Status = false;
|
|
qCritical() <<"PanasonicMcNet Connect failed! ";
|
|
|
|
}
|
|
Sleep(100);
|
|
}
|
|
return tcp_Status;
|
|
|
|
}
|
|
|
|
bool Hd_SocketModule_PanasonicMC::run()
|
|
{
|
|
// 没有用到
|
|
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Hd_SocketModule_PanasonicMC::decodeWriteResult(QByteArray byte, QVector<QString>& vec_ret)
|
|
{
|
|
try
|
|
{
|
|
if (byte.isEmpty())
|
|
{
|
|
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " byte is null!";
|
|
return false;
|
|
}
|
|
while (!byte.isEmpty())
|
|
{
|
|
int len = 0;
|
|
QByteArray m_len = byte.mid(0, 4);
|
|
memcpy((void*)&len, (void*)m_len.data(), 4);
|
|
byte.remove(0, 4);
|
|
|
|
QByteArray mm_data = byte.mid(0, len);
|
|
vec_ret.append(QString::fromUtf8(mm_data));
|
|
byte.remove(0, len);
|
|
}
|
|
}
|
|
catch (QString ev) {
|
|
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
|
return false;
|
|
}
|
|
return true;
|
|
} |