1 | // Copyright (C) 2017 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "readtask.h" |
5 | |
6 | ReadTask::ReadTask(QTextStream &output, QObject *parent) : |
7 | QObject(parent), |
8 | m_output(output) { } |
9 | |
10 | void ReadTask::setShowTimeStamp(bool showTimeStamp) |
11 | { |
12 | m_showTimeStamp = showTimeStamp; |
13 | } |
14 | |
15 | bool ReadTask::isShowFlags() const |
16 | { |
17 | return m_showFlags; |
18 | } |
19 | |
20 | void ReadTask::setShowFlags(bool showFlags) |
21 | { |
22 | m_showFlags = showFlags; |
23 | } |
24 | |
25 | void ReadTask::handleFrames() { |
26 | auto canDevice = qobject_cast<QCanBusDevice *>(object: QObject::sender()); |
27 | if (canDevice == nullptr) { |
28 | qWarning(msg: "ReadTask::handleFrames: Unknown sender."); |
29 | return; |
30 | } |
31 | |
32 | while (canDevice->framesAvailable()) { |
33 | const QCanBusFrame frame = canDevice->readFrame(); |
34 | |
35 | QString view; |
36 | |
37 | if (m_showTimeStamp) { |
38 | view = QStringLiteral("%1.%2 ") |
39 | .arg(a: frame.timeStamp().seconds(), fieldwidth: 10, base: 10, fillChar: QLatin1Char(' ')) |
40 | .arg(a: frame.timeStamp().microSeconds() / 100, fieldwidth: 4, base: 10, fillChar: QLatin1Char('0')); |
41 | } |
42 | |
43 | if (m_showFlags) { |
44 | QLatin1String flags("- - - "); |
45 | |
46 | if (frame.hasBitrateSwitch()) |
47 | flags[0] = QLatin1Char('B'); |
48 | if (frame.hasErrorStateIndicator()) |
49 | flags[2] = QLatin1Char('E'); |
50 | if (frame.hasLocalEcho()) |
51 | flags[4] = QLatin1Char('L'); |
52 | |
53 | view += flags; |
54 | } |
55 | |
56 | if (frame.frameType() == QCanBusFrame::ErrorFrame) |
57 | view += canDevice->interpretErrorFrame(errorFrame: frame); |
58 | else |
59 | view += frame.toString(); |
60 | |
61 | m_output << view << Qt::endl; |
62 | } |
63 | } |
64 | |
65 | void ReadTask::handleError(QCanBusDevice::CanBusError /*error*/) |
66 | { |
67 | auto canDevice = qobject_cast<QCanBusDevice *>(object: QObject::sender()); |
68 | if (canDevice == nullptr) { |
69 | qWarning(msg: "ReadTask::handleError: Unknown sender."); |
70 | return; |
71 | } |
72 | |
73 | m_output << tr(s: "Read error: '%1'").arg(a: canDevice->errorString()) << Qt::endl; |
74 | } |
75 |