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 = "/dev/ttyACM0") { this->device = device; }
37
38 virtual void begin(unsigned long baudrate) { open(baudrate); }
39
40 virtual void begin(unsigned long baudrate, uint16_t config) {
41 open(baudrate);
42 }
43
44 virtual void end() {
45 is_open = false;
46 serial.closeDevice();
47 }
48
49 virtual int available(void) { return serial.available(); };
50
51 virtual int peek(void) {
52 if (peek_char == -1) {
53 peek_char = read();
54 }
55 return peek_char;
56 }
57
58 virtual int read(void) {
59 int result = -1;
60 if (peek_char != -1) {
61 result = peek_char;
62 peek_char = -1;
63 } else {
64 char c;
65 result = serial.readChar(&c, timeout);
66 }
67 return result;
68 };
69
70 virtual void flush(void) {};
71
72 virtual size_t write(uint8_t c) { return serial.writeChar(c); }
73
74 virtual operator bool() { return is_open; }
75
76 // sets maximum milliseconds to wait for stream data, default is 1 second
77 void setTimeout(unsigned long timeout) {
78 this->timeout = timeout;
79 HardwareSerial::setTimeout(timeout);
80 }
81
82 protected:
83 const char* device;
84 serialib serial;
85 bool is_open = false;
86 int peek_char = -1;
87 long timeout = 1000;
88
89 virtual void open(unsigned long baudrate) {
90 if (!serial.openDevice(device, baudrate)) {
91 Logger.error("SerialImpl", "could not open", device);
92 }
93 is_open = true;
94 }
95};
96
97} // namespace arduino
98
99#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:600
char openDevice(const char *Device, const unsigned int Bauds)
Open the serial port.
Definition serialib.cpp:92
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:339
void closeDevice()
Close the connection with the current device.
Definition serialib.cpp:217
char writeChar(char)
Write a char on the current serial port.
Definition serialib.cpp:241
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.