本文记录在Qt中使用RS24ngc的一些使用注意事项。仅供参考,不喜勿喷,谢谢!
导入rs274ngc源码到Qt
在Qt工程里通过source右键把所有的rs274ngc的源码都选上(driver.cc和Makefile不需要)。
最好在Qt工程内创建一个rs274ngc文件夹,将需要的rs274ngc源码从rs274ngc-main/rs274ngc/所有源码,复制到rs274ngc中,删去其中的Makefile和driver.cc
创建一个QThread线程执行rs274ngc
ngcThread.cc内容
#include "ngcThread.h"
#include "rs274ngc/myNGC.hh"
#include <QString>
#include <QDebug>
#include "rs274ngc/myNGC.hh"
myNGC *workNGC=nullptr;
void ngcThread::run()
{
workNGC = new myNGC(this);
while(1){
msleep(200);
}
}
void ngcThread::slotGetGCode(const QString &str)
{
workNGC->getOneLine(str);
}
ngcThread.hh内容
#ifndef NGCTHREAD_H__
#define NGCTHREAD_H__
#include <QThread>
#include <QWidget>
class ngcThread:public QThread
{
Q_OBJECT;
public:
void run() override;
public slots:
void slotGetGCode(const QString &str);
};
#endif
创建一个myNGC类
用于实现rs274函数接口调用。myNGC.CC源码
#include "myNGC.hh"
#include <QObject>
#include <QDebug>
#include <QFile>
#include <cstdio>
#include <cstring>
#include <QCoreApplication>
#include "canon.hh"
#include "rs274ngc.hh"
#include "rs274ngc_return.hh"
#define GCODE_LINE_MAX 256
FILE *_outfile=nullptr;
extern char _parameter_file_name[];
extern CANON_TOOL_TABLE _tools[];
extern int _tool_max;
myNGC::myNGC(QObject *parent):QObject(parent)
{
QString exePath = QCoreApplication::applicationDirPath();
QString outPath = exePath + "/out.bin";
char buff[20]="TEST NGC";
this->rs274Flag = false;
char paraFileName[]=":/rs274ngc.var";
// 输出结果到文件中
_outfile = fopen(outPath.toUtf8(), "wb+");
outPath = exePath + "/rs274ngc.var";
memcpy(_parameter_file_name, outPath.toUtf8(), strlen(outPath.toUtf8()));
if(0 == read_tool_file(":/rs274ngc.tool_default")){
if(RS274NGC_OK == rs274ngc_init()){
this->rs274Flag = true;
qDebug()<<"RS274 OK"<<endl;
}
}
}
myNGC::~myNGC()
{
}
void myNGC::getOneLine(QString str)
{
int status;
gcodeLine = str;
char gcodeLine[GCODE_LINE_MAX];
if(this->rs274Flag == true){
this->copyToArray(str, gcodeLine);
status = rs274ngc_read(gcodeLine);
qDebug()<<status<<endl;
if(RS274NGC_OK == status){
status = rs274ngc_execute();
qDebug()<<status<<endl;
if(RS274NGC_OK == status){
fflush(_outfile);
QFile file("./out.bin");
file.open(QFile::ReadOnly | QIODevice::Text);
QTextStream in(&file);
while(! in.atEnd()){
QString line = in.readLine();
qDebug()<<line;
}
}
}
}
}
int myNGC::read_tool_file(const char *name)
{
QFile toolFile(name);
int getFlag = -1;
int slot;
toolFile.open(QFile::ReadOnly);
if(toolFile.size() > 0){
// 跳过表头的内容,找到刀具参数实际内容
QTextStream infile(&toolFile);
QString getStr;
while(!infile.atEnd()){
getStr = infile.readLine();
if(0 == getStr.size()){ // C语言需要判断的是\n符号
getFlag = 0;
break;
}
}
if(getFlag != 0){
toolFile.close();
return -1;
}
for(slot = 0; slot <= _tool_max; slot ++){
_tools[slot].id = -1;
_tools[slot].length = 0;
_tools[slot].diameter = 0;
}
while(! infile.atEnd()){
getStr = infile.readLine();
QStringList list = getStr.split(QRegExp("\\s+"));
// foreach (QString tempStr, list) {
// qDebug()<<tempStr<<endl;
// }
getStr = list.at(0);
slot = getStr.toInt();
if(slot > 0 && slot < _tool_max){
getStr = list.at(1);
_tools[slot].id = getStr.toInt();
getStr = list.at(2);
_tools[slot].length = getStr.toDouble();
getStr = list.at(3);
_tools[slot].diameter = getStr.toDouble();
// qDebug()<<"slot"<<slot<<_tools[slot].id<<" len"<<_tools[slot].length<<"diam"<<_tools[slot].diameter<<endl;
}
}
}
toolFile.close();
return 0;
}
void myNGC::copyToArray(QString &str, char array[])
{
QByteArray byteArry = str.toUtf8();
strcpy(array, byteArry.data());
}
myNGC.hh源码
#ifndef MYNGC_HH_
#define MYNGC_HH_
#include <QObject>
#include <QString>
#include <QFile>
#include <cstdlib>
#include "../ngcThread.h"
class myNGC:public QObject
{
Q_OBJECT;
public:
explicit myNGC(QObject *parent = nullptr);
~myNGC();
void getOneLine(QString str);
int read_tool_file(const char *name);
void copyToArray(QString &str, char strArray[]);
private:
QString gcodeLine;
bool rs274Flag;
};
#endif
main.cpp实体化myThread类并连接信号和槽
#include "mainwidget.h"
#include <QApplication>
#include <QFile>
#include <QString>
#include "ngcThread.h"
#include <fstream>
#include <QThread>
#include <QObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ngcThread ngcTask;
mainWidget w(&ngcTask);
QObject::connect(&w, &mainWidget::sendInputText, &ngcTask, &ngcThread::slotGetGCode);
w.show();
ngcTask.start();
return a.exec();
}
在需要的控件页的头文件内,声明一个信号sig(QString Str)用于将需要译码的文本信息发送到myNGC类的槽函数中。
使用时,在需要的位置通过emit发送sig(QString Str)信号即可。
emit this->sendInputText(gCodeStr);
发送G代码:g0x100y100
实验结果显示:
UI页面:
录入G代码文本后,点击就绪按钮,发送到ngcThread线程,执行并输出,后期可以和运动控制指令结合实现运动控制。
注意事项
1、必须让rs274ngc.tool_default和rs274ngc.var文件能正确指示路径并打开。
2、输出文件_outfile也需要指示到正确的文件,确保文件能打开。
3、通过中间数据即可实现UI和rs274ngc的数据互传。
4、以上只是学习示例,仅供参考,不喜勿喷,谢谢。
仅供学习:Qt工程源码