2020-08-05 17:04:25 +08:00
|
|
|
|
#include "vincecontrol.h"
|
2020-08-05 18:15:32 +08:00
|
|
|
|
#include"QSerialPortInfo"
|
2020-08-23 17:06:25 +08:00
|
|
|
|
#include "QDebug"
|
|
|
|
|
#include "QThread"
|
2020-08-05 18:15:32 +08:00
|
|
|
|
VinceControl::VinceControl(ProTools proto)
|
|
|
|
|
{
|
|
|
|
|
protools = proto;
|
|
|
|
|
IsMotorInit = false;
|
|
|
|
|
RS485ID = "0";
|
2020-08-23 17:06:25 +08:00
|
|
|
|
Speednow = 0;
|
|
|
|
|
SpeedisSet = false;
|
2020-08-05 18:15:32 +08:00
|
|
|
|
}
|
2020-08-05 17:04:25 +08:00
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
|
|
|
|
|
|
2020-08-05 18:15:32 +08:00
|
|
|
|
VinceControl::~VinceControl()
|
2020-08-05 17:04:25 +08:00
|
|
|
|
{
|
2020-08-23 17:06:25 +08:00
|
|
|
|
if (protools == RS232 || protools == RS485)
|
|
|
|
|
{
|
|
|
|
|
serial->close();
|
|
|
|
|
IsMotorInit = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 18:15:32 +08:00
|
|
|
|
}
|
2020-08-05 17:04:25 +08:00
|
|
|
|
|
2020-08-05 18:15:32 +08:00
|
|
|
|
bool VinceControl::serialconnect(QString comname, QString bandrate)
|
|
|
|
|
{
|
2020-08-23 17:06:25 +08:00
|
|
|
|
if (protools == RS232 || protools == RS485)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-05 18:15:32 +08:00
|
|
|
|
QSerialPortInfo info;
|
|
|
|
|
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach(info, infos) {
|
|
|
|
|
if (info.portName() == comname) break;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
if (i != infos.size())
|
|
|
|
|
{
|
|
|
|
|
serial->close();
|
|
|
|
|
serial->setPort(info);
|
|
|
|
|
bool aa = serial->open(QIODevice::ReadWrite);
|
|
|
|
|
qint32 b = bandrate.toInt();
|
|
|
|
|
serial->setBaudRate(b);
|
|
|
|
|
IsMotorInit = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
serial->close();
|
|
|
|
|
IsMotorInit = false;
|
|
|
|
|
}
|
2020-08-11 17:55:51 +08:00
|
|
|
|
return true;
|
2020-08-05 17:04:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void VinceControl::Handshacke(QString motorid)
|
|
|
|
|
{
|
|
|
|
|
SendCommandtoMotor("dev\n", motorid);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 18:15:32 +08:00
|
|
|
|
void VinceControl::SetRS485ID(QString id)
|
|
|
|
|
{
|
|
|
|
|
RS485ID = id;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::EnableMotro(QString id )
|
2020-08-05 17:04:25 +08:00
|
|
|
|
{
|
2020-08-23 17:06:25 +08:00
|
|
|
|
QString str = "ena\n";
|
|
|
|
|
if (id =="non")
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
SendCommandtoMotor(str);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SendCommandtoMotor(str, id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 18:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
2020-08-05 17:04:25 +08:00
|
|
|
|
|
|
|
|
|
}
|
2020-08-05 17:33:45 +08:00
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::DisableMotro(QString id )
|
2020-08-05 18:15:32 +08:00
|
|
|
|
{
|
|
|
|
|
QString str = "off\n";
|
2020-08-23 17:06:25 +08:00
|
|
|
|
SendCommandtoMotor(str,id);
|
2020-08-05 18:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::SendCommandtoMotor(QString str)
|
2020-08-05 17:33:45 +08:00
|
|
|
|
{
|
2020-08-05 18:15:32 +08:00
|
|
|
|
if (protools == RS232)
|
|
|
|
|
{
|
|
|
|
|
QByteArray buf;
|
|
|
|
|
buf.append(str);
|
|
|
|
|
serial->write(buf);
|
2020-08-23 17:06:25 +08:00
|
|
|
|
serial->waitForBytesWritten(1000);
|
2020-08-05 18:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
else if(protools==RS485)
|
|
|
|
|
{
|
|
|
|
|
QString str2 = RS485ID + " " + str;
|
|
|
|
|
QByteArray buf;
|
|
|
|
|
buf.append(str2);
|
|
|
|
|
serial->write(buf);
|
2020-08-23 17:06:25 +08:00
|
|
|
|
serial->waitForBytesWritten(1000);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (protools==NETTCP)
|
|
|
|
|
{
|
|
|
|
|
if (tcpSocket.length() == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tcpSocket[0]->write(str.toUtf8().data());
|
|
|
|
|
}
|
|
|
|
|
tcpSocket[0]->waitForBytesWritten(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::SendCommandtoMotor(QString str, QString modor)
|
|
|
|
|
{
|
|
|
|
|
if (modor == "non")
|
|
|
|
|
{
|
|
|
|
|
SendCommandtoMotor(str);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lenth = tcpSocket.length();
|
|
|
|
|
int lenthstring = Motorlist.length();
|
|
|
|
|
if (lenthstring!=lenth)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < lenth; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Motorlist.at(i)==modor)
|
|
|
|
|
{
|
|
|
|
|
tcpSocket[i]->write(str.toUtf8().data());
|
|
|
|
|
tcpSocket[i]->waitForBytesWritten(1000);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-05 17:33:45 +08:00
|
|
|
|
|
2020-08-05 18:15:32 +08:00
|
|
|
|
}
|
2020-08-05 17:33:45 +08:00
|
|
|
|
}
|
2020-08-11 17:55:51 +08:00
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::MoveSetDistance(long distance,QString motornetid )
|
2020-08-11 17:55:51 +08:00
|
|
|
|
{
|
2020-08-23 17:06:25 +08:00
|
|
|
|
QString commonstr = "rmv " + QString::number(distance) + "\n";
|
|
|
|
|
SendCommandtoMotor(commonstr, motornetid);
|
2020-08-11 17:55:51 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::MoveMotar(bool direction, QString motornetid )
|
2020-08-11 17:55:51 +08:00
|
|
|
|
{
|
2020-08-23 17:06:25 +08:00
|
|
|
|
if (motornetid == "non")
|
|
|
|
|
{
|
|
|
|
|
if (protools != NETTCP)
|
|
|
|
|
{
|
|
|
|
|
if (!SpeedisSet)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (direction)
|
|
|
|
|
{
|
|
|
|
|
SettingSpeedByThis(abs(Speednow));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SettingSpeedByThis(abs(Speednow)*-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (protools == NETTCP&&Motorlist.length() > 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (!isSettingSpeedlist[0])
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (direction)
|
|
|
|
|
{
|
|
|
|
|
SettingSpeedByThis(abs(Speedlist[0]), motornetid);;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SettingSpeedByThis(abs(Speedlist[0])*-1, motornetid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int lenthstring = Motorlist.length();
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lenthstring; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Motorlist.at(i) == motornetid)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (!isSettingSpeedlist[i])
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (direction)
|
|
|
|
|
{
|
|
|
|
|
SettingSpeedByThis(abs( Speedlist[i]), motornetid);;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
int a = abs(Speedlist[i]);
|
|
|
|
|
SettingSpeedByThis(abs(Speedlist[i])*-1, motornetid);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-11 17:55:51 +08:00
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
QThread::msleep(100);
|
|
|
|
|
QString commendtosend = "mov\n";
|
|
|
|
|
SendCommandtoMotor(commendtosend, motornetid);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::SettingSpeed(unsigned long Speed, QString motornetid /*= "non"*/)
|
|
|
|
|
{
|
|
|
|
|
SettingSpeedByThis(Speed, motornetid);
|
|
|
|
|
//speed
|
2020-08-11 17:55:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::SettingSpeedByThis( long Speed, QString motornetid)
|
2020-08-11 17:55:51 +08:00
|
|
|
|
{
|
2020-08-23 17:06:25 +08:00
|
|
|
|
if (motornetid=="non")
|
|
|
|
|
{
|
|
|
|
|
if (protools==NETTCP&&Motorlist.length()>0)
|
|
|
|
|
{
|
|
|
|
|
isSettingSpeedlist[0] = true;
|
|
|
|
|
Speedlist[0] = Speed;
|
|
|
|
|
}
|
|
|
|
|
if (protools!=NETTCP)
|
|
|
|
|
{
|
|
|
|
|
Speednow = Speed;
|
|
|
|
|
SpeedisSet = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int lenthstring = Motorlist.length();
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lenthstring; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Motorlist.at(i) == motornetid)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
isSettingSpeedlist[i] = true;
|
|
|
|
|
Speedlist[i] = Speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QString commentosend = "cfg spd=" + QString::number(Speed) + "\n";
|
|
|
|
|
SendCommandtoMotor(commentosend, motornetid);
|
2020-08-11 17:55:51 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:06:25 +08:00
|
|
|
|
void VinceControl::MovetoZero(QString motornetid )
|
2020-08-11 17:55:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long VinceControl::GetLocationNow()
|
|
|
|
|
{
|
|
|
|
|
return 10000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::MoveToLocation(long Location)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::SettingUpandDownSpeed(int addspeed, int downspeed)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2020-08-23 17:06:25 +08:00
|
|
|
|
|
|
|
|
|
void VinceControl::StopMotormove(QString motornetid )
|
|
|
|
|
{
|
|
|
|
|
QString commonstr = "stp\n";
|
|
|
|
|
SendCommandtoMotor(commonstr, motornetid);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VinceControl::VinceControl(ProTools proto, int port)
|
|
|
|
|
{
|
|
|
|
|
if (proto != NETTCP)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
protools = proto;
|
|
|
|
|
//Motor1IpAddress.append("192.168.1.1");
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BD><EFBFBD>,ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>տռ<D5BF>
|
|
|
|
|
tcpServer = new QTcpServer();
|
|
|
|
|
|
|
|
|
|
//setWindowTitle("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>6666");
|
|
|
|
|
tcpServer->listen(QHostAddress::Any, port);
|
|
|
|
|
connect(tcpServer, &QTcpServer::newConnection, this, &VinceControl::onNewTcpClinetConnet);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
void VinceControl::onNewTcpClinetConnet()
|
|
|
|
|
{
|
|
|
|
|
if (protools != NETTCP)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QTcpSocket *tcpsocket1;
|
|
|
|
|
|
|
|
|
|
tcpSocket.append(tcpServer->nextPendingConnection());
|
|
|
|
|
|
|
|
|
|
QString ip = tcpSocket[tcpSocket.length() - 1]->peerAddress().toString().split("::ffff:")[1];
|
|
|
|
|
Motorlist.append(ip);
|
|
|
|
|
bool statofmotor = false;
|
|
|
|
|
isSettingSpeedlist.append(statofmotor);
|
|
|
|
|
int speed = 200;
|
|
|
|
|
Speedlist.append(speed);
|
|
|
|
|
qint32 port = tcpSocket[tcpSocket.length() - 1]->peerPort();
|
|
|
|
|
QString str11 = ip + " Connected The Port Number is "+port+"\n";
|
|
|
|
|
SendLog(str11);
|
|
|
|
|
qDebug() << ip << ":" << port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::onReciveFromClinet()
|
|
|
|
|
{
|
|
|
|
|
QTcpSocket* sc = dynamic_cast<QTcpSocket*>(sender());
|
|
|
|
|
QString ip = sc->peerAddress().toString().split("::ffff:")[1];
|
|
|
|
|
//<2F><>ȡ<EFBFBD>Է<EFBFBD><D4B7><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
#ifdef LOGOUT
|
|
|
|
|
QByteArray array = sc->readAll();
|
|
|
|
|
QString strofrecive;
|
|
|
|
|
strofrecive.append(array);
|
|
|
|
|
QString strtosend = "sender " + ip + ":" + strofrecive;
|
|
|
|
|
SendLog(strtosend);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::onClinetDisConnet()
|
|
|
|
|
{
|
|
|
|
|
QTcpSocket* sc = dynamic_cast<QTcpSocket*>(sender());
|
|
|
|
|
QString ip = sc->peerAddress().toString().split("::ffff:")[1];
|
|
|
|
|
int lenth = Motorlist.length();
|
|
|
|
|
for (size_t i = 0; i < lenth; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Motorlist[i]==ip)
|
|
|
|
|
{
|
|
|
|
|
Motorlist.removeAt(i);
|
|
|
|
|
isSettingSpeedlist.removeAt(i);
|
|
|
|
|
Speedlist.removeAt(i);
|
|
|
|
|
delete tcpSocket[i];
|
|
|
|
|
tcpSocket.removeAt(i);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VinceControl::SendLog(QString str)
|
|
|
|
|
{
|
|
|
|
|
#ifdef LOGOUT
|
|
|
|
|
emit SendLogToCallClass(str);
|
|
|
|
|
#endif // LOGOUT
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|