ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
gpib.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file gpib.h
6 * @brief GPIB-over-LAN (Prologix-style) controller command codec (PROTOCORE_ENABLE_GPIB) - a zero-heap
7 * codec for the Prologix-compatible `++` command set that drives a bench of legacy IEEE-488
8 * (GPIB) instruments through a Prologix GPIB-Ethernet / GPIB-USB adapter (raw socket on TCP
9 * 1234). The bridge into pre-LAN test gear that will never speak SCPI-over-TCP directly.
10 *
11 * The device is the host: it sends `++` commands (a line starting with an unescaped `++`) to
12 * configure/control the adapter, and data lines (anything else) that the adapter forwards over GPIB
13 * to the addressed instrument. This codec builds the commands (@ref protocore_gpib_command and the typed
14 * @ref protocore_gpib_addr / @ref protocore_gpib_read / @ref protocore_gpib_spoll / @ref protocore_gpib_eos helpers),
15 * builds an escaped data line (@ref protocore_gpib_build_data - a leading ESC before a CR / LF / ESC / `+` byte in the
16 * payload, then an unescaped newline terminator), classifies a line (@ref protocore_gpib_is_command), and parses the
17 * responses (@ref protocore_gpib_parse_decimal for the serial-poll status byte / SRQ / address, @ref
18 * protocore_gpib_parse_version). Pure codec, host-tested; the socket / serial link is the application's.
19 *
20 * Reference: Prologix GPIB-ETHERNET / GPIB-USB Controller manuals (prologix.biz).
21 *
22 * @author Douglas Quigg (dstroy0)
23 * @date 2026
24 */
25
26#ifndef PROTOCORE_GPIB_H
27#define PROTOCORE_GPIB_H
28
29#include "protocore_config.h" // the entry point: protocore_types.h for the widths
30
31#if PROTOCORE_ENABLE_GPIB
32
33EMBED_BEGIN_DECLS
34
35/** @brief The Prologix GPIB-Ethernet raw-socket TCP port. */
36#define PROTOCORE_GPIB_PORT 1234
37/** @brief The Prologix NetFinder UDP discovery port. */
38#define PROTOCORE_GPIB_DISCOVERY_PORT 3040
39
40/** @brief GPIB terminator appended to data sent to the instrument (`++eos`). */
41EMBED_ENUM_BYTE(GpibEos,
42 GPIB_EOS_CRLF = 0, ///< GPIB_EOS_CR + GPIB_EOS_LF
43 GPIB_EOS_CR = 1, ///< GPIB_EOS_CR
44 GPIB_EOS_LF = 2, ///< GPIB_EOS_LF
45 GPIB_EOS_PROTOCORE_NONE = 3, ///< none (use for binary payloads)
46);
47
48/** @brief Read-termination mode (`++read`). */
49EMBED_ENUM_BYTE(GpibRead,
50 UNTIL_TIMEOUT, ///< `++read` - read until the inter-character timeout only (NOT until EOI)
51 UNTIL_EOI, ///< `++read eoi` - read until EOI or timeout
52 UNTIL_CHAR, ///< `++read <char>` - read until the given character or timeout
53);
54
55/**
56 * @brief Build a generic `++` command line: `"++"` + @p cmd + `'\n'` (e.g. `protocore_gpib_command(...,
57 * "mode 1")` -> `"++mode 1\n"`, `"eoi 1"`, `"clr"`, `"ver"`, `"read_tmo_ms 500"`).
58 * @return characters written (excluding the NUL), or 0 on overflow / bad input.
59 */
60size_t protocore_gpib_command(char *buf, size_t cap, const char *cmd);
61
62/**
63 * @brief Build `++addr <pad>[ <sad>]` - set the instrument GPIB address.
64 * @param pad primary address (0-30).
65 * @param sad optional secondary address (96-126); pass < 0 for none.
66 * @return characters written (excluding NUL), or 0 on overflow / bad @p pad.
67 */
68size_t protocore_gpib_addr(char *buf, size_t cap, uint8_t pad, int sad);
69
70/**
71 * @brief Build a `++read` command in one of its three forms.
72 * @param ch the read-until character (decimal), used only when @p mode is @ref UNTIL_CHAR.
73 * @return characters written (excluding NUL), or 0 on overflow.
74 */
75size_t protocore_gpib_read(char *buf, size_t cap, GpibRead mode, uint8_t ch);
76
77/**
78 * @brief Build `++spoll` (serial poll). With @p pad < 0, polls the currently-addressed instrument;
79 * otherwise `++spoll <pad>[ <sad>]`. The response is the status byte as a decimal string.
80 * @return characters written (excluding NUL), or 0 on overflow.
81 */
82size_t protocore_gpib_spoll(char *buf, size_t cap, int pad, int sad);
83
84/** @brief Build `++eos <n>` - the GPIB terminator appended to instrument data. */
85size_t protocore_gpib_eos(char *buf, size_t cap, GpibEos eos);
86
87/**
88 * @brief Build an escaped data line to send to the addressed instrument: each CR (13) / LF (10) /
89 * ESC (27) / `+` (43) byte in @p src is preceded by an ESC (27), then an unescaped `'\n'`
90 * line terminator is appended. (Data received FROM instruments is never escaped.)
91 * @return total bytes written (NOT NUL-terminated - the payload may be binary), or 0 on overflow.
92 */
93size_t protocore_gpib_build_data(uint8_t *buf, size_t cap, const uint8_t *src, size_t len);
94
95/** @brief True if @p line is a controller command (starts with an unescaped `++`), else it is data. */
96embed_bool protocore_gpib_is_command(const char *line, size_t len);
97
98/**
99 * @brief Parse a decimal integer response (trims surrounding spaces / CR / LF) - the `++spoll`
100 * status byte, the `++srq` 0/1, or a `++addr` primary address.
101 * @return true on a clean decimal; false otherwise.
102 */
103embed_bool protocore_gpib_parse_decimal(const char *s, size_t len, uint32_t *out);
104
105/**
106 * @brief Parse a `++addr` query response: a primary address, optionally a space + secondary.
107 * @param pad receives the primary address.
108 * @param sad receives the secondary address, or -1 if none was present.
109 * @return true on a clean response; false otherwise.
110 */
111embed_bool protocore_gpib_parse_addr(const char *s, size_t len, uint8_t *pad, int *sad);
112
113/**
114 * @brief Parse a `++ver` response: locate the version token after `"version "`. @p ver points INTO
115 * @p s (trailing CR/LF trimmed from @p ver_len).
116 * @return true if a version token was found; false otherwise.
117 */
118embed_bool protocore_gpib_parse_version(const char *s, size_t len, const char **ver, size_t *ver_len);
119
120EMBED_END_DECLS
121
122#endif // PROTOCORE_ENABLE_GPIB
123
124#endif // PROTOCORE_GPIB_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.