arduino-emulator
Loading...
Searching...
No Matches
Serial.h
1/*
2 Serial.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21
22#if USE_SERIALLIB
23
24#include "serialib.h"
25#include "api/HardwareSerial.h"
26
27namespace arduino {
28
34class SerialImpl : public HardwareSerial {
35 public:
36 SerialImpl(const char* device = nullptr) { this->device = device; }
37
38 // Physical serial is opt-in: the host cannot safely guess a usable COM
39 // port. Examples can use SerialImpl serial; serial.setDevice("COM5").
40 void setDevice(const char* device) { this->device = device; }
41
42 virtual void begin(unsigned long baudrate) { open(baudrate); }
43
44 virtual void begin(unsigned long baudrate, uint16_t config) {
45 open(baudrate);
46 }
47
48 virtual void end() {
49 is_open = false;
50 serial.closeDevice();
51 }
52
53 virtual int available(void) { return serial.available(); };
54
55 virtual int peek(void) {
56 if (peek_char == -1) {
57 peek_char = read();
58 }
59 return peek_char;
60 }
61
62 virtual int read(void) {
63 int result = -1;
64 if (peek_char != -1) {
65 result = peek_char;
66 peek_char = -1;
67 } else {
68 char c;
69 result = serial.readChar(&c, timeout);
70 }
71 return result;
72 };
73
74 virtual void flush(void) {};
75
76 virtual size_t write(uint8_t c) { return serial.writeChar(c); }
77
78 virtual operator bool() { return is_open; }
79
80 // sets maximum milliseconds to wait for stream data, default is 1 second
81 void setTimeout(unsigned long timeout) {
82 this->timeout = timeout;
83 HardwareSerial::setTimeout(timeout);
84 }
85
86 protected:
87 const char* device;
88 serialib serial;
89 bool is_open = false;
90 int peek_char = -1;
91 long timeout = 1000;
92
93 virtual void open(unsigned long baudrate) {
94 if (device == nullptr) {
95 is_open = false;
96 return;
97 }
98 if (serial.openDevice(device, baudrate) <= 0) {
99 is_open = false;
100 Logger.error("SerialImpl", "could not open", device);
101 return;
102 }
103 is_open = true;
104 }
105};
106
107} // namespace arduino
108
109#endif
Definition DMAPool.h:103
Definition HardwareSerial.h:89
Definition Serial.h:34
This class is used for communication over a serial device.
Definition serialib.h:50
int available()
Return the number of bytes in the received buffer (UNIX only)
Definition serialib.cpp:608
char openDevice(const char *Device, const unsigned int Bauds)
Open the serial port.
Definition serialib.cpp:93
char readChar(char *pByte, const unsigned int timeOut_ms=0)
Wait for a byte from the serial device and return the data read.
Definition serialib.cpp:347
void closeDevice()
Close the connection with the current device.
Definition serialib.cpp:225
char writeChar(char)
Write a char on the current serial port.
Definition serialib.cpp:249
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
Header file of the class serialib. This class is used for communication over a serial device.