1 Star 1 Fork 1

幻煜/SKYserialtool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
command.cpp 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
huanyuai 提交于 2024-09-01 21:55 . 第一次提交
#include "command.h"
Command::DataType Command::stringToDataType(const QString &dataTypeStr) const {
if (dataTypeStr == "uint" || dataTypeStr == "Uint" || dataTypeStr == "UINT") {
return UINT;
} else if (dataTypeStr == "int" || dataTypeStr == "Int" || dataTypeStr == "INT") {
return INT;
} else if (dataTypeStr == "float" || dataTypeStr == "Float" || dataTypeStr == "FLOAT") {
return FLOAT;
} else if (dataTypeStr == "bool" || dataTypeStr == "Bool" || dataTypeStr == "BOOL") {
return BOOL;
} else {
return UNKNOWN;
}
}
QByteArray Command::generateCommandBytes() {
QByteArray byteArray;
QDataStream stream(&byteArray, QDataStream::WriteOnly);
stream.setByteOrder(QDataStream::ByteOrder::LittleEndian);
// MagicNum
uint8_t magicNum = 0xAB;
stream << magicNum;
// ChunkNum
uint8_t chunkNum = 1;
stream << chunkNum;
// CMD number
stream << m_number;
// data
switch (m_dataType) {
case UINT: {
uint32_t value = m_data.toUInt();
stream << value;
break;
}
case INT: {
int32_t value = m_data.toInt();
stream << value;
break;
}
case FLOAT: {
float value = m_data.toFloat();
stream.writeRawData(reinterpret_cast<const char*>(&value), sizeof(float));
break;
}
case BOOL: {
uint32_t boolValue = m_data.toUInt() ? 1 : 0;
stream << boolValue;
break;
}
case UNKNOWN:
default:
qDebug() << "wrong data type: ";
// Handle unknown data type if necessary
break;
}
return byteArray;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/huanyuai/SKYserialtool.git
git@gitee.com:huanyuai/SKYserialtool.git
huanyuai
SKYserialtool
SKYserialtool
main

搜索帮助