diff --git a/vincecontrol.cpp b/vincecontrol.cpp index ea50e15..a39ae86 100644 --- a/vincecontrol.cpp +++ b/vincecontrol.cpp @@ -2,13 +2,15 @@ #include"QSerialPortInfo" #include "QDebug" #include "QThread" +union MyUnion +{ + int i; + unsigned char a[4]; +}; VinceControl::VinceControl(ProTools proto) { protools = proto; - IsMotorInit = false; - RS485ID = "0"; - Speednow = 0; - SpeedisSet = false; + initme(); } @@ -60,7 +62,25 @@ bool VinceControl::serialconnect(QString comname, QString bandrate) void VinceControl::Handshacke(QString motorid) { + SendCommandtoMotor("dev\n", motorid); + QByteArray buf; + + GetCommonRetrun(buf); + if (unsigned char(buf[0])==0xff) + { + if (motorid == "non") + { + motorid = Motorlist[0]; + } + buf.remove(0, 3); + buf.remove(buf.length() - 3, 4); + + QString str=motorid+":"; + str.append(buf); + SendLog(str); + } + } void VinceControl::SetRS485ID(QString id) @@ -80,6 +100,13 @@ void VinceControl::EnableMotro(QString id ) { SendCommandtoMotor(str, id); } + QByteArray buf; + GetCommonRetrun(buf,id); + + + ByteBack Camback = TranslateBytedata(buf,id); + + @@ -120,7 +147,7 @@ void VinceControl::SendCommandtoMotor(QString str) { tcpSocket[0]->write(str.toUtf8().data()); } - tcpSocket[0]->waitForBytesWritten(1000); + //tcpSocket[0]->waitForBytesWritten(1000); } } @@ -239,6 +266,19 @@ void VinceControl::SettingSpeed(unsigned long Speed, QString motornetid /*= "non //speed } +void VinceControl::GetCommonRetrun(QByteArray &buf, QString id/*="non"*/) +{ + if (id == "non") + { + if (protools==NETTCP) + { + buf.clear(); + tcpSocket[0]->waitForReadyRead(10000); + buf = tcpSocket[0]->readAll(); + } + } +} + void VinceControl::SettingSpeedByThis( long Speed, QString motornetid) { if (motornetid=="non") @@ -252,6 +292,8 @@ void VinceControl::SettingSpeedByThis( long Speed, QString motornetid) { Speednow = Speed; SpeedisSet = true; + + // TranslateBytedata(buf); } } @@ -266,12 +308,70 @@ void VinceControl::SettingSpeedByThis( long Speed, QString motornetid) isSettingSpeedlist[i] = true; Speedlist[i] = Speed; + } } } QString commentosend = "cfg spd=" + QString::number(Speed) + "\n"; SendCommandtoMotor(commentosend, motornetid); + QByteArray buf; + GetCommonRetrun(buf, motornetid); + TranslateBytedata(buf); + +} + +ByteBack VinceControl::TranslateBytedata(QByteArray buf, QString motornetid)// = "non") +{ + ByteBack backdatt; + backdatt.Speed = -1000000; + if (buf.length()!=21) + { + return backdatt; + } + if (unsigned char(buf[0]) == 0xff) + { + + buf.remove(0, 3); + buf.remove(buf.length() - 3, 3); + int location; + unsigned char a[15]; + memcpy(a, buf.data(), 15); + location = ((a[0] & 0x0f) << 28) | ((a[1] & 0x7f) << 21) | ((a[2] & 0x7f) << 14) | ((a[3] & 0x7f) << 7) | ((a[4] & 0x7f)); + float speed; + + memcpy(&speed, &location, 4); + backdatt.Speed = speed; + int loc; + loc = ((a[5] & 0x0f) << 28) | ((a[6] & 0x7f) << 21) | ((a[7] & 0x7f) << 14) | ((a[8] & 0x7f) << 7) | ((a[9] & 0x7f)); + backdatt.Location = loc; + backdatt.Speed = speed; + location = ((a[10] & 0x0f) << 28) | ((a[11] & 0x7f) << 21) | ((a[12] & 0x7f) << 14) | ((a[13] & 0x7f) << 7) | ((a[14] & 0x7f)); + backdatt.Stata = unsigned int(location); + + if (motornetid == "non") + { + motornetid = Motorlist[0]; + } + //memcpy(buf.data() + 6, &location, 4); + QString str1 = motornetid + ": Speed " + QString::number(backdatt.Speed, 'f', 2) + " location " + QString::number(backdatt.Location); + + SendLog(str1); + + return backdatt; + //memcpy(buf.data() + 6, &location, 4); + + } + return backdatt; +} + +void VinceControl::initme() +{ + + IsMotorInit = false; + RS485ID = "0"; + Speednow = 0; + SpeedisSet = false; } @@ -299,11 +399,26 @@ void VinceControl::StopMotormove(QString motornetid ) { QString commonstr = "stp\n"; SendCommandtoMotor(commonstr, motornetid); + QByteArray buf; + GetCommonRetrun(buf,motornetid); + TranslateBytedata(buf,motornetid); } +ByteBack VinceControl::GetState(QString motorid /*= "non"*/) +{ + QString commonstr = "cts\n"; + SendCommandtoMotor(commonstr, motorid); + QByteArray buf; + GetCommonRetrun(buf,motorid); + ByteBack back = TranslateBytedata(buf, motorid); + return back; +} + VinceControl::VinceControl(ProTools proto, int port) { + //VinceControl(proto); + initme(); if (proto != NETTCP) { return; @@ -329,6 +444,7 @@ void VinceControl::onNewTcpClinetConnet() QTcpSocket *tcpsocket1; tcpSocket.append(tcpServer->nextPendingConnection()); +// connect(tcpSocket[tcpSocket.length() - 1], &QTcpSocket::readyRead, this, &VinceControl::onReciveFromClinet); QString ip = tcpSocket[tcpSocket.length() - 1]->peerAddress().toString().split("::ffff:")[1]; Motorlist.append(ip); @@ -337,22 +453,42 @@ void VinceControl::onNewTcpClinetConnet() int speed = 200; Speedlist.append(speed); qint32 port = tcpSocket[tcpSocket.length() - 1]->peerPort(); - QString str11 = ip + " Connected The Port Number is "+port+"\n"; + QString str11 = ip + " Connected The Port Number is "+QString::number(port)+"\n"; SendLog(str11); + Handshacke(Motorlist[Motorlist.length()-1]); qDebug() << ip << ":" << port; + IsMotorInit = true; } -void VinceControl::onReciveFromClinet() +void VinceControl::onReciveFromClinet(QString motornetid)// = "non") { - QTcpSocket* sc = dynamic_cast(sender()); - QString ip = sc->peerAddress().toString().split("::ffff:")[1]; + //QTcpSocket* sc = dynamic_cast(sender()); + //QString ip = sc->peerAddress().toString().split("::ffff:")[1]; + //获取对方发送的内容 + if (motornetid=="non") + { + if (protools==NETTCP) + { + QByteArray buf; + tcpSocket[0]->waitForReadyRead(1000); + buf = tcpSocket[0]->readAll(); + if (buf.length()==0) + { + return; + } + QString str = Motorlist[0] + ":"; + str.append(buf); + SendLog(str); + + } + } #ifdef LOGOUT - QByteArray array = sc->readAll(); - QString strofrecive; - strofrecive.append(array); - QString strtosend = "sender " + ip + ":" + strofrecive; - SendLog(strtosend); +// QByteArray array = sc->readAll(); +// QString strofrecive; +// strofrecive.append(array); + //QString strtosend = "sender " + ip + ":" + strofrecive; +// SendLog(strtosend); #endif diff --git a/vincecontrol.h b/vincecontrol.h index a109657..55b83f0 100644 --- a/vincecontrol.h +++ b/vincecontrol.h @@ -57,6 +57,13 @@ enum ProTools RS232 = 0, RS485 = 1, NETTCP=2 +}; +struct ByteBack +{ + float Speed; + int Location; + unsigned int Stata; + }; /******************************************************************** * 鎻忚堪锛氶┈杈炬帶鍒剁▼搴 @@ -224,19 +231,19 @@ public: void SettingUpandDownSpeed(int addspeed, int downspeed); void StopMotormove(QString motornetid = "non"); - - + ByteBack GetState(QString motorid = "non"); QStringList Motorlist; + bool IsMotorInit; signals: void SendLogToCallClass(QString str); public slots : void onNewTcpClinetConnet(); - void onReciveFromClinet(); + void onClinetDisConnet(); private: QSerialPort *serial; + void onReciveFromClinet(QString motornetid = "non"); - bool IsMotorInit; ProTools protools; QString RS485ID; unsigned long speed; @@ -247,8 +254,11 @@ private: QList Speedlist; bool SpeedisSet; int Speednow; + void GetCommonRetrun(QByteArray &buf,QString id="non"); // void SettingSpeedByThis(long Speed, QString motornetid = "non"); + ByteBack TranslateBytedata(QByteArray buf, QString motornetid = "non"); + void initme(); }; diff --git a/vincecontrol.vcxproj.user b/vincecontrol.vcxproj.user index c657ef0..6fc2e63 100644 --- a/vincecontrol.vcxproj.user +++ b/vincecontrol.vcxproj.user @@ -3,10 +3,10 @@ C:\Qt\Qt5.8.0\5.8\msvc2013_64 - PATH=$(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"$(QTDIR)\bin%3b$(QTDIR)\bin%3b$(PATH) C:\Qt\Qt5.8.0\5.8\msvc2013_64 - PATH=$(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"$(QTDIR)\bin%3b$(QTDIR)\bin%3b$(PATH) \ No newline at end of file