IP地址:可以根据UR机器人控制终端读取到IP地址
端口:端口取30001或者30002或者30003,(本实验使用的是30002端口)
脚本指令可以直接从官网下载,也可以从相关CSDN帖子材料中获得,或者联系我QQ:476596998
服务器:UR5
客户端:PC上位机
注意事项:
1、连接好时,URScript 程序或命令以清晰文本形式在每一个 socket 端口发送。每行均以“\n”结束。在编程的时候务必确定***终发送的文本中的格式为:命令(参数)\n
本人是以QT5.6来编写的上位机程序:如下所示:
界面:
mainwindow.cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qmessagebox.h"
#include "qhostaddress.h"
#include "qbytearray.h"
#include "qpalette.h"
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Init();
}
MainWindow::~MainWindow()
{
delet ui;
}
void MainWindow::Init()
{
mysocket = new QTcpSocket(this);
ui->comboBox_Protocol->insertItem(0,"TCPSever");
ui->comboBox_Protocol->insertItem(1,"TCPClient");
ui->pushButton_DisLink ->setDisabled(true);
connect(ui->pushButton_Link ,SIGNAL(clicked(bool)),this,SLOT(do_pushButtonLink _clicked(bool)));
connect(ui->pushButton_DisLink ,SIGNAL(clicked(bool)),this,SLOT(do_pushButtonDisLink _clicked(bool)));
connect(ui->pushButton_SendData,SIGNAL(clicked(bool)),this,SLOT(do_pushButtonSendData_clicked(bool)));
connect(ui->pushButton_ClearReceive,SIGNAL(clicked(bool)),this,SLOT(do_pushButtonClearReceive_clicked(bool)));
connect(ui->pushButton_ClearSend,SIGNAL(clicked(bool)),this,SLOT(do_pushButtonClearSend_clicked(bool)));
}
void MainWindow::do_pushButtonClearSend_clicked(bool)
{
ui->textEdit_SendField->clear();
}
void MainWindow::do_pushButtonClearReceive_clicked(bool)
{
ui->textEdit_ReceiveField->clear();
}
void MainWindow::do_pushButtonSendData_clicked(bool)
{
if(ui->textEdit_SendField->toPlainText().isEmpty())
{
return;
}
QString sendText = ui->textEdit_SendField->toPlainText()+"\n";
mysocket->write(sendText.toLocal8Bit());
ui->textEdit_ReceiveField->append(sendText);
ui->textEdit_SendField->clear();
}
void MainWindow::do_pushButtonDisLink _clicked(bool)
{
mysocket->disconnect();
mysocket->close();
ui->pushButton_DisLink ->setDisabled(true);
ui->pushButton_Link ->setDisabled(false);
}
void MainWindow::do_pushButtonLink _clicked(bool)
{
if(ui->lineEdit_IPAddr->text().isEmpty())
{
QMessageBox::information(this,"提示:","IP地址不能为空");
return;
}
if(ui->lineEdit_Port->text().isEmpty())
{
QMessageBox::information(this,"提示","端口号不能为空");
return;
}
myIPAddress = (QHostAddress)ui->lineEdit_IPAddr->text();
myIPPort = ui->lineEdit_Port->text().toUInt();
mysocket->connectToHost(myIPAddress,myIPPort);
if(!mysocket->waitForConnected())
{
QMessageBox::information(this,"提示:","连接失败,请重新连接》》》");
return;
}
ui->pushButton_Link ->setDisabled(true);
ui->pushButton_DisLink ->setDisabled(false);
connect(mysocket,SIGNAL(readyRead()),this,SLOT(do_mysocket_readyRead()));
connect(mysocket,SIGNAL(connected()),this,SLOT(do_mysocket_connected()));
}
void MainWindow::do_mysocket_connected()
{
ui->pushButton_Link ->setBackgroundRole(QPalette::Window);
}
void MainWindow::do_mysocket_readyRead()
{
QByteArray receiveText = mysocket->readAll();
ui->textEdit_ReceiveField->append(QString("获得的字节数:%1").arg(mysocket->bytesAvailable()));
ui->textEdit_ReceiveField->append(QString("所获得的数据:"+receiveText.tohe().toUpper()));
//QByteArray receiveLine = mysocket->readLine(1024);
//ui->textEdit_ReceiveField->append((QString)receiveLine.tohe());
//ui->textEdit_ReceiveField->append("\n");
}
mainwindow.h文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "qtcpsocket.h"
#include "qhostaddress.h"
#pragma execution_character_set("utf-8")
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public://设定全局变量:
QTcpSocket *mysocket;
QHostAddress myIPAddress;
quint16 myIPPort;
public://设定全局函数:
void Init();
public slots:
void do_pushButtonLink _clicked(bool);
void do_mysocket_readyRead();
void do_pushButtonDisLink _clicked(bool);
void do_pushButtonSendData_clicked(bool);
void do_pushButtonClearSend_clicked(bool);
void do_pushButtonClearReceive_clicked(bool);
void do_mysocket_connected();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp文件
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
工程文件:URClient.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-09-06T15:53:17
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = URClient
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
发送的命令实例:
movej([0.244,-2.25,-0.89,-3.19,0,0],a=0.1,v=0.1,t=0,r=0)
注意:以上实例中“\n”已经被内嵌入程序中,可以去程序中查找
————————————————
更多详情:UR协作机器人