Skip to content

Commit 282ba9a

Browse files
DenisMartin0gizmocuz
authored andcommitted
Adding CharSize:8bits fallback to TeleinfoSerial (default is 7bits) (#2388)
* TeleinfoSerial will fallback to CSIZE8 if CSIZE7 fail - Fallback to 8bits per char instead of 7bits - Exception catched in ASyncSerial::open to close the port
1 parent 5480aca commit 282ba9a

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

hardware/ASyncSerial.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,19 @@ void AsyncSerial::open(const std::string& devname, unsigned int baud_rate,
9898

9999
setErrorStatus(true);//If an exception is thrown, error_ remains true
100100
pimpl->port.open(devname);
101-
pimpl->port.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
102-
pimpl->port.set_option(opt_parity);
103-
pimpl->port.set_option(opt_csize);
104-
pimpl->port.set_option(opt_flow);
105-
pimpl->port.set_option(opt_stop);
101+
try {
102+
pimpl->port.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
103+
pimpl->port.set_option(opt_parity);
104+
pimpl->port.set_option(opt_csize);
105+
pimpl->port.set_option(opt_flow);
106+
pimpl->port.set_option(opt_stop);
107+
}
108+
catch (...)
109+
{
110+
_log.Log(LOG_ERROR, "ASyncSerial: Error setting options!");
111+
pimpl->port.close();
112+
throw;
113+
}
106114

107115
pimpl->io.reset();
108116

@@ -125,13 +133,17 @@ void AsyncSerial::openOnlyBaud(const std::string& devname, unsigned int baud_rat
125133

126134
setErrorStatus(true);//If an exception is thrown, error_ remains true
127135
pimpl->port.open(devname);
128-
pimpl->port.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
129-
/*
130-
pimpl->port.set_option(opt_parity);
131-
pimpl->port.set_option(opt_csize);
132-
pimpl->port.set_option(opt_flow);
133-
pimpl->port.set_option(opt_stop);
134-
*/
136+
137+
try {
138+
pimpl->port.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
139+
}
140+
catch (...)
141+
{
142+
_log.Log(LOG_ERROR, "ASyncSerial: Error setting options!");
143+
pimpl->port.close();
144+
throw;
145+
}
146+
135147
pimpl->io.reset();
136148

137149
//This gives some work to the io_service before it is started

hardware/TeleinfoSerial.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,26 @@ bool CTeleinfoSerial::StartHardware()
8585
try
8686
{
8787
_log.Log(LOG_STATUS, "(%s) Teleinfo device uses serial port: %s at %i bauds", Name.c_str(), m_szSerialPort.c_str(), m_iBaudRate);
88-
open(
89-
m_szSerialPort,
90-
m_iBaudRate,
91-
m_iOptParity,
92-
m_iOptCsize
93-
);
88+
open(m_szSerialPort, m_iBaudRate, m_iOptParity, m_iOptCsize);
9489
}
9590
catch (boost::exception & e)
9691
{
97-
_log.Log(LOG_ERROR, "Teleinfo: Error opening serial port!");
98-
#ifdef DEBUG_TeleinfoSerial
92+
93+
#ifdef DEBUG_TeleinfoSerial
9994
_log.Log(LOG_ERROR, "-----------------\n%s\n-----------------", boost::diagnostic_information(e).c_str());
100-
#else
95+
#else
10196
(void)e;
102-
#endif
103-
return false;
97+
#endif
98+
_log.Log(LOG_STATUS, "Teleinfo: Serial port open failed, let's retry with CharSize:8 ...");
99+
100+
try {
101+
open(m_szSerialPort,m_iBaudRate,m_iOptParity,boost::asio::serial_port_base::character_size(8));
102+
_log.Log(LOG_STATUS, "Teleinfo: Serial port open successfully with CharSize:8 ...");
103+
}
104+
catch (...) {
105+
_log.Log(LOG_ERROR, "Teleinfo: Error opening serial port, even with CharSize:8 !");
106+
return false;
107+
}
104108
}
105109
catch (...)
106110
{

0 commit comments

Comments
 (0)