mirror of
https://github.com/Serial-Studio/Serial-Studio.git
synced 2025-01-31 17:42:55 +08:00
Add function that converts 'invalid' RX characters to hex
This commit is contained in:
parent
d8a880c660
commit
415113ea52
@ -31,6 +31,37 @@
|
||||
*/
|
||||
static SerialManager *INSTANCE = nullptr;
|
||||
|
||||
/**
|
||||
* Returns a string that displays unknown characters in hexadecimal format
|
||||
*/
|
||||
static QString SAFE_STRING(const QByteArray &data)
|
||||
{
|
||||
QString hexString;
|
||||
QString string = QString::fromUtf8(data);
|
||||
|
||||
for (int i = 0; i < string.length(); ++i)
|
||||
{
|
||||
auto byte = string.at(i);
|
||||
auto code = byte.unicode();
|
||||
|
||||
if (code >= 0 && code <= 31 && byte != '\n')
|
||||
{
|
||||
auto hex = QString::number(code, 16);
|
||||
if (hex.length() < 2)
|
||||
hex.prepend("0");
|
||||
|
||||
hexString.append(" 0x");
|
||||
hexString.append(hex);
|
||||
hexString.append(" ");
|
||||
}
|
||||
|
||||
else
|
||||
hexString.append(byte);
|
||||
}
|
||||
|
||||
return hexString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a macOS-like message box with the given properties
|
||||
*/
|
||||
@ -423,9 +454,10 @@ void SerialManager::disconnectDevice()
|
||||
auto name = portName();
|
||||
|
||||
// Disconnect signals/slots
|
||||
port()->disconnect(this, SLOT(handleError()));
|
||||
port()->disconnect(this, SLOT(onDataReceived()));
|
||||
port()->disconnect(this, SLOT(disconnectDevice()));
|
||||
port()->disconnect(this,
|
||||
SLOT(handleError(QSerialPort::SerialPortError)));
|
||||
|
||||
// Close & delete serial port handler
|
||||
port()->close();
|
||||
@ -909,7 +941,7 @@ void SerialManager::onDataReceived()
|
||||
|
||||
// Notify user interface
|
||||
emit receivedBytesChanged();
|
||||
emit rx(QString::fromUtf8(data));
|
||||
emit rx(SAFE_STRING(data));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -949,10 +981,11 @@ void SerialManager::handleError(QSerialPort::SerialPortError error)
|
||||
{
|
||||
LOG_INFO() << "Serial port error" << port()->errorString();
|
||||
|
||||
if (error == QSerialPort::ResourceError)
|
||||
if (error != QSerialPort::NoError)
|
||||
{
|
||||
NiceMessageBox(tr("Critical serial port error"), port()->errorString());
|
||||
auto errorStr = port()->errorString();
|
||||
setPort(0);
|
||||
NiceMessageBox(tr("Critical serial port error"), errorStr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,9 +29,9 @@
|
||||
#include <QQmlApplicationEngine>
|
||||
|
||||
#include <Logger.h>
|
||||
#include <FileAppender.h>
|
||||
#include <QSimpleUpdater.h>
|
||||
#include <ConsoleAppender.h>
|
||||
#include <FileAppender.h>
|
||||
|
||||
#include "Group.h"
|
||||
#include "Dataset.h"
|
||||
@ -70,9 +70,12 @@ int main(int argc, char **argv)
|
||||
|
||||
// Configure CuteLogger
|
||||
auto fileAppender = new FileAppender;
|
||||
auto consoleAppender = new ConsoleAppender;
|
||||
fileAppender->setFormat(LOG_FORMAT);
|
||||
fileAppender->setFileName(LOG_FILE);
|
||||
consoleAppender->setFormat(LOG_FORMAT);
|
||||
cuteLogger->registerAppender(fileAppender);
|
||||
cuteLogger->registerAppender(consoleAppender);
|
||||
|
||||
// Begin logging
|
||||
LOG_INFO() << "Running on"
|
||||
|
Loading…
x
Reference in New Issue
Block a user