Skip to content

Commit 7f803c9

Browse files
committed
Refactored serial port logging
1 parent dc43730 commit 7f803c9

File tree

7 files changed

+39
-58
lines changed

7 files changed

+39
-58
lines changed

qt/logger.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ void Logger::setTextEdit(QTextEdit *textEdit)
102102

103103
std::basic_streambuf<char>::int_type Logger::overflow(int_type v)
104104
{
105+
qCritical() << tempBuf;
106+
tempBuf.clear();
107+
105108
return v;
106109
}
107110

108111
std::streamsize Logger::xsputn(const char *p, std::streamsize n)
109112
{
110-
qCritical() << p;
113+
tempBuf.append(p);
111114

112115
return n;
113116
}

qt/logger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Logger: public std::basic_streambuf<char>
1616
static Logger *logger;
1717
static int refCount;
1818
std::streambuf *oldBuf;
19+
QString tempBuf;
1920

2021
explicit Logger();
2122
~Logger();

qt/programmer.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ Programmer::~Programmer()
4040
int Programmer::serialPortConnect()
4141
{
4242
if (!serialPort.start(usbDevName.toLatin1(), SERIAL_PORT_SPEED))
43-
{
44-
std::string err = serialPort.errorString();
45-
QString errStr = QString(err.c_str());
46-
qCritical() << "Failed to open serial port " << usbDevName << ": " <<
47-
errStr;
4843
return -1;
49-
}
5044

5145
return 0;
5246
}

qt/reader.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ int Reader::write(const uint8_t *data, uint32_t len)
3939

