-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added easier connection directly to the device serial port instead of…
… using Linux/Windows tools.
- Loading branch information
1 parent
f1eca99
commit fe6fcdc
Showing
3 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* Serial.h | ||
* | ||
* @author 2021 Slavey Karadzhov <slav@attachix.com> | ||
* | ||
* | ||
****/ | ||
|
||
#pragma once | ||
|
||
#ifndef ARCH_HOST | ||
#error "Hosted::Serial can be used only on the Host architecture!" | ||
#endif | ||
|
||
#include <Stream.h> | ||
#include <WString.h> | ||
#include <serialib.h> | ||
|
||
namespace Hosted | ||
{ | ||
class Serial : public Stream | ||
{ | ||
public: | ||
Serial(const String& ttyDevice) : ttyDevice(ttyDevice) | ||
{ | ||
} | ||
|
||
~Serial() | ||
{ | ||
transport.closeDevice(); | ||
} | ||
|
||
/** @brief Initialise the serial port | ||
* @param baud BAUD rate of the serial port (Default: 9600) | ||
*/ | ||
bool begin(uint32_t baud = 9600) | ||
{ | ||
char result = transport.openDevice(ttyDevice.c_str(), baud); | ||
if(result == 1) { | ||
return true; | ||
} | ||
|
||
debug_w("Hosted::Serial:begin error: %d", result); | ||
return false; | ||
} | ||
|
||
size_t write(uint8_t c) override | ||
{ | ||
if(transport.writeChar(c)) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int available() override | ||
{ | ||
return transport.available(); | ||
} | ||
|
||
int read() override | ||
{ | ||
int ch; | ||
int result = transport.readChar(reinterpret_cast<char*>(&ch), 1); | ||
if(result == 1) { | ||
return ch; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
size_t readBytes(char* buffer, size_t length) override | ||
{ | ||
int result = transport.readBytes(buffer, length, 100, 100); | ||
if(result > 0) { | ||
return result; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
size_t write(const uint8_t* buffer, size_t size) | ||
{ | ||
char result = transport.writeBytes(buffer, size); | ||
if(result == 1) { | ||
return size; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int peek() override | ||
{ | ||
return -1; | ||
} | ||
|
||
void flush() override | ||
{ | ||
} | ||
|
||
private: | ||
String ttyDevice; | ||
|
||
serialib transport; | ||
}; | ||
|
||
} // namespace Hosted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters