1+ #ifndef RS485_FILE_HANDLE_H
2+ #define RS485_FILE_HANDLE_H
3+
4+ #include " mbed.h"
5+
6+ /* *
7+ * @file RS485FileHandle.h
8+ * @brief Declares an mbed FileHandle that redirects stdio to the RS485 bus.
9+ * This is used to capture crash logs on Mbed-based boards that support RS485 but don't have other serial output options.
10+ */
11+
12+ /* *
13+ * @brief FileHandle implementation that forwards standard I/O to ArduinoRS485.
14+ * The baud rate is fixed at 115200 bps. The internal logic is tuned to ensure correct RS485 transmission behavior in
15+ * the context of crash log output.
16+ */
17+ class RS485FileHandle : public mbed ::FileHandle {
18+ public:
19+ /* *
20+ * @brief Constructs a FileHandle that targets the global RS485 interface.
21+ */
22+ RS485FileHandle ();
23+
24+ /* *
25+ * @brief Writes a buffer to the RS485 bus.
26+ * When the first write occurs, the RS485 interface is initialised automatically.
27+ *
28+ * @param buffer Data to send.
29+ * @param size Number of bytes to write.
30+ * @return Bytes written on success, or a negative error code.
31+ */
32+ virtual ssize_t write (const void * buffer, size_t size) override ;
33+ /* *
34+ * @brief Reads data from the RS485 bus.
35+ *
36+ * @param buffer Destination buffer.
37+ * @param size Maximum number of bytes to read.
38+ * @return Bytes read on success, or a negative error code.
39+ */
40+ virtual ssize_t read (void * buffer, size_t size) override ;
41+ /* *
42+ * @brief Changes the current position within the stream.
43+ *
44+ * @param offset Byte offset relative to whence.
45+ * @param whence Reference position, defaults to SEEK_SET.
46+ * @return New position on success, or -1 on error.
47+ */
48+ virtual off_t seek (off_t offset, int whence = SEEK_SET) override ;
49+ /* *
50+ * @brief Closes the FileHandle and ends any active transmission.
51+ *
52+ * @return 0 on success, or a negative error code.
53+ */
54+ virtual int close () override ;
55+ /* *
56+ * @brief Reports readiness for the requested events.
57+ *
58+ * @param events Bitmask of poll events (POLLIN, POLLOUT, ...).
59+ * @return Bitmask indicating the ready events.
60+ */
61+ virtual short poll (short events) const override ;
62+ /* *
63+ * @brief Flushes pending data to the RS485 bus.
64+ *
65+ * @return 0 on success, or a negative error code.
66+ */
67+ virtual int sync () override ;
68+ /* *
69+ * @brief Indicates whether the handle represents a TTY-like device.
70+ *
71+ * @return Non-zero when treated as a TTY.
72+ */
73+ virtual int isatty () const ;
74+
75+ private:
76+ bool _isInitialized = false ;
77+ bool _inTransmission = false ;
78+ void begin ();
79+ void begin (int baudRate);
80+ };
81+
82+ /* *
83+ * @brief Global RS485 FileHandle instance used to redirect stdio.
84+ */
85+ extern RS485FileHandle RS485Console;
86+
87+ #endif // RS485_FILE_HANDLE_H
0 commit comments