|
|
@ -3,13 +3,25 @@
|
|
|
|
#include <WS2tcpip.h>
|
|
|
|
#include <WS2tcpip.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <qtcpsocket.h>
|
|
|
|
#include <qtcpsocket.h>
|
|
|
|
|
|
|
|
#include <PLCDevice.h>
|
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern PLCDevice* m_PLCTCPDevice;
|
|
|
|
|
|
|
|
extern PLCDevice* m_PLCDevice;
|
|
|
|
void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_) {
|
|
|
|
void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_) {
|
|
|
|
ip = QString::fromStdString(ip_);
|
|
|
|
ip = QString::fromStdString(ip_);
|
|
|
|
port = port_;
|
|
|
|
port = port_;
|
|
|
|
qDebug() << "tcp ip:" << ip << "| tcp port:" << port;
|
|
|
|
qDebug() << "tcp ip:" << ip << "| tcp port:" << port;
|
|
|
|
Local_TCP_Info_queue = p_TCP_Info_queue;
|
|
|
|
Local_TCP_Info_queue = p_TCP_Info_queue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __TCPServer
|
|
|
|
|
|
|
|
tcpServer = new QTcpServer(this);
|
|
|
|
|
|
|
|
tcpServer->listen(QHostAddress::Any, port);// Equivalent to QHostAddress("127.0.0.1").
|
|
|
|
|
|
|
|
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
|
|
|
|
|
|
|
|
connect(mySocket, SIGNAL(connected()), this, SLOT(onClientConnected()));
|
|
|
|
|
|
|
|
connect(mySocket, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void threadSendTCP::start_work()
|
|
|
|
void threadSendTCP::start_work()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -19,42 +31,195 @@ void threadSendTCP::start_work()
|
|
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::stop()
|
|
|
|
void threadSendTCP::stop()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef __TCPServer
|
|
|
|
|
|
|
|
if (tcpServer->isListening())
|
|
|
|
|
|
|
|
tcpServer->close();
|
|
|
|
|
|
|
|
if(tcpServer) delete tcpServer;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
|
|
|
|
mySocket->deleteLater();
|
|
|
|
|
|
|
|
if (mySocket) delete mySocket;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
isLoop = false;
|
|
|
|
isLoop = false;
|
|
|
|
// wait();
|
|
|
|
// wait();
|
|
|
|
delete mySocket;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
bool threadSendTCP::connectTCP() {
|
|
|
|
bool threadSendTCP::connectTCP() {
|
|
|
|
mySocket = new QTcpSocket();
|
|
|
|
mySocket = new QTcpSocket();
|
|
|
|
// 取消已有的连接
|
|
|
|
// 取消已有的连接
|
|
|
|
mySocket->abort();
|
|
|
|
mySocket->abort();
|
|
|
|
// 连接服务器
|
|
|
|
// 连接服务器
|
|
|
|
mySocket->connectToHost(ip, port);
|
|
|
|
mySocket->connectToHost(ip, port);
|
|
|
|
if (!mySocket->waitForConnected(30000)) {
|
|
|
|
if (!mySocket->waitForConnected(100)) {
|
|
|
|
qDebug() << "connect failed!";
|
|
|
|
qDebug() << "connect failed!";
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qDebug() << "connect successfully!";
|
|
|
|
qDebug() << "connect successfully!";
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::run()
|
|
|
|
void threadSendTCP::run()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
if (!connectTCP())
|
|
|
|
if (!connectTCP())
|
|
|
|
|
|
|
|
{
|
|
|
|
qDebug() << "TCP connect error!";
|
|
|
|
qDebug() << "TCP connect error!";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
while (isLoop) {
|
|
|
|
while (isLoop) {
|
|
|
|
_TCPSendInfo TCPSendInfo;
|
|
|
|
_TCPSendInfo TCPSendInfo;
|
|
|
|
Local_TCP_Info_queue->take(TCPSendInfo);
|
|
|
|
Local_TCP_Info_queue->take(TCPSendInfo);
|
|
|
|
num++;
|
|
|
|
num++;
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
|
|
|
#ifdef __TCPServer
|
|
|
|
|
|
|
|
if (ClientStatus == QAbstractSocket::ConnectedState)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __TCPClient
|
|
|
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
//mySocket->write("Hello! here is tcp client!\n");
|
|
|
|
//mySocket->write("Hello! here is tcp client!\n");
|
|
|
|
//mySocket->flush();
|
|
|
|
//mySocket->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
|
|
|
|
// 注意:std::exception通常不捕获由运行时错误引发的异常
|
|
|
|
|
|
|
|
// 但这里是为了演示catch块的结构
|
|
|
|
|
|
|
|
std::cerr << __FUNCTION__<<"捕获到异常: " << e.what() << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (...) {
|
|
|
|
|
|
|
|
// 使用省略号(...)可以捕获所有类型的异常,这是一种通用的捕获方式
|
|
|
|
|
|
|
|
std::cerr << "捕获到未知类型的异常" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
|
|
|
|
void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
|
|
|
|
std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString();
|
|
|
|
|
|
|
|
|
|
|
|
std::string fileName = TCPSendInfo->pics_name;
|
|
|
|
|
|
|
|
QDateTime ts_start = QDateTime::currentDateTime();
|
|
|
|
|
|
|
|
#if defined(__TCPServer) || defined(__TCPClient)
|
|
|
|
mySocket->write(fileName.c_str());
|
|
|
|
mySocket->write(fileName.c_str());
|
|
|
|
|
|
|
|
char temp = num % 10;
|
|
|
|
|
|
|
|
mySocket->write((char*)&temp, sizeof(char));
|
|
|
|
mySocket->write("\n");
|
|
|
|
mySocket->write("\n");
|
|
|
|
mySocket->flush();
|
|
|
|
mySocket->flush();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(__ModebusServer) || defined(__ModebusClient)
|
|
|
|
|
|
|
|
std::vector<int> asciiVals = stringToAscii(fileName);
|
|
|
|
|
|
|
|
//mySocket->write(fileName.c_str());
|
|
|
|
|
|
|
|
/*int temp = num % 2;
|
|
|
|
|
|
|
|
if (temp == 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_PLCDevice->write_bit_2_plc(10006, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_PLCDevice->write_bit_2_plc(10006, 0);
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//mySocket->write((char*)&temp,sizeof(char));
|
|
|
|
|
|
|
|
//mySocket->write("\n");
|
|
|
|
|
|
|
|
//mySocket->flush();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//遍历发送
|
|
|
|
|
|
|
|
int val_[32] = {};
|
|
|
|
|
|
|
|
uint8_t high[17] = {};
|
|
|
|
|
|
|
|
uint8_t low[17] = {};
|
|
|
|
|
|
|
|
uint16_t val[17] = {};
|
|
|
|
|
|
|
|
int h = 0;
|
|
|
|
|
|
|
|
int l = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < asciiVals.size(); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (i % 2 == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
high[h] = asciiVals[i];
|
|
|
|
|
|
|
|
h++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
low[l] = asciiVals[i];
|
|
|
|
|
|
|
|
l++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(val) / sizeof(short); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
val[i] = (static_cast<uint16_t>(high[i]) << 8) | low[i];//组合高低字节
|
|
|
|
|
|
|
|
m_PLCTCPDevice->mapping->tab_registers[i] = val[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*uint16_t da, dat, data, data_, data__;
|
|
|
|
|
|
|
|
da = m_PLCTCPDevice->mapping->tab_registers[13];
|
|
|
|
|
|
|
|
dat = m_PLCTCPDevice->mapping->tab_registers[14];
|
|
|
|
|
|
|
|
data = m_PLCTCPDevice->mapping->tab_registers[15];
|
|
|
|
|
|
|
|
data_ = m_PLCTCPDevice->mapping->tab_registers[16];
|
|
|
|
|
|
|
|
data__ = m_PLCTCPDevice->mapping->tab_registers[17];
|
|
|
|
|
|
|
|
uint8_t da8[2], dat8[2], data8[2], data8_[2], data8__[2];
|
|
|
|
|
|
|
|
Ui16ToUin8_P(da, da8);
|
|
|
|
|
|
|
|
Ui16ToUin8_P(dat, dat8);
|
|
|
|
|
|
|
|
Ui16ToUin8_P(data, data8);
|
|
|
|
|
|
|
|
Ui16ToUin8_P(data_, data8_);
|
|
|
|
|
|
|
|
Ui16ToUin8_P(data__, data8__);
|
|
|
|
|
|
|
|
string str[8];
|
|
|
|
|
|
|
|
str[0] = std::to_string(data8[0]);
|
|
|
|
|
|
|
|
str[1] = std::to_string(data8[1]);
|
|
|
|
|
|
|
|
str[2] = std::to_string(data8_[0]);
|
|
|
|
|
|
|
|
str[3] = std::to_string(data8__[0]);
|
|
|
|
|
|
|
|
str[4] = std::to_string(dat8[0]);
|
|
|
|
|
|
|
|
str[7] = std::to_string(da8[1]);
|
|
|
|
|
|
|
|
cout << "tcp_send_times:" << num << endl;
|
|
|
|
|
|
|
|
cout << "data:" << str[7] << str[4] << "." << str[0] << str[1] << str[2] << "_" << str[3] << endl;*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//字符串转ASCII
|
|
|
|
|
|
|
|
std::vector<int> threadSendTCP::stringToAscii(const std::string& str)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<int> asciiValues;
|
|
|
|
|
|
|
|
for (char c : str) {
|
|
|
|
|
|
|
|
asciiValues.push_back(static_cast<int>(c));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return asciiValues;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __TCPServer
|
|
|
|
|
|
|
|
void threadSendTCP::onNewConnection()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mySocket = tcpServer->nextPendingConnection(); //创建socket
|
|
|
|
|
|
|
|
qDebug() << "NewConnectionConnected";
|
|
|
|
|
|
|
|
ClientStatus = QAbstractSocket::ConnectedState;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::onClientConnected()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qDebug() << "ClientConnected";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void threadSendTCP::onClientDisconnected()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ClientStatus = QAbstractSocket::UnconnectedState;
|
|
|
|
|
|
|
|
qDebug() << "ClientDisconnected";
|
|
|
|
|
|
|
|
mySocket->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|