init files
This commit is contained in:
@@ -0,0 +1,476 @@
|
||||
#include "Hd_SocketModule_ModbusTcp.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_ModbusTcp* create()
|
||||
{
|
||||
qDebug() << "Hd_SocketModule_ModbusTcp create success!";
|
||||
return new Hd_SocketModule_ModbusTcp;
|
||||
}
|
||||
|
||||
void destory(Hd_SocketModule_ModbusTcp* ptr)
|
||||
{
|
||||
|
||||
if (ptr)
|
||||
{
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
}
|
||||
qDebug() << " Hd_SocketModule_ModbusTcp destory success!";
|
||||
}
|
||||
bool ifRun = true;
|
||||
Hd_SocketModule_ModbusTcp::Hd_SocketModule_ModbusTcp()
|
||||
{
|
||||
ifRun = true;
|
||||
*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet();
|
||||
}
|
||||
bool m_IsFinsTcp = false;
|
||||
Hd_SocketModule_ModbusTcp::~Hd_SocketModule_ModbusTcp()
|
||||
{
|
||||
ifRun = false;
|
||||
Sleep(100);
|
||||
if (m_IsFinsTcp)
|
||||
{
|
||||
if ((*m_pModBusNet)->ConnectClose()->IsSuccess)
|
||||
{
|
||||
qDebug() << " (*m_pModBusNet)->ConnectClose() success!";
|
||||
}
|
||||
else {
|
||||
qCritical() << " (*m_pModBusNet)->ConnectClose() failed!";
|
||||
}
|
||||
m_IsFinsTcp = false;
|
||||
}
|
||||
qDebug() << " ~Hd_SocketModule_ModbusTcp success!";
|
||||
}
|
||||
|
||||
// 从类中读数据到实例对象
|
||||
bool Hd_SocketModule_ModbusTcp::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_ModbusTcp::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;
|
||||
}
|
||||
if (m_list[2].contains("M") || m_list[2].contains("D"))
|
||||
m_list[2] = m_list[2].mid(1, m_list[2].size() - 1);
|
||||
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_pModBusNet)->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_pModBusNet)->Dispose();
|
||||
//qDebug() << " (*m_pModBusNet)->Dispose() success!";
|
||||
|
||||
if ((*m_pModBusNet)->ConnectClose()->IsSuccess)
|
||||
{
|
||||
qDebug() << " (*m_pModBusNet)->ConnectClose() success!";
|
||||
}
|
||||
else {
|
||||
qCritical() << " (*m_pModBusNet)->ConnectClose() failed!";
|
||||
}
|
||||
//m_IsFinsTcp = false;
|
||||
delete* m_pModBusNet;
|
||||
Sleep(50);
|
||||
*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet();
|
||||
(*m_pModBusNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
|
||||
(*m_pModBusNet)->Port = stoi(m_port.toStdString());
|
||||
Sleep(10);
|
||||
}
|
||||
|
||||
//if ((*m_pModBusNet)->ConnectClose()->IsSuccess)
|
||||
//{
|
||||
// qDebug() << " (*m_pModBusNet)->ConnectClose() success!";
|
||||
//}
|
||||
//else {
|
||||
// qCritical() << " (*m_pModBusNet)->ConnectClose() failed!";
|
||||
//}
|
||||
int xunhuan_num = 20;
|
||||
while (xunhuan_num) {
|
||||
qDebug() << " TRY xunhuan_num: " << xunhuan_num;
|
||||
(*m_pModBusNet)->ConnectTimeOut = 1000; // 连接超时2秒
|
||||
(*m_pModBusNet)->ReceiveTimeOut = 500; // 收发超时0.5秒
|
||||
if ((*m_pModBusNet)->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_pModBusNet)->Write(address, ii)->IsSuccess)
|
||||
{
|
||||
if (val == 4) Writefailpack.push_back(*ii);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
WriteFailPack.clear();
|
||||
WriteFailPack = Writefailpack;
|
||||
Writefailpack.clear();*/
|
||||
qDebug() << "ModBusNet 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() << "ModBusNet 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_pModBusNet)->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_pModBusNet)->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_pModBusNet)->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_pModBusNet)->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_ModbusTcp::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_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet();
|
||||
//(*m_pModBusNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
|
||||
//(*m_pModBusNet)->Port = stoi(m_port.toStdString());
|
||||
byte s = '1';
|
||||
System::String^ ss= gcnew System::String(m_IP.toStdString().c_str());
|
||||
*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet(ss, m_port.toInt(), s);
|
||||
int xunhuan_num = 20;
|
||||
while (xunhuan_num) {
|
||||
//(*m_pModBusNet)->ConnectTimeOut = 1000; // 连接超时0.5秒
|
||||
//(*m_pModBusNet)->ReceiveTimeOut = 1000; // 收发超时0.5秒
|
||||
if ((*m_pModBusNet)->ConnectServer()->IsSuccess)
|
||||
{
|
||||
m_IsFinsTcp = true;
|
||||
tcp_Status = true;
|
||||
qDebug() << "ModBusNet Connect Success! ";
|
||||
return tcp_Status;
|
||||
}
|
||||
else
|
||||
{
|
||||
xunhuan_num--;
|
||||
tcp_Status = false;
|
||||
qCritical() << "ModBusNet Connect failed! ";
|
||||
|
||||
}
|
||||
Sleep(100);
|
||||
}
|
||||
return tcp_Status;
|
||||
|
||||
}
|
||||
|
||||
bool Hd_SocketModule_ModbusTcp::run()
|
||||
{
|
||||
// 没有用到
|
||||
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Hd_SocketModule_ModbusTcp::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;
|
||||
}
|
||||
Reference in New Issue
Block a user