4040
ret = serialPort->write(reinterpret_cast<const char *>(data), len);
4141
if (!ret)
42-
{
43-
logErr(QString("Failed to write: %1").arg(serialPort->errorString()
44-
.c_str()));
4542
return -1;
46-
}
4743
else if (ret < len)
4844
{
4945
logErr(QString("Data was partialy written, returned %1, expected %2")
@@ -210,12 +206,8 @@ int Reader::handlePackets(char *pbuf, uint32_t len)
210206

211207
void Reader::readCb(int size)
212208
{
213-
214209
if (size < 0)
215210
{
216-
std::string err = serialPort->errorString();
217-
QString errStr = QString(err.c_str());
218-
logErr("Failed to read data: " + errStr);
219211
emit result(-1);
220212
serialPortDestroy();
221213
return;
@@ -271,12 +263,7 @@ int Reader::serialPortCreate()
271263
serialPort = new SerialPort();
272264

273265
if (!serialPort->start(portName.toLatin1(), baudRate))
274-
{
275-
std::string err = serialPort->errorString();
276-
QString errStr = QString(err.c_str());
277-
logErr(QString("Failed to open serial port: %1").arg(errStr));
278266
return -1;
279-
}
280267

281268
return 0;
282269
}
@@ -289,9 +276,6 @@ void Reader::serialPortDestroy()
289276

290277
void Reader::start()
291278
{
292-
/* Required for logger */
293-
qRegisterMetaType<QtMsgType>();
294-
295279
if (serialPortCreate())
296280
{
297281
emit result(-1);

qt/serial_port.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,62 @@ SerialPort::~SerialPort()
1818
int SerialPort::write(const char *buf, int size)
1919
{
2020
int ret;
21+
boost::system::error_code ec;
2122

2223
if (!port)
24+
{
25+
std::cerr << "Port is not opened" << std::endl;
2326
return -1;
27+
}
2428

2529
if (!size)
2630
return 0;
2731

2832
if (!(ret = port->write_some(boost::asio::buffer(buf, size), ec)))
33+
{
34+
std::cerr << "Write error: " << ec.message() << std::endl;
2935
return -1;
36+
}
3037

3138
return ret;
3239
}
3340

3441
int SerialPort::read(char *buf, int size)
3542
{
3643
int ret;
44+
boost::system::error_code ec;
3745

38-
if (!port || !port.get() || !port->is_open())
46+
if (!port || !port->is_open())
47+
{
48+
std::cerr << "Port is not opened" << std::endl;
3949
return -1;
50+
}
4051

4152
if (!(ret = port->read_some(boost::asio::buffer(buf, size), ec)))
53+
{
54+
std::cerr << "Read error: " << ec.message() << std::endl;
4255
return -1;
56+
}
4357

4458
return ret;
4559
}
4660

4761
int SerialPort::asyncRead(char *buf, int size, std::function<void(int)> cb)
4862
{
49-
if (!port || !port.get() || !port->is_open())
63+
if (!port || !port->is_open())
64+
{
65+
std::cerr << "Port is not opened" << std::endl;
5066
return -1;
67+
}
5168

5269
readCb = cb;
5370

5471
port->async_read_some(boost::asio::buffer(buf, size),
5572
boost::bind(&SerialPort::onRead, this, boost::asio::placeholders::error,
5673
boost::asio::placeholders::bytes_transferred));
5774

58-
if (!isStarted)
59-
{
60-
boost::thread t(boost::bind(&boost::asio::io_service::run, &ioService));
61-
isStarted = true;
62-
}
75+
thread = thread_ptr(new boost::thread(boost::bind(&boost::asio::
76+
io_service::run, &ioService)));
6377

6478
return 0;
6579
}
@@ -70,8 +84,8 @@ void SerialPort::onRead(const boost::system::error_code &ec, size_t bytesRead)
7084

7185
if (ec)
7286
{
87+
std::cerr << "Read error: " << ec.message() << std::endl;
7388
readCb(-1);
74-
isStarted = false;
7589
return;
7690
}
7791

@@ -81,8 +95,11 @@ void SerialPort::onRead(const boost::system::error_code &ec, size_t bytesRead)
8195
int SerialPort::asyncReadWithTimeout(char *buf, int size,
8296
std::function<void (int)> cb, int timeout)
8397
{
84-
if (!port || !port.get() || !port->is_open())
98+
if (!port || !port->is_open())
99+
{
100+
std::cerr << "Port is not opened" << std::endl;
85101
return -1;
102+
}
86103

87104
readCb = cb;
88105

@@ -96,12 +113,8 @@ int SerialPort::asyncReadWithTimeout(char *buf, int size,
96113
timer->async_wait(boost::bind(&SerialPort::onTimeout, this,
97114
boost::asio::placeholders::error));
98115

99-
if (!isStarted)
100-
{
101-
thread = thread_ptr(new boost::thread(boost::bind(&boost::asio::
102-
io_service::run, &ioService)));
103-
isStarted = true;
104-
}
116+
thread = thread_ptr(new boost::thread(boost::bind(&boost::asio::
117+
io_service::run, &ioService)));
105118

106119
return 0;
107120
}
@@ -116,8 +129,8 @@ void SerialPort::onReadWithTimeout(const boost::system::error_code &ec,
116129

117130
if (ec)
118131
{
132+
std::cerr << "Read error: " << ec.message() << std::endl;
119133
readCb(-1);
120-
isStarted = false;
121134
return;
122135
}
123136

@@ -128,18 +141,12 @@ void SerialPort::onTimeout(const boost::system::error_code &e)
128141
{
129142
if (!e)
130143
{
131-
std::cout << "timeout: " << e.message() << std::endl;
144+
std::cerr << "Read timeout: " << e.message() << std::endl;
132145
port->cancel();
133146
readCb(-1);
134-
isStarted = false;
135147
}
136148
else if (e != boost::asio::error::operation_aborted)
137-
std::cout << "Timer error: " << e.message() << std::endl;
138-
}
139-
140-
std::string SerialPort::errorString()
141-
{
142-
return ec.message();
149+
std::cerr << "Timer setup error: " << e.message() << std::endl;
143150
}
144151

145152
bool SerialPort::start(const char *portName, int baudRate)
@@ -148,16 +155,16 @@ bool SerialPort::start(const char *portName, int baudRate)
148155

149156
if (port)
150157
{
151-
std::cout << "error : port is already opened..." << std::endl;
158+
std::cerr << "Port is already opened" << std::endl;
152159
return false;
153160
}
154161

155162
port = serial_port_ptr(new boost::asio::serial_port(ioService));
156163
port->open(portName, ec);
157164
if (ec)
158165
{
159-
std::cout << "error : port_->open() failed...com_port_name="
160-
<< portName << ", e=" << ec.message().c_str() << std::endl;
166+
std::cerr << "Failed to open " << portName << ": " << ec.message() <<
167+
std::endl;
161168
port = nullptr;
162169
return false;
163170
}
@@ -194,7 +201,6 @@ void SerialPort::stop()
194201

195202
ioService.stop();
196203
ioService.reset();
197-
isStarted = false;
198204

199205
if (thread)
200206
thread = nullptr;

qt/serial_port.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ class SerialPort
2424
boost::asio::io_service ioService;
2525
thread_ptr thread;
2626
serial_port_ptr port;
27-
boost::system::error_code ec;
2827
boost::mutex mutex;
2928
timer_ptr timer;
3029
std::function<void(int)> readCb;
31-
bool isStarted = false;
3230

3331
SerialPort(const SerialPort &p);
3432
SerialPort &operator=(const SerialPort &p);
@@ -50,7 +48,6 @@ class SerialPort
5048
int asyncRead(char *buf, int size, std::function<void(int)> cb);
5149
int asyncReadWithTimeout(char *buf, int size, std::function<void (int)> cb,
5250
int timeout);
53-
std::string errorString();
5451
};
5552

5653
#endif // SERIAL_PORT_H

qt/writer.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ int Writer::write(uint8_t *data, uint32_t dataLen)
3939

4040
ret = serialPort->write(reinterpret_cast<const char *>(data), dataLen);
4141
if (ret < 0)
42-
{
43-
logErr(QString("Failed to write: %1").arg(serialPort->errorString()
44-
.c_str()));
4542
return -1;
46-
}
4743
else if (static_cast<uint32_t>(ret) < dataLen)
4844
{
4945
logErr(QString("Data was partialy written, returned %1, expected %2")

0 commit comments

Comments
 (0)