54 StdSerialIo(SendMsgFunc send_func,
const std::string& port,
unsigned baud)
57 bool SetOutMsg(
const wxString& msg)
override;
58 void Start()
override;
67 bool OpenComPortPhysical(
const wxString& com_name,
unsigned baud_rate);
68 void CloseComPortPhysical();
69 ssize_t WriteComPortPhysical(
const char* msg);
74 const std::string& port,
76 return std::make_unique<StdSerialIo>(send_msg_func, port, baud);
79void* StdSerialIo::Entry() {
83 if (!OpenComPortPhysical(m_portname, m_baud)) {
84 std::string msg(_(
"NMEA input device open failed: "));
91 m_stats.driver_bus = NavAddr::Bus::N0183;
92 m_stats.driver_iface = m_portname.ToStdString();
99 newdata = m_serial.
read(rdata,
sizeof(rdata));
100 }
catch (std::exception& e) {
101 DEBUG_LOG <<
"Serial read exception: " << e.what();
102 if (10 < retries++) {
106 CloseComPortPhysical();
114 wxMilliSleep(250 * retries);
115 CloseComPortPhysical();
116 if (OpenComPortPhysical(m_portname, m_baud))
118 else if (retries < 10)
123 for (
unsigned i = 0; i < newdata; i++) line_buf.
Put(rdata[i]);
125 auto line = line_buf.
GetLine();
127 std::lock_guard lock(m_stats_mutex);
130 m_send_msg_func(line);
137 bool failed_write = WriteComPortPhysical(qmsg.c_str()) == -1;
139 std::lock_guard lock(m_stats_mutex);
142 if (failed_write && 10 < retries++) {
146 CloseComPortPhysical();
150 CloseComPortPhysical();
156 std::thread t([&] { Entry(); });
161 if (msg.size() < 6 || (msg[0] !=
'$' && msg[0] !=
'!'))
return false;
162 m_out_que.
Put(msg.ToStdString());
167 std::lock_guard lock(m_stats_mutex);
171bool StdSerialIo::OpenComPortPhysical(
const wxString& com_name,
172 unsigned baud_rate) {
174 m_serial.
setPort(com_name.ToStdString());
178 }
catch (std::exception& e) {
179 auto msg = std::string(
"Unhandled Exception while opening serial port: ");
180 m_open_log_filter.
Log(msg + e.what());
183 std::lock_guard lock(m_stats_mutex);
184 m_stats.available = m_serial.
isOpen();
189void StdSerialIo::CloseComPortPhysical() {
192 }
catch (std::exception& e) {
193 MESSAGE_LOG <<
"Unhandled Exception while closing serial port: "
198ssize_t StdSerialIo::WriteComPortPhysical(
const char* msg) {
201 return static_cast<ssize_t
>(m_serial.
write((uint8_t*)msg, strlen(msg)));
202 }
catch (std::exception& e) {
203 DEBUG_LOG <<
"Unhandled Exception while writing to serial port: "
EventVar evt_driver_msg
Notified for messages from drivers.
void Notify() override
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.
Class that provides a portable serial port interface.
size_t read(uint8_t *buffer, size_t size)
Read a given amount of bytes from the serial port into a given buffer.
void setPort(const std::string &port)
Sets the serial port identifier.
size_t write(const uint8_t *data, size_t size)
Write a string to the serial port.
void setBaudrate(uint32_t baudrate)
Sets the baudrate for the serial port.
void close()
Closes the serial port.
void setTimeout(Timeout &timeout)
Sets the timeout for reads and writes using the Timeout struct.
void open()
Opens the serial port as long as the port is set and the port isn't already open.
bool isOpen() const
Gets the open status of the serial port.
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.
unsigned rx_count
Number of bytes received since program start.