// // Created by Administrator on 2025/12/9. // #include "probe_tube.h" #include #include #include "Public/crc16util.h" ProbeTube::ProbeTube(QObject *parent) : QObject(parent) { qDebug() << "探针管构造函数"; // 连接串口 serial.setPortName("COM2"); serial.setBaudRate(QSerialPort::Baud115200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); connect(&serial, SIGNAL(readyRead()), this, SLOT(SerialRead())); if (!serial.open(QIODevice::ReadWrite)) { qDebug() << "打开探测镜筒串口失败" << serial.errorString(); isConnected = false; } else { qDebug() << "打开探测镜筒串口成功"; isConnected = true; } } ProbeTube::~ProbeTube() { qDebug() << "探针管析构函数"; serial.close(); } void ProbeTube::doBakeZero() { // 发送归零指令 QString cmd = "01 06 23 B0 00 01"; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送归零指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); } void ProbeTube::doControlModeSelectionA() { // 发送控制模式选择指令 QString cmd = "01 06 21 09 00 01"; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送控制模式选择指令A:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待控制模式选择指令A执行完成 } void ProbeTube::doControlModeSelectionB() { // 发送控制模式选择指令 QString cmd = "01 06 23 10 00 03"; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送控制模式选择指令B:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待控制模式选择指令B执行完成 } void ProbeTube::doGetAbsolutePosition() { // 发送获取绝对位置指令 QString cmd = "01 06 23 11 00 01"; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送获取绝对位置指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待获取绝对位置指令执行完成 } void ProbeTube::doTargetSpeed(int speed) { // 发送目标速度指令 QString cmd = "01 06 23 21"; QByteArray speedBytes; speedBytes.resize(2); speedBytes[0] = (speed >> 8) & 0xFF; // 高字节 speedBytes[1] = speed & 0xFF; // 低字节 QString speedHex = speedBytes.toHex().toUpper(); QString speedStr = speedHex.insert(2, " "); cmd = cmd + " " + speedStr; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送目标速度指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待目标速度指令执行完成 } void ProbeTube::doAcceleratedSpeed(int speed) { // 发送加速速度指令 QString cmd = "01 06 23 22"; QByteArray speedBytes; speedBytes.resize(2); speedBytes[0] = (speed >> 8) & 0xFF; // 高字节 speedBytes[1] = speed & 0xFF; // 低字节 QString speedHex = speedBytes.toHex().toUpper(); QString speedStr = speedHex.insert(2, " "); cmd = cmd + " " + speedStr; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送加速速度指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待加速速度指令执行完成 } void ProbeTube::doDecelerationSpeed(int speed) { // 发送减速度指令 QString cmd = "01 06 23 23"; QByteArray speedBytes; speedBytes.resize(2); speedBytes[0] = (speed >> 8) & 0xFF; // 高字节 speedBytes[1] = speed & 0xFF; // 低字节 QString speedHex = speedBytes.toHex().toUpper(); QString speedStr = speedHex.insert(2, " "); cmd = cmd + " " + speedStr; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待减速度指令执行完成 } void ProbeTube::doMoveTargetPositionA() { // 发送移动目标位置指令 QString cmd = "01 06 23 16 00 00"; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送移动目标位置指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待移动目标位置指令执行完成 } void ProbeTube::doMoveTargetPositionB() { // 发送移动目标位置指令 QString cmd = "01 06 23 16 00 01"; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送移动目标位置指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待移动目标位置指令执行完成 } void ProbeTube::doSetTargetPosition(int position) { // 发送设置目标位置指令 QString cmd = "01 10 23 20 00 02 04"; QByteArray positionBytes; positionBytes.resize(4); positionBytes[0] = (position >> 24) & 0xFF; // 最高字节 positionBytes[1] = (position >> 16) & 0xFF; // 次高字节 positionBytes[2] = (position >> 8) & 0xFF; // 次低字节 positionBytes[3] = position & 0xFF; // 转换为十六进制字符串并确保格式正确 QString positionHex = positionBytes.toHex().toUpper(); QString positionStr; for (int i = 0; i < positionHex.length(); i += 2) { if (i > 0) positionStr += " "; positionStr += positionHex.mid(i, 2); } cmd = cmd + " " + positionStr; QString crc16Str = QString::fromStdString(Crc16Util::getCRC16(cmd.toStdString())).insert(2," "); cmd = cmd + " " + crc16Str; qDebug() << "发送设置目标位置指令:" << cmd; serial.clear(); serial.write(QByteArray::fromHex(cmd.toUtf8())); // 等待设置目标位置指令执行完成 } void ProbeTube::SerialRead() { qDebug() << "探针管串口读取数据"; m_readBuffer.clear(); QByteArray newData = serial.readAll(); m_readBuffer.append(newData); qDebug() << "m_readBuffer数据:" << m_readBuffer.toHex(' ') << "大小:" << m_readBuffer.size(); while (m_readBuffer.size() == 8) { // crc16校验 QString hexStr = m_readBuffer.right(2).toHex(); QString crc16Str; for (int i = 0; i < hexStr.length(); i += 2) { if (i > 0) crc16Str += " "; crc16Str += hexStr.mid(i, 2).toUpper(); } QString cmd = m_readBuffer.left(m_readBuffer.size() - 2).toHex(); QString spacedCmd; for (int i = 0; i < cmd.length(); i += 2) { if (i > 0) spacedCmd += " "; spacedCmd += cmd.mid(i, 2).toUpper(); } QString crc16StrCal = QString::fromStdString(Crc16Util::getCRC16(spacedCmd.toStdString())).insert(2," ").toUpper(); if (crc16Str != crc16StrCal) { qDebug() << "crc16校验错误" << "crc16Str:" << crc16Str << "crc16StrCal:" << crc16StrCal; return; } // 解析数据 quint8 id = m_readBuffer[3]; switch (id) { case 0x09: // 解析绝对位置数据 qDebug() << "控制模式选择A指令执行成功"; emit finishControlModeSelectionA(); break; case 0x10: // 解析绝对位置数据 qDebug() << "控制模式选择B指令执行成功"; emit finishControlModeSelectionB(); break; case 0x11: // 解析绝对位置数据 qDebug() << "绝对位置指令执行成功"; emit finishGetAbsolutePosition(); break; case 0x16: // 解析绝对位置数据 id = m_readBuffer[5]; switch (id) { case 0x00: qDebug() << "执行当前位置A指令成功"; emit finishMoveTargetPositionA(); break; case 0x01: qDebug() << "执行当前位置B指令成功"; emit finishMoveTargetPositionB(); break; default: qDebug() << "执行当前位置指令失败"; break; } break; case 0x20: // 解析绝对位置数据 qDebug() << "设置目标位置指令执行成功"; emit finishSetTargetPosition(); break; case 0x21: // 解析绝对位置数据 qDebug() << "目标速度指令执行成功"; emit finishTargetSpeed(); break; case 0x22: // 解析绝对位置数据 qDebug() << "加速速度指令执行成功"; emit finishAcceleratedSpeed(); break; case 0x23: // 解析绝对位置数据 qDebug() << "减速速度指令执行成功"; emit finishDecelerationSpeed(); break; case 0xB0: // 解析绝对位置数据 qDebug() << "归零指令执行成功"; emit finishBakeZero(); break; default: qDebug() << "未知数据ID"; break; } return; } }