Skip to content

Commit 12bea1e

Browse files
committed
Make header-only
1 parent 1c83301 commit 12bea1e

File tree

2 files changed

+60
-62
lines changed

2 files changed

+60
-62
lines changed

arduinoSerial.h

-62
This file was deleted.

arduinoSerial.cpp renamed to arduinoSerial.hpp

+60
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,66 @@
1111
#include <algorithm>
1212
#include <chrono>
1313

14+
/*
15+
* ArduinoSerial
16+
*
17+
* This class enables communication with an Arduino over a serial port.
18+
* It is based on the Arduino Serial class, but is not a direct copy.
19+
* Only works on good operating systems.
20+
* This version implements timeout functionality.
21+
*
22+
* Features which will not be implemented (in this version):
23+
* - AvailableForWrite()
24+
* - Peek()
25+
*
26+
* Notes:
27+
* - The configuration of the serial port may not be correct.
28+
* - Flush is set up to behave as it did prior to arduino 1.0, it removes incoming data.
29+
*/
30+
31+
class arduinoSerial {
32+
private:
33+
int fd; // The file descriptor for the serial port (Usually /dev/ttyACM0)
34+
std::string ttyName; // The name of the serial port (Usually /dev/ttyACM0). This string exists mainly for debugging purposes
35+
bool debug; // If true, debug messages will be printed to the console
36+
// Every single baud rate defined in termios.h or termios-baud.h:
37+
const std::array<unsigned long, 31> acceptableBaudRates = {B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000};
38+
long timeout = 1000; // The timeout for read operations. This is in milliseconds. Default is 1000ms (1 second)
39+
public:
40+
arduinoSerial(std::string port, bool debug=false);
41+
~arduinoSerial();
42+
unsigned int available();
43+
//unsigned int availableForWrite();
44+
void begin(unsigned long baudRate);
45+
void end();
46+
bool find(char target);
47+
bool find(std::string targetStr);
48+
bool findUntil(char target, char terminator);
49+
bool findUntil(std::string targetStr, char terminator);
50+
void flush();
51+
float parseFloat();
52+
long parseInt();
53+
//int peek();
54+
void print(std::string str);
55+
void print(char c);
56+
void print(int num);
57+
void print(float num);
58+
void println(std::string str);
59+
void println(char c);
60+
void println(int num);
61+
void println(float num);
62+
int read_s(); // read_s() is used instead of read() because read() is already taken by the C library. >:(
63+
size_t readBytes(char *buffer, size_t length);
64+
size_t readBytesUntil(char terminator, char *buffer, size_t length);
65+
std::string readString();
66+
std::string readStringUntil(char terminator);
67+
void setTimeout(unsigned long timeout);
68+
size_t write_s(char byte); // Named like read_s for the same reason
69+
size_t write_s(char *buffer, size_t size);
70+
};
71+
72+
73+
1474
#define BUFFERSIZE 1024 // Used by functions that are not provided a buffer by the user (e.g. readString()). This could be as small as 1, but i dont think a single kb is too much to ask for.
1575

1676
// Constructor

0 commit comments

Comments
 (0)