Instrument Neutral Distributed Interface INDI 2.2.5
mxhd_serial.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2026 Makoto Kasahara All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file LICENSE. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*******************************************************************************/
18#pragma once
19
20#include <cstdint>
21#include <string>
22#include <vector>
23
24// Minimal serial helper for MX-HD protocol:
25// - Most commands are LX200-style ":xxx#" with optional "#"-terminated ASCII response.
26// - Status "@ST#" returns exactly 3 binary bytes (no terminator).
28{
29public:
30 explicit MXHDSerial(int fd);
31
32 // Drop unread bytes in RX buffer to avoid stale-response desync.
33 bool discardInput(std::string &err);
34
35 // Write command bytes as-is (no extra terminators).
36 bool writeRaw(const void *data, size_t len, std::string &err);
37 bool writeString(const std::string &s, std::string &err);
38
39 // Read until '#', return string without '#'. Timeout is in milliseconds.
40 bool readHashTerminated(std::string &out, int timeoutMs, std::string &err);
41
42 // Read exactly N bytes (binary). Timeout is in milliseconds for the whole read.
43 bool readExact(std::vector<uint8_t> &out, size_t n, int timeoutMs, std::string &err);
44
45 // Read a single byte. Timeout is in milliseconds.
46 bool readChar(char &ch, int timeoutMs, std::string &err);
47
48 // Convenience: send command (expects '#'-terminated response)
49 bool queryHash(const std::string &cmd, std::string &response, int timeoutMs, std::string &err);
50
51 // Convenience: send command and read one-char ACK response.
52 bool queryAck(const std::string &cmd, char &ack, int timeoutMs, std::string &err);
53
54 // Convenience: send status command (@ST#) and read 3 bytes.
55 bool queryStatus3(const std::string &cmd, uint8_t &b1, uint8_t &b2, uint8_t &b3, int timeoutMs, std::string &err);
56
57private:
58 int m_fd;
59};
bool queryStatus3(const std::string &cmd, uint8_t &b1, uint8_t &b2, uint8_t &b3, int timeoutMs, std::string &err)
bool discardInput(std::string &err)
bool writeRaw(const void *data, size_t len, std::string &err)
bool queryHash(const std::string &cmd, std::string &response, int timeoutMs, std::string &err)
bool readHashTerminated(std::string &out, int timeoutMs, std::string &err)
bool writeString(const std::string &s, std::string &err)
bool readExact(std::vector< uint8_t > &out, size_t n, int timeoutMs, std::string &err)
bool queryAck(const std::string &cmd, char &ack, int timeoutMs, std::string &err)
bool readChar(char &ch, int timeoutMs, std::string &err)
int fd
uint8_t cmd[4]
Definition pwc-ioctl.h:2