#include "Hd_SocketModule_SiemensTcp.h" #include "qprocess.h" static QQueue ReadPack; //static QVector Failed_address; //static QVector Failed_address; static QQueue Failed_data; QMutex* sendData_Mutex = new QMutex(); QMutex* recive_Mutex = new QMutex(); Hd_SocketModule_SiemensTcp* create() { qDebug() << "Hd_SocketModule_SiemensTcp create success!"; return new Hd_SocketModule_SiemensTcp; } void destory(Hd_SocketModule_SiemensTcp* ptr) { if (ptr) { delete ptr; ptr = nullptr; } qDebug() << " Hd_SocketModule_SiemensTcp destory success!"; } bool ifRun = true; Hd_SocketModule_SiemensTcp::Hd_SocketModule_SiemensTcp() { ifRun = true; //m_pSnap7Tcp = new gcroot; *m_pSnap7Tcp = gcnew HslCommunication::Profinet::Siemens::SiemensS7Net(HslCommunication::Profinet::Siemens::SiemensPLCS::S1200); } bool m_IsFinsTcp = false; Hd_SocketModule_SiemensTcp::~Hd_SocketModule_SiemensTcp() { ifRun = false; Sleep(100); if (m_IsFinsTcp) { if ((*m_pSnap7Tcp)->ConnectClose()->IsSuccess) { qDebug() << " (*m_pSnap7Tcp)->ConnectClose() success!"; }else { qCritical() << " (*m_pSnap7Tcp)->ConnectClose() failed!"; } m_IsFinsTcp = false; } qDebug() << " ~Hd_SocketModule_SiemensTcp success!"; } // 从类中读数据到实例对象 bool Hd_SocketModule_SiemensTcp::readData(std::vector& 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_SiemensTcp::writeData(std::vector& 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 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()); short read = (*m_pSnap7Tcp)->ReadInt16("M100")->Content; ////cli::array^ write_list = gcnew cli::array(length); //OperateResult read = (*m_pSnap7Tcp)->ReadInt16(address, length)->Content; //if (read.IsSuccess) //{ // unsigned char* valuePtr = new unsigned char[m_list[3].toInt() * 2]; // QByteArray byte; // for (int i = 0; i < length * 2; i++) // { // valuePtr[i] = read->Content[i]; // char v = read->Content[i]; // byte.append(v); // } // ReadPack.append(byte); // if (valuePtr) { // delete[] valuePtr; // valuePtr = nullptr; // } // tcp_Status = true; // break; //} //else // std::cout << "m_pSnap7Tcp::read failed !" << std::endl; } } else if ( commond == "write" ) { QString m_value = m_list.at(3); //例"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^ write_list = gcnew cli::array(valueList.length()); /*short write_list[valueList.length()]={0}; 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);OperateResult write = siemens.Write( "M100", (short)100 ); //OperateResult write = (*m_pSnap7Tcp)->Write(address, (short)100); //if (!write.IsSuccess)//(!(*m_pSnap7Tcp)->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); // OperateResult write = (*m_pSnap7Tcp)->Write(address, write_Value); // if (!write.IsSuccess)//(!(*m_pSnap7Tcp)->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_SiemensTcp::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_pSnap7Tcp = new gcroot; (*m_pSnap7Tcp)->IpAddress = gcnew System::String(m_IP.toStdString().c_str()); *m_pSnap7Tcp = gcnew HslCommunication::Profinet::Siemens::SiemensS7Net(HslCommunication::Profinet::Siemens::SiemensPLCS::S1200, (*m_pSnap7Tcp)->IpAddress); //(*m_pSnap7Tcp)->Port = stoi(m_port.toStdString()); short read = (*m_pSnap7Tcp)->ReadInt16("M100")->Content; /*OperateResult read=(*m_pSnap7Tcp)->ReadInt16("M100"); if (read.IsSuccess) std::cout << "m_pSnap7Tcp::read Success." << std::endl; else std::cout << "m_pSnap7Tcp::read failed !" << std::endl;*/ //int xunhuan_num = 20; //while (xunhuan_num) { // (*m_pSnap7Tcp)->ConnectTimeOut = 500; // 连接超时0.5秒 // (*m_pSnap7Tcp)->ReceiveTimeOut = 500; // 收发超时0.5秒 // if ((*m_pSnap7Tcp)->ConnectServer()->IsSuccess) // { // m_IsFinsTcp = true; // tcp_Status = true; // qDebug() << "Snap7Tcp Connect Success! " ; // return tcp_Status; // } // else // { // xunhuan_num--; // tcp_Status = false; // qCritical() <<"Snap7Tcp Connect failed! "; // // } // Sleep(100); //} return tcp_Status; } bool Hd_SocketModule_SiemensTcp::run() { // 没有用到 //qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!"; return true; } bool Hd_SocketModule_SiemensTcp::decodeWriteResult(QByteArray byte, QVector& 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; }