gpt4 book ai didi

c++ - QT C++ 中的 UCHAR 到 Qstring

转载 作者:太空狗 更新时间:2023-10-29 21:14:24 24 4
gpt4 key购买 nike

我在 QT C++ 中有一个函数:

UCHAR GetResult(HANDLE Handle, UCHAR *Address, UCHAR *Status, int *Value)
{
UCHAR RxBuffer[9], Checksum;
DWORD Errors, BytesRead;
COMSTAT ComStat;
int i;

//Check if enough bytes can be read
ClearCommError(Handle, &Errors, &ComStat);
if(ComStat.cbInQue>8)
{
//Receive
ReadFile(Handle, RxBuffer, 9, &BytesRead, NULL);

Checksum=0;
for(i=0; i<8; i++)
Checksum+=RxBuffer[i];

if(Checksum!=RxBuffer[8]) return TMCL_RESULT_CHECKSUM_ERROR;

*Address=RxBuffer[0];
*Status=RxBuffer[2];
*Value=(RxBuffer[4] << 24) | (RxBuffer[5] << 16) | (RxBuffer[6] << 8) | RxBuffer[7];
} else return TMCL_RESULT_NOT_READY;

return TMCL_RESULT_OK;
}

它返回以下任一项的 UCHAR 值:

  • TMCL_RESULT_CHECKSUM_ERROR
  • TMCL_RESULT_NOT_READY
  • TMCL_RESULT_OK

基于输入参数。我现在想在 QLabel 中显示返回值,但问题当然是这些是 UCHAR 而不是字符串变量。

如何将这些值转换为 QString 以便输出它们。

我想做三个 if 语句,但认为有更好的方法。例如:

if (GetResult(RS232Handle, &Address, &Status, &Value)==TMCL_RESULT_OK) {
printf("TMCL_RESULT_OK");
}

最佳答案

首先,GetResult既不是Qt也不是C++。它是C代码。它可能会在 1993 年左右使用任何 32 位 Windows C 编译器进行编译。

如果你想让它现代化,你可以使用QSerialPort:

// https://github.com/KubaO/stackoverflown/tree/master/questions/serial-controller-40754585
#include <QtWidgets>
#include <QtSerialPort>

class MyController : public QObject {
Q_OBJECT
public:
enum IoStatus { Closed, NotReady, Ok, ChecksumError, PortError, Invalid = -1 };
private:
QSerialPort m_port{this};
QDataStream m_str{&m_port};
IoStatus m_ioStatus = Invalid;
uint8_t m_address, m_status;
uint32_t m_value;

bool check(const QByteArray &packet) {
char checksum = 0;
for (int i = 0; i < packet.size()-1; ++i)
checksum += packet [i];
return checksum == packet[packet.size()-1];
}
void onError(QSerialPort::SerialPortError err) {
if (err != QSerialPort::NoError)
setIoStatus(PortError);
}
void onRxData() {
if (m_port.bytesAvailable() < 9) return;
if (m_port.error() != QSerialPort::NoError)
return;
if (! check(m_port.peek(9)))
return setIoStatus(ChecksumError);
uint8_t dummy;
m_str >> m_address >> dummy >> m_status >> dummy >> m_value;
setIoStatus(Ok);
}
void setIoStatus(IoStatus ioStatus) {
if (m_ioStatus == ioStatus) return;
m_ioStatus = ioStatus;
emit ioStatusChanged(m_ioStatus);
}
static QString text(IoStatus ioStatus) {
switch (ioStatus) {
case NotReady: return "Not Ready";
case Ok: return "Ok";
case ChecksumError: return "Checksum Error";
case PortError: return "Serial Port Error";
default: return "Unknown Status";
}
}
public:
explicit MyController(QObject *parent = nullptr) : QObject(parent) {
connect(&m_port, &QIODevice::readyRead, this, &MyController::onRxData);
connect(&m_port, static_cast<void(QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error), this, &MyController::onError);
m_str.setByteOrder(QDataStream::BigEndian);
QTimer::singleShot(0, this, [this]{ setIoStatus(Closed); });
}
bool open() {
auto rc = m_port.open(QIODevice::ReadWrite);
if (rc) setIoStatus(NotReady);
return rc;
}
IoStatus ioStatus() const { return m_ioStatus; }
QString ioStatusText() const { return text(m_ioStatus); }
Q_SIGNAL void ioStatusChanged(IoStatus);
QSerialPort *port() { return &m_port; }
};

它大致可以按如下方式使用 - 这只是您如何与 Controller 交互的示例:

int main(int argc, char **argv) {
QApplication app{argc, argv};
MyController ctl;

QWidget w;
QFormLayout layout{&w};
QLineEdit port;
QLabel status;
QPushButton open{"Open"};
layout.addRow("Port", &port);
layout.addRow(&status);
layout.addRow(&open);

QObject::connect(&open, &QPushButton::clicked, [&]{
ctl.port()->setPortName(port.text());
ctl.open();
});
QObject::connect(&ctl, &MyController::ioStatusChanged, [&]{
status.setText(ctl.ioStatusText());
});
w.show();
return app.exec();
}
#include "main.moc"

如果您坚持使用旧的 C 代码,可以通过在结构中返回结果来对其进行一点点现代化:

struct Result {
uint8_t address;
uint8_t status;
uint32_t value;
enum Status { Ok, NotReady, ChecksumError, ReadError };
Status ioStatus = Ok;

QString ioStatusText() const {
switch (ioStatus) {
case NotReady: return "Not Ready";
case Ok: return "Ok";
case ChecksumError: return "Checksum Error";
case ReadError: return "Read Error";
default: return "Unknown Status";
}
}
};

Result getResult(HANDLE handle)
{
Result result;

DWORD errors, bytesRead;
COMSTAT comStat;

ClearCommError(handle, &errors, &comStat);
if (comStat.cbInQue < 9) {
result.ioStatus = Result::NotReady;
return result;
}

uint8_t buffer[9];
ReadFile(handle, buffer, 9, &bytesRead, NULL);

if (bytesRead < 9) {
result.ioStatus = Result::ReadError;
return result;
}

uint8_t checksum=0;
for (int i=0; i<8; i++)
checksum += buffer[i];

if (checksum != buffer[8]) {
result.ioStatus = Result::ChecksumError;
return result;
}

result.address = buffer[0];
result.status = buffer[2];
result.value = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
return result;
}

然后你可以这样做:

auto result = getResult(handle);
printf("%s\n", result.ioStatusText().toLocal8Bit().constData());

关于c++ - QT C++ 中的 UCHAR 到 Qstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40754585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com