391 lines
6.5 KiB
C++
391 lines
6.5 KiB
C++
#include "vincecontrol.h"
|
||
#include"QSerialPortInfo"
|
||
#include "QDebug"
|
||
#include "QThread"
|
||
VinceControl::VinceControl(ProTools proto)
|
||
{
|
||
protools = proto;
|
||
IsMotorInit = false;
|
||
RS485ID = "0";
|
||
Speednow = 0;
|
||
SpeedisSet = false;
|
||
}
|
||
|
||
|
||
|
||
VinceControl::~VinceControl()
|
||
{
|
||
if (protools == RS232 || protools == RS485)
|
||
{
|
||
serial->close();
|
||
IsMotorInit = false;
|
||
}
|
||
|
||
}
|
||
|
||
bool VinceControl::serialconnect(QString comname, QString bandrate)
|
||
{
|
||
if (protools == RS232 || protools == RS485)
|
||
{
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
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;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
|
||
void VinceControl::Handshacke(QString motorid)
|
||
{
|
||
SendCommandtoMotor("dev\n", motorid);
|
||
}
|
||
|
||
void VinceControl::SetRS485ID(QString id)
|
||
{
|
||
RS485ID = id;
|
||
}
|
||
|
||
void VinceControl::EnableMotro(QString id )
|
||
{
|
||
QString str = "ena\n";
|
||
if (id =="non")
|
||
{
|
||
|
||
SendCommandtoMotor(str);
|
||
}
|
||
else
|
||
{
|
||
SendCommandtoMotor(str, id);
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
void VinceControl::DisableMotro(QString id )
|
||
{
|
||
QString str = "off\n";
|
||
SendCommandtoMotor(str,id);
|
||
}
|
||
|
||
void VinceControl::SendCommandtoMotor(QString str)
|
||
{
|
||
if (protools == RS232)
|
||
{
|
||
QByteArray buf;
|
||
buf.append(str);
|
||
serial->write(buf);
|
||
serial->waitForBytesWritten(1000);
|
||
}
|
||
else if(protools==RS485)
|
||
{
|
||
QString str2 = RS485ID + " " + str;
|
||
QByteArray buf;
|
||
buf.append(str2);
|
||
serial->write(buf);
|
||
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;
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
void VinceControl::MoveSetDistance(long distance,QString motornetid )
|
||
{
|
||
QString commonstr = "rmv " + QString::number(distance) + "\n";
|
||
SendCommandtoMotor(commonstr, motornetid);
|
||
|
||
}
|
||
|
||
void VinceControl::MoveMotar(bool direction, QString motornetid )
|
||
{
|
||
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;
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
QThread::msleep(100);
|
||
QString commendtosend = "mov\n";
|
||
SendCommandtoMotor(commendtosend, motornetid);
|
||
|
||
}
|
||
|
||
void VinceControl::SettingSpeed(unsigned long Speed, QString motornetid /*= "non"*/)
|
||
{
|
||
SettingSpeedByThis(Speed, motornetid);
|
||
//speed
|
||
}
|
||
|
||
void VinceControl::SettingSpeedByThis( long Speed, QString motornetid)
|
||
{
|
||
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);
|
||
|
||
}
|
||
|
||
void VinceControl::MovetoZero(QString motornetid )
|
||
{
|
||
|
||
}
|
||
|
||
long VinceControl::GetLocationNow()
|
||
{
|
||
return 10000;
|
||
}
|
||
|
||
void VinceControl::MoveToLocation(long Location)
|
||
{
|
||
|
||
}
|
||
|
||
void VinceControl::SettingUpandDownSpeed(int addspeed, int downspeed)
|
||
{
|
||
|
||
}
|
||
|
||
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
|
||
|
||
}
|
||
|