Components/Hosted/include/Hosted/Serial.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Serial.h
8  *
9  * @author 2021 Slavey Karadzhov <slav@attachix.com>
10  *
11  *
12  ****/
13 
14 #pragma once
15 
16 #ifndef ARCH_HOST
17 #error "Hosted::Serial can be used only on the Host architecture!"
18 #endif
19 
20 #include <SerialLib.h>
21 #include <Stream.h>
22 #include <WString.h>
23 #include <hostlib/hostmsg.h>
24 
25 namespace Hosted
26 {
27 class Serial : public Stream
28 {
29 public:
30  Serial(const String& ttyDevice) : ttyDevice(ttyDevice)
31  {
32  }
33 
35  {
36  transport.closeDevice();
37  }
38 
39  bool setDtr(bool on)
40  {
41  return transport.DTR(on);
42  }
43 
44  bool setRts(bool on)
45  {
46  return transport.RTS(on);
47  }
48 
52  bool begin(uint32_t baud = 9600)
53  {
54  char result = transport.openDevice(ttyDevice.c_str(), baud);
55  if(result == 1) {
56  return true;
57  }
58 
59  host_debug_w("Hosted::Serial:begin error: %d", result);
60  return false;
61  }
62 
63  size_t write(uint8_t c) override
64  {
65  if(transport.writeChar(c)) {
66  return 1;
67  }
68 
69  return 0;
70  }
71 
72  int available() override
73  {
74  return transport.available();
75  }
76 
77  int read() override
78  {
79  int ch;
80  int result = transport.readChar(reinterpret_cast<char*>(&ch), 1);
81  if(result == 1) {
82  return ch;
83  }
84 
85  return -1;
86  }
87 
88  size_t readBytes(char* buffer, size_t length) override
89  {
90  int result = transport.readBytes(buffer, length, 100, 100);
91  if(result > 0) {
92  return result;
93  }
94 
95  return 0;
96  }
97 
98  size_t write(const uint8_t* buffer, size_t size)
99  {
100  char result = transport.writeBytes(buffer, size);
101  if(result == 1) {
102  return size;
103  }
104 
105  return 0;
106  }
107 
108  int peek() override
109  {
110  return -1;
111  }
112 
113  void flush() override
114  {
115  }
116 
117 private:
118  String ttyDevice;
119  serialib transport;
120 };
121 
122 } // namespace Hosted
Definition: Components/Hosted/include/Hosted/Serial.h:28
Serial(const String &ttyDevice)
Definition: Components/Hosted/include/Hosted/Serial.h:30
~Serial()
Definition: Components/Hosted/include/Hosted/Serial.h:34
int read() override
Definition: Components/Hosted/include/Hosted/Serial.h:77
int available() override
Definition: Components/Hosted/include/Hosted/Serial.h:72
bool setRts(bool on)
Definition: Components/Hosted/include/Hosted/Serial.h:44
void flush() override
Definition: Components/Hosted/include/Hosted/Serial.h:113
bool begin(uint32_t baud=9600)
Initialise the serial port.
Definition: Components/Hosted/include/Hosted/Serial.h:52
size_t write(const uint8_t *buffer, size_t size)
Writes characters from a buffer to output stream.
Definition: Components/Hosted/include/Hosted/Serial.h:98
size_t write(uint8_t c) override
Writes a single character to output stream.
Definition: Components/Hosted/include/Hosted/Serial.h:63
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
Definition: Components/Hosted/include/Hosted/Serial.h:88
int peek() override
Definition: Components/Hosted/include/Hosted/Serial.h:108
bool setDtr(bool on)
Definition: Components/Hosted/include/Hosted/Serial.h:39
Base Stream class.
Definition: Wiring/Stream.h:33
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
#define host_debug_w(fmt,...)
Definition: hostmsg.h:50
Definition: Components/Hosted/include/Hosted/Client.h:30