50#include "serial/serial.h"
55 StdSerialIo(SendMsgFunc send_func,
const std::string& port,
unsigned baud)
58 bool SetOutMsg(
const wxString& msg)
override;
59 void Start()
override;
63 serial::Serial m_serial;
68 bool OpenComPortPhysical(
const wxString& com_name,
unsigned baud_rate);
69 void CloseComPortPhysical();
70 ssize_t WriteComPortPhysical(
const char* msg);
75 const std::string& port,
77 return std::make_unique<StdSerialIo>(send_msg_func, port, baud);
80void* StdSerialIo::Entry() {
84 if (!OpenComPortPhysical(m_portname, m_baud)) {
85 std::string msg(_(
"NMEA input device open failed: "));
92 m_stats.driver_bus = NavAddr::Bus::N0183;
93 m_stats.driver_iface = m_portname.ToStdString();
98 if (m_serial.isOpen()) {
100 newdata = m_serial.read(rdata,
sizeof(rdata));
101 }
catch (std::exception& e) {
102 DEBUG_LOG <<
"Serial read exception: " << e.what();
103 if (10 < retries++) {
107 CloseComPortPhysical();
115 wxMilliSleep(250 * retries);
116 CloseComPortPhysical();
117 if (OpenComPortPhysical(m_portname, m_baud))
119 else if (retries < 10)
124 for (
unsigned i = 0; i < newdata; i++) line_buf.
Put(rdata[i]);
126 auto line = line_buf.
GetLine();
128 std::lock_guard lock(m_stats_mutex);
131 m_send_msg_func(line);
138 bool failed_write = WriteComPortPhysical(qmsg.c_str()) == -1;
140 std::lock_guard lock(m_stats_mutex);
143 if (failed_write && 10 < retries++) {
147 CloseComPortPhysical();
151 CloseComPortPhysical();
157 std::thread t([&] { Entry(); });
162 if (msg.size() < 6 || (msg[0] !=
'$' && msg[0] !=
'!'))
return false;
163 m_out_que.
Put(msg.ToStdString());
168 std::lock_guard lock(m_stats_mutex);
172bool StdSerialIo::OpenComPortPhysical(
const wxString& com_name,
173 unsigned baud_rate) {
175 m_serial.setPort(com_name.ToStdString());
176 m_serial.setBaudrate(baud_rate);
178 m_serial.setTimeout(250, 250, 0, 250, 0);
179 }
catch (std::exception& e) {
180 auto msg = std::string(
"Unhandled Exception while opening serial port: ");
181 m_open_log_filter.
Log(msg + e.what());
184 std::lock_guard lock(m_stats_mutex);
185 m_stats.available = m_serial.isOpen();
187 return m_serial.isOpen();
190void StdSerialIo::CloseComPortPhysical() {
193 }
catch (std::exception& e) {
194 MESSAGE_LOG <<
"Unhandled Exception while closing serial port: "
199ssize_t StdSerialIo::WriteComPortPhysical(
const char* msg) {
200 if (m_serial.isOpen()) {
202 return static_cast<ssize_t
>(m_serial.write((uint8_t*)msg, strlen(msg)));
203 }
catch (std::exception& e) {
204 DEBUG_LOG <<
"Unhandled Exception while writing to serial port: "
EventVar evt_driver_msg
Notified for messages from drivers.
const void Notify()
Notify all listeners, no data supplied.
Assembles input characters to lines.
void Put(uint8_t ch)
Add a single character.
std::vector< uint8_t > GetLine()
Retrieve a line from buffer, return empty line if none available.
bool HasLine() const
Return true if a line is available to be returned by GetLine().
Synchronized buffer for outbound, line oriented data.
void Put(const std::string line)
Insert line in buffer.
bool Get(std::string &line)
Retrieve a line in buffer.
Nmea0183 serial communications wrapper, possibly running a thread.
static std::unique_ptr< SerialIo > Create(SendMsgFunc send_msg_func, const std::string &port, unsigned baud)
Factory.
virtual void RequestStop() override=0
Request that thread stops operation.
SerialIo implementation based on serial/serial.h.
bool SetOutMsg(const wxString &msg) override
Send a message to remote peer.
DriverStats GetStats() const override
Retrieve updated driver statistics.
void Start() override
Start IO operations including input, possibly in separate thread.
void SignalExit()
Signal that thread has exited.
bool KeepGoing() const
If true continue thread operation, else exit and invoke SignalExit()
virtual void RequestStop()
Request that thread stops operation.
void Log(const std::string &message)
Log a repeated message after interval seconds.
Line-oriented input/output buffers.
Driver registration container, a singleton.
Enhanced logging interface on top of wx/log.h.
Abstract N0183 serial communications interface.
Driver statistics report.
unsigned tx_count
Number of bytes sent since program start.