1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSerialPort module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "slavethread.h" |
52 | |
53 | #include <QSerialPort> |
54 | #include <QTime> |
55 | |
56 | SlaveThread::SlaveThread(QObject *parent) : |
57 | QThread(parent) |
58 | { |
59 | } |
60 | |
61 | //! [0] |
62 | SlaveThread::~SlaveThread() |
63 | { |
64 | m_mutex.lock(); |
65 | m_quit = true; |
66 | m_mutex.unlock(); |
67 | wait(); |
68 | } |
69 | //! [0] |
70 | |
71 | //! [1] //! [2] |
72 | void SlaveThread::startSlave(const QString &portName, int waitTimeout, const QString &response) |
73 | { |
74 | //! [1] |
75 | const QMutexLocker locker(&m_mutex); |
76 | m_portName = portName; |
77 | m_waitTimeout = waitTimeout; |
78 | m_response = response; |
79 | //! [3] |
80 | if (!isRunning()) |
81 | start(); |
82 | } |
83 | //! [2] //! [3] |
84 | |
85 | //! [4] |
86 | void SlaveThread::run() |
87 | { |
88 | bool currentPortNameChanged = false; |
89 | |
90 | m_mutex.lock(); |
91 | //! [4] //! [5] |
92 | QString currentPortName; |
93 | if (currentPortName != m_portName) { |
94 | currentPortName = m_portName; |
95 | currentPortNameChanged = true; |
96 | } |
97 | |
98 | int currentWaitTimeout = m_waitTimeout; |
99 | QString currentRespone = m_response; |
100 | m_mutex.unlock(); |
101 | //! [5] //! [6] |
102 | QSerialPort serial; |
103 | |
104 | while (!m_quit) { |
105 | //![6] //! [7] |
106 | if (currentPortNameChanged) { |
107 | serial.close(); |
108 | serial.setPortName(currentPortName); |
109 | |
110 | if (!serial.open(mode: QIODevice::ReadWrite)) { |
111 | emit error(s: tr(s: "Can't open %1, error code %2" ) |
112 | .arg(a: m_portName).arg(a: serial.error())); |
113 | return; |
114 | } |
115 | } |
116 | |
117 | if (serial.waitForReadyRead(msecs: currentWaitTimeout)) { |
118 | //! [7] //! [8] |
119 | // read request |
120 | QByteArray requestData = serial.readAll(); |
121 | while (serial.waitForReadyRead(msecs: 10)) |
122 | requestData += serial.readAll(); |
123 | //! [8] //! [10] |
124 | // write response |
125 | const QByteArray responseData = currentRespone.toUtf8(); |
126 | serial.write(data: responseData); |
127 | if (serial.waitForBytesWritten(msecs: m_waitTimeout)) { |
128 | const QString request = QString::fromUtf8(str: requestData); |
129 | //! [12] |
130 | emit this->request(s: request); |
131 | //! [10] //! [11] //! [12] |
132 | } else { |
133 | emit timeout(s: tr(s: "Wait write response timeout %1" ) |
134 | .arg(a: QTime::currentTime().toString())); |
135 | } |
136 | //! [9] //! [11] |
137 | } else { |
138 | emit timeout(s: tr(s: "Wait read request timeout %1" ) |
139 | .arg(a: QTime::currentTime().toString())); |
140 | } |
141 | //! [9] //! [13] |
142 | m_mutex.lock(); |
143 | if (currentPortName != m_portName) { |
144 | currentPortName = m_portName; |
145 | currentPortNameChanged = true; |
146 | } else { |
147 | currentPortNameChanged = false; |
148 | } |
149 | currentWaitTimeout = m_waitTimeout; |
150 | currentRespone = m_response; |
151 | m_mutex.unlock(); |
152 | } |
153 | //! [13] |
154 | } |
155 | |