完善了像马达发送命令的函数 修改了控制函数为带有默认参数的函数 区分单个马达和多个马达 等等

This commit is contained in:
xin
2020-08-23 17:06:25 +08:00
parent 994fa0570d
commit 2f67b91000
6 changed files with 440 additions and 45 deletions

View File

@ -1,20 +1,37 @@
#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()
{
serial->close();
IsMotorInit = false;
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;
@ -39,33 +56,50 @@ bool VinceControl::serialconnect(QString comname, QString bandrate)
return true;
}
void VinceControl::Handshacke(QString motorid)
{
SendCommandtoMotor("dev\n", motorid);
}
void VinceControl::SetRS485ID(QString id)
{
RS485ID = id;
}
void VinceControl::EnableMotro()
void VinceControl::EnableMotro(QString id )
{
QString str = "ena\n";
SendCommandtoSerial(str);
QString str = "ena\n";
if (id =="non")
{
SendCommandtoMotor(str);
}
else
{
SendCommandtoMotor(str, id);
}
}
void VinceControl::DisableMotro()
void VinceControl::DisableMotro(QString id )
{
QString str = "off\n";
SendCommandtoSerial(str);
SendCommandtoMotor(str,id);
}
void VinceControl::SendCommandtoSerial(QString str)
void VinceControl::SendCommandtoMotor(QString str)
{
if (protools == RS232)
{
QByteArray buf;
buf.append(str);
serial->write(buf);
serial->waitForBytesWritten(1000);
}
else if(protools==RS485)
{
@ -73,26 +107,175 @@ void VinceControl::SendCommandtoSerial(QString 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)
void VinceControl::MoveSetDistance(long distance,QString motornetid )
{
QString commonstr = "rmv " + QString::number(distance) + "\n";
SendCommandtoMotor(commonstr, motornetid);
}
void VinceControl::MoveMotar(bool direction)
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)
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()
void VinceControl::MovetoZero(QString motornetid )
{
}
@ -111,3 +294,97 @@ 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
}