From 1f0df65a502a39c04533e4cbe085beacfec22a5c Mon Sep 17 00:00:00 2001 From: xin Date: Tue, 13 Oct 2020 17:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=9E=90=E6=9E=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vincecontrol.cpp | 76 +++++++++++++++++++++++++++++++++------ vincecontrol.vcxproj.user | 4 +-- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/vincecontrol.cpp b/vincecontrol.cpp index 42bbfbc..9613a83 100644 --- a/vincecontrol.cpp +++ b/vincecontrol.cpp @@ -28,6 +28,12 @@ VinceControl::~VinceControl() serial->close(); IsMotorInit = false; } + if (protools==NETTCP) + { + tcpServer->close(); + delete tcpServer; + + } } @@ -189,7 +195,7 @@ void VinceControl::SendCommandtoMotor(QString str, QString modor) str = "0 " + str; } tcpSocket[i]->write(str.toUtf8().data()); - tcpSocket[i]->waitForBytesWritten(1000); + tcpSocket[i]->waitForBytesWritten(50); return; } @@ -272,7 +278,7 @@ void VinceControl::MoveMotar(bool direction, QString motornetid ) } } - QThread::msleep(100); + //QThread::msleep(100); QString commendtosend = "mov\n"; SendCommandtoMotor(commendtosend, motornetid); @@ -290,9 +296,26 @@ void VinceControl::GetCommonRetrun(QByteArray &buf, QString id/*="non"*/) { if (protools==NETTCP) { + if (!IsMotorInit) + { + return; + } buf.clear(); - tcpSocket[0]->waitForReadyRead(10000); - buf = tcpSocket[0]->readAll(); + if (tcpSocket.size() == 0) + { + return; + } + + tcpSocket[0]->waitForReadyRead(4000); + if (tcpSocket.size() == 0) + { + return; + } + if (tcpSocket[0]->bytesAvailable()) + { + buf = tcpSocket[0]->readAll(); + } + } } } @@ -397,10 +420,15 @@ void VinceControl::initme() void VinceControl::initMotor(QString motornetid /*= "non"*/) { QByteArray buf; + //设置s1下降时停止旋转 QString commonstr = "cfg s1f=2\n"; SendCommandtoMotor(commonstr, motornetid); GetCommonRetrun(buf, motornetid); + //设置细分 + commonstr = "cfg mcs=7\n"; + SendCommandtoMotor(commonstr, motornetid); + GetCommonRetrun(buf, motornetid); commonstr = "cfg zmd=1\n";//归零尝试次数 SendCommandtoMotor(commonstr, motornetid); @@ -436,18 +464,22 @@ void VinceControl::initMotor(QString motornetid /*= "non"*/) commonstr = "cfg psv=0\n"; SendCommandtoMotor(commonstr, motornetid); GetCommonRetrun(buf, motornetid); - commonstr = "cfg acc=10000\n"; + //加速度设置为10000 + commonstr = "cfg acc=20000\n"; SendCommandtoMotor(commonstr, motornetid); GetCommonRetrun(buf, motornetid); - commonstr = "cfg dec=10000\n"; + //减速度设置为10000 + commonstr = "cfg dec=20000\n"; SendCommandtoMotor(commonstr, motornetid); GetCommonRetrun(buf, motornetid); + //电流设置为4 commonstr = "cfg crn=4\n"; SendCommandtoMotor(commonstr, motornetid); GetCommonRetrun(buf, motornetid); commonstr = "cfg cra=4\n"; SendCommandtoMotor(commonstr, motornetid); GetCommonRetrun(buf, motornetid); + } @@ -504,11 +536,28 @@ void VinceControl::StopMotormove(QString motornetid ) ByteBack VinceControl::GetState(QString motorid /*= "non"*/) { + ByteBack back; + if (!IsMotorInit) + { + back.Speed = -1000000; + } QString commonstr = "cts\n"; SendCommandtoMotor(commonstr, motorid); QByteArray buf; GetCommonRetrun(buf,motorid); - ByteBack back = TranslateBytedata(buf, motorid); + + if (buf.size()==0) + { + back.Speed = -1000000; + IsMotorInit = false; + emit SendLogToCallClass("motor " + QString::number(tcpServer->serverPort())+ " may be disconnect!!!"); + + } + else + { + back = TranslateBytedata(buf, motorid); + } + return back; } @@ -541,6 +590,7 @@ VinceControl::VinceControl(ProTools proto, int port) connect(tcpServer, &QTcpServer::newConnection, this, &VinceControl::onNewTcpClinetConnet); + } void VinceControl::onNewTcpClinetConnet() { @@ -552,7 +602,8 @@ void VinceControl::onNewTcpClinetConnet() tcpSocket.append(tcpServer->nextPendingConnection()); // connect(tcpSocket[tcpSocket.length() - 1], &QTcpSocket::readyRead, this, &VinceControl::onReciveFromClinet); - + connect(tcpSocket[tcpSocket.length() - 1], &QTcpSocket::disconnected, this, &VinceControl::onClinetDisConnet); + //connect(tcpSocket[tcpSocket.length() - 1], SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(onClinetDisConnet())); QString ip = tcpSocket[tcpSocket.length() - 1]->peerAddress().toString().split("::ffff:")[1]; Motorlist.append(ip); bool statofmotor = false; @@ -610,19 +661,24 @@ void VinceControl::onClinetDisConnet() int lenth = Motorlist.length(); for (size_t i = 0; i < lenth; i++) { + emit SendLogToCallClass(ip + "is disconnect"); if (Motorlist[i]==ip) { Motorlist.removeAt(i); isSettingSpeedlist.removeAt(i); Speedlist.removeAt(i); - delete tcpSocket[i]; + //delete tcpSocket[i]; tcpSocket.removeAt(i); + IsMotorInit = false; return; } - } + }//多传感器需要修改 + IsMotorInit = false; } + + void VinceControl::SendLog(QString str) { #ifdef LOGOUT diff --git a/vincecontrol.vcxproj.user b/vincecontrol.vcxproj.user index 895d7c2..ca343e4 100644 --- a/vincecontrol.vcxproj.user +++ b/vincecontrol.vcxproj.user @@ -5,11 +5,11 @@ C:\Qt\Qt5.8.0\5.8\msvc2013_64 - PATH=$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b$(PATH) + PATH=$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTDIR)\bin%3b$(QTDIR)\bin%3b"$(QTD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