QT下的压电陶瓷使用小工具实现
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
piezoceramics.cpp
HEADERS += \
piezoceramics.h
FORMS += \
piezoceramics.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "piezoceramics.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PiezoCeramics w;
w.show();
return a.exec();
}
#ifndef PIEZOCERAMICS_H
#define PIEZOCERAMICS_H
#include <QWidget>
#include <QUdpSocket>
#include <QNetworkDatagram>
#include <QHostAddress>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class PiezoCeramics; }
QT_END_NAMESPACE
class PiezoCeramics : public QWidget
{
Q_OBJECT
public:
PiezoCeramics(QWidget *parent = nullptr);
~PiezoCeramics();
static uchar* encode(double ddata, uchar arrdata[4]);
static double decode(uchar data0, uchar data1, uchar data2, uchar data3);
static uchar getCheckSum(QByteArray ba);
void sendSinglePointMotion(double data);
void readMovingOrVoltage();
void readOpenOrCloseState();
void setOpenOrCloseState(bool oFlag);
void sendDataToPZT(uchar *data, int length);
public slots:
void readData();
private slots:
void on_connectBtn_clicked();
void on_disconnectBtn_clicked();
void on_closeLoopBtn_clicked();
void on_openLoopBtn_clicked();
void on_moveBtn_clicked();
signals:
void sendIsConnected(bool isConnnected, bool oFlag);
void sendMovingOrVoltageData(double data);
private:
Ui::PiezoCeramics *ui;
QUdpSocket * udpSocket = nullptr;
QTimer* m_timer = nullptr;
bool m_bDeviceIsOpen = false;
bool m_bDeviceIsConnect = false;
QChar m_OpenOrCloseFlag;
};
#endif // PIEZOCERAMICS_H
#include "piezoceramics.h"
#include "ui_piezoceramics.h"
#include <QMessageBox>
#include <QThread>
PiezoCeramics::PiezoCeramics(QWidget *parent)
: QWidget(parent)
, ui(new Ui::PiezoCeramics)
{
ui->setupUi(this);
udpSocket = new QUdpSocket(this);
m_timer = new QTimer(this);
m_timer->setInterval(1000);
connect(m_timer, &QTimer::timeout, this, &PiezoCeramics::readMovingOrVoltage);
m_timer->start();
}
PiezoCeramics::~PiezoCeramics()
{
delete ui;
}
uchar *PiezoCeramics::encode(double ddata, uchar arrdata[4])
{
if (ddata < 0)
{
ddata = -ddata;
int a = int(ddata);
arrdata[0] = a / 256 + 0x80;
arrdata[1] = a % 256;
a = int((ddata - a) * 10000);
arrdata[2] = a / 256;
arrdata[3] = a % 256;
}
else
{
int a = int(ddata);
arrdata[0] = a / 256;
arrdata[1] = a % 256;
a = int((ddata - a + 0.000001) * 10000);
arrdata[2] = a / 256;
arrdata[3] = a % 256;
}
return arrdata;
}
double PiezoCeramics::decode(uchar data0, uchar data1, uchar data2, uchar data3)
{
double d;
if (data0 & 0x80)
{
data0 -= 0x80;
d = (double)(data0 * 256 + data1 + (data2 * 256 + data3) * 0.0001);
d *= (-1);
}
else
{
d = (double)(data0 * 256 + data1 + (data2 * 256 + data3) * 0.0001);
}
return d;
}
uchar PiezoCeramics::getCheckSum(QByteArray ba)
{
uchar checkSum = 0x00;
for (int i = 0; i < ba.size(); i++) {
uchar ch = ba[i];
checkSum = ch ^ checkSum;
}
return checkSum;
}
void PiezoCeramics::sendSinglePointMotion(double data)
{
uint8_t command[11] = {0xaa, 0x01, 0x0b, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if(m_OpenOrCloseFlag == 'O')
command[3] = 0x00;
unsigned char encoderData[4];
encode(data, encoderData);
command[6] = encoderData[0];
command[7] = encoderData[1];
command[8] = encoderData[2];
command[9] = encoderData[3];
QByteArray b;
for(int i = 0; i < 10; i++ ){
b.append(command[i]);
}
command[10] = getCheckSum(b);
b.append(command[10]);
sendDataToPZT(command, 11);
}
void PiezoCeramics::readMovingOrVoltage()
{
if(m_bDeviceIsConnect){
uint8_t command[7] = {0xaa, 0x01, 0x07, 0x06, 0x00, 0x00, 0xaa};
if(m_OpenOrCloseFlag == 'O'){
command[3] = 0x05;
command[6] = 0xa9;
}
sendDataToPZT(command,7);
QThread::sleep(20);
readData();
}
}
void PiezoCeramics::readOpenOrCloseState()
{
uint8_t OPenOrCloseCommand[7] = {0xaa, 0x01, 0x07, 0x13, 0x00, 0x00, 0xbf};
sendDataToPZT(OPenOrCloseCommand, 7);
}
void PiezoCeramics::setOpenOrCloseState(bool oFlag)
{
uint8_t command[8] = {0xaa, 0x01, 0x08, 0x12, 0x00, 0x00, 0x4f, 0xfe};
if(!oFlag){
command[6] = 0x43;
command[7] = 0xf2;
}
sendDataToPZT(command, 8);
}
void PiezoCeramics::sendDataToPZT(uchar *data, int length)
{
QByteArray ba;
for(int i = 0; i < length; i++ ){
ba.append(data[i]);
}
udpSocket->writeDatagram(ba, QHostAddress(ui->addressRemote->text()), ui->portRemote->text().toInt());
}
void PiezoCeramics::readData()
{
QMessageBox msgBox;
QByteArray ba;
QString strTemp;
ba.resize(udpSocket->bytesAvailable());
udpSocket->readDatagram(ba.data(),ba.size());
if(!ba.isEmpty()){
strTemp = QString(ba);
}
if((strTemp.size() == 8) && (strTemp[6] == 'C' || strTemp[6] == 'O')){
m_bDeviceIsConnect = true;
bool oFlag = strTemp[6] == 'O' ? true :false;
m_OpenOrCloseFlag = strTemp[6];
emit sendIsConnected(true, oFlag);
msgBox.setText("Success on Linking Piezoceramic!");
msgBox.exec();
return ;
}
if(ba.size() == 11 &&(static_cast<quint8>(ba[3])==0x05|| static_cast<quint8>(ba[3])==0x06))
{
double data = decode(ba[6], ba[7], ba[8], ba[9]);
ui->posCurrent->setText(QString::number(data,10,5));
emit sendMovingOrVoltageData(data);
}
ba.clear();
}
void PiezoCeramics::on_connectBtn_clicked()
{
QMessageBox qmsbox;
udpSocket->bind(QHostAddress(ui->addressLocal->text()),ui->portLocal->text().toInt());
connect(udpSocket, &QUdpSocket::readyRead, this, &PiezoCeramics::readData);
m_bDeviceIsOpen = (udpSocket->state()==QAbstractSocket::BoundState);
if(m_bDeviceIsOpen){
readOpenOrCloseState();
m_bDeviceIsConnect = true;
}else{
qmsbox.setText("Fail to link Piezoceramic!");
qmsbox.exec();
}
}
void PiezoCeramics::on_disconnectBtn_clicked()
{
udpSocket->close();
m_bDeviceIsOpen = false;
m_bDeviceIsConnect = false;
emit sendIsConnected(false, m_OpenOrCloseFlag == 'O');
}
void PiezoCeramics::on_closeLoopBtn_clicked()
{
setOpenOrCloseState(false);
}
void PiezoCeramics::on_openLoopBtn_clicked()
{
setOpenOrCloseState(true);
}
void PiezoCeramics::on_moveBtn_clicked()
{
sendSinglePointMotion(ui->posExpected->text().toDouble());
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<widget name="__qt_fake_top_level">
<widget class="QLineEdit" name="addressLocal">
<property name="geometry">
<rect>
<x>96</x>
<y>26</y>
<width>303</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>192.168.0.2</string>
</property>
</widget>
<widget class="QLineEdit" name="posCurrent">
<property name="geometry">
<rect>
<x>182</x>
<y>304</y>
<width>217</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLineEdit" name="portLocal">
<property name="geometry">
<rect>
<x>96</x>
<y>72</y>
<width>303</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>7010</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>9</x>
<y>165</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>设备IP地址</string>
</property>
</widget>
<widget class="QLineEdit" name="portRemote">
<property name="geometry">
<rect>
<x>96</x>
<y>211</y>
<width>303</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>7010</string>
</property>
</widget>
<widget class="QPushButton" name="moveBtn">
<property name="geometry">
<rect>
<x>182</x>
<y>396</y>
<width>217</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>移动到指定位置</string>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>9</x>
<y>350</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>设置PZT位置</string>
</property>
</widget>
<widget class="QLineEdit" name="posExpected">
<property name="geometry">
<rect>
<x>182</x>
<y>350</y>
<width>217</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
<widget class="QPushButton" name="closeLoopBtn">
<property name="geometry">
<rect>
<x>9</x>
<y>257</y>
<width>167</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>闭环</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>9</x>
<y>72</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>本地端口</string>
</property>
</widget>
<widget class="QPushButton" name="openLoopBtn">
<property name="geometry">
<rect>
<x>182</x>
<y>257</y>
<width>217</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>开环</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>9</x>
<y>26</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>本机IP地址</string>
</property>
</widget>
<widget class="QPushButton" name="connectBtn">
<property name="geometry">
<rect>
<x>9</x>
<y>118</y>
<width>167</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>连接</string>
</property>
</widget>
<widget class="QLineEdit" name="addressRemote">
<property name="geometry">
<rect>
<x>96</x>
<y>165</y>
<width>303</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>192.168.0.100</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>9</x>
<y>211</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>设备端口</string>
</property>
</widget>
<widget class="QPushButton" name="disconnectBtn">
<property name="geometry">
<rect>
<x>182</x>
<y>118</y>
<width>217</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>断开连接</string>
</property>
</widget>
<widget class="Spacer" name="horizontalSpacer">
<property name="geometry">
<rect>
<x>9</x>
<y>396</y>
<width>167</width>
<height>23</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>164</width>
<height>20</height>
</size>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>9</x>
<y>304</y>
<width>81</width>
<height>23</height>
</rect>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>当前PZT位置</string>
</property>
</widget>
</widget>
<resources/>
</ui>
原文地址:https://blog.csdn.net/zkmrobot/article/details/137270795
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!