|
|
|
@ -35,34 +35,36 @@ void threadSendTCP::stop()
|
|
|
|
|
if (tcpServer->isListening())
|
|
|
|
|
tcpServer->close();
|
|
|
|
|
if(tcpServer) delete tcpServer;
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
|
mySocket->deleteLater();
|
|
|
|
|
if (mySocket) delete mySocket;
|
|
|
|
|
#endif
|
|
|
|
|
isLoop = false;
|
|
|
|
|
// wait();
|
|
|
|
|
mySocket->deleteLater();
|
|
|
|
|
if (mySocket) delete mySocket;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef __TCPServer
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
|
bool threadSendTCP::connectTCP() {
|
|
|
|
|
//mySocket = new QTcpSocket();
|
|
|
|
|
////mySocket = new QTcpServer();
|
|
|
|
|
//// 取消已有的连接
|
|
|
|
|
//mySocket->abort();
|
|
|
|
|
//// 连接服务器
|
|
|
|
|
//mySocket->connectToHost(ip, port);
|
|
|
|
|
//if (!mySocket->waitForConnected(100)) {
|
|
|
|
|
// qDebug() << "connect failed!";
|
|
|
|
|
// return false;
|
|
|
|
|
//}
|
|
|
|
|
//qDebug() << "connect successfully!";
|
|
|
|
|
//
|
|
|
|
|
mySocket = new QTcpSocket();
|
|
|
|
|
// 取消已有的连接
|
|
|
|
|
mySocket->abort();
|
|
|
|
|
// 连接服务器
|
|
|
|
|
mySocket->connectToHost(ip, port);
|
|
|
|
|
if (!mySocket->waitForConnected(100)) {
|
|
|
|
|
qDebug() << "connect failed!";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "connect successfully!";
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::run()
|
|
|
|
|
{
|
|
|
|
|
#ifndef __TCPServer
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
|
if (!connectTCP())
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "TCP connect error!";
|
|
|
|
@ -73,49 +75,56 @@ void threadSendTCP::run()
|
|
|
|
|
Local_TCP_Info_queue->take(TCPSendInfo);
|
|
|
|
|
num++;
|
|
|
|
|
#ifdef __TCPServer
|
|
|
|
|
if (ClientStatus == QAbstractSocket::ConnectedState)
|
|
|
|
|
{
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
if (ClientStatus == QAbstractSocket::ConnectedState)
|
|
|
|
|
{
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
#endif
|
|
|
|
|
//mySocket->write("Hello! here is tcp client!\n");
|
|
|
|
|
//mySocket->flush();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
|
|
|
|
|
std::string fileName = TCPSendInfo->pics_name;
|
|
|
|
|
QDateTime ts_start = QDateTime::currentDateTime();
|
|
|
|
|
#if defined(__TCPServer) || defined(__TCPClient)
|
|
|
|
|
mySocket->write(fileName.c_str());
|
|
|
|
|
char temp = num % 10;
|
|
|
|
|
mySocket->write((char*)&temp,sizeof(char));
|
|
|
|
|
mySocket->write("\n");
|
|
|
|
|
mySocket->flush();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(__ModebusServer) || defined(__ModebusClient)
|
|
|
|
|
std::vector<int> asciiVals = stringToAscii(fileName);
|
|
|
|
|
//mySocket->write(fileName.c_str());
|
|
|
|
|
//char temp = num % 10;
|
|
|
|
|
//mySocket->write((char*)&temp,sizeof(char));
|
|
|
|
|
//mySocket->write("\n");
|
|
|
|
|
//mySocket->flush();
|
|
|
|
|
//
|
|
|
|
|
//遍历发送
|
|
|
|
|
int val_[35] = {};
|
|
|
|
|
QDateTime now_ts = QDateTime::currentDateTime();
|
|
|
|
|
qDebug() << "now_ts=" << now_ts;
|
|
|
|
|
for (int i = 0; i < asciiVals.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
val_[i] = asciiVals[i];
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef __ModebusServer
|
|
|
|
|
//m_PLCTCPDevice->write_2_plc(40000, 70, (uint16_t*)&val_);//小盒
|
|
|
|
|
m_PLCTCPDevice->write_2_plc(40070, 70, (uint16_t*)&val_);//条盒
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef __ModebusClient
|
|
|
|
|
memcpy(&(m_PLCTCPDevice->mapping->tab_registers[0]),(uint16_t*)&val_,70);
|
|
|
|
|
for(int i = 0;i<sizeof(val_)/sizeof(int);i++)
|
|
|
|
|
{
|
|
|
|
|
m_PLCTCPDevice->mapping->tab_registers[i] = val_[i];
|
|
|
|
|
m_PLCTCPDevice->mapping->tab_input_registers[i] = val_[i];
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
now_ts = QDateTime::currentDateTime();
|
|
|
|
|
qDebug() << "end-now_ts=" << now_ts;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QDateTime ts_end = QDateTime::currentDateTime();
|
|
|
|
|
int time_process = ts_start.msecsTo(ts_end);
|
|
|
|
|
qDebug() << "end-now_ts=" << time_process;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//字符串转ASCII
|
|
|
|
|
std::vector<int> threadSendTCP::stringToAscii(const std::string& str)
|
|
|
|
|
{
|
|
|
|
@ -132,12 +141,10 @@ void threadSendTCP::onNewConnection()
|
|
|
|
|
mySocket = tcpServer->nextPendingConnection(); //创建socket
|
|
|
|
|
qDebug() << "NewConnectionConnected";
|
|
|
|
|
ClientStatus = QAbstractSocket::ConnectedState;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::onClientConnected()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
qDebug() << "ClientConnected";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|