1
+ #include < string>
2
+ #include < vector>
3
+ #include < array>
4
+ #include < termios.h>
5
+
6
+ /*
7
+ * ArduinoSerial
8
+ *
9
+ * This class enables communication with an Arduino over a serial port.
10
+ * It is based on the Arduino Serial class, but is not a direct copy.
11
+ * Only works on good operating systems.
12
+ * This version implements timeout functionality.
13
+ *
14
+ * Features which will not be implemented (in this version):
15
+ * - AvailableForWrite()
16
+ * - Peek()
17
+ *
18
+ * Notes:
19
+ * - The configuration of the serial port may not be correct.
20
+ * - Flush is set up to behave as it did prior to arduino 1.0, it removes incoming data.
21
+ */
22
+
23
+
24
+ class arduinoSerial {
25
+ private:
26
+ int fd; // The file descriptor for the serial port (Usually /dev/ttyACM0)
27
+ std::string ttyName; // The name of the serial port (Usually /dev/ttyACM0). This string exists mainly for debugging purposes
28
+ bool debug; // If true, debug messages will be printed to the console
29
+ // Every single baud rate defined in termios.h or termios-baud.h:
30
+ 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};
31
+ unsigned long timeout = 1000 ; // The timeout for read operations. This is in milliseconds. Default is 1000ms (1 second)
32
+ public:
33
+ arduinoSerial (std::string port, bool debug=false );
34
+ ~arduinoSerial ();
35
+ unsigned int available ();
36
+ // unsigned int availableForWrite();
37
+ void begin (unsigned long baudRate);
38
+ void end ();
39
+ bool find (char target);
40
+ bool find (std::string targetStr);
41
+ bool findUntil (char target, char terminator);
42
+ bool findUntil (std::string targetStr, char terminator);
43
+ void flush ();
44
+ float parseFloat ();
45
+ long parseInt ();
46
+ // int peek();
47
+ void print (std::string str);
48
+ void print (char c);
49
+ void print (int num);
50
+ void print (float num);
51
+ void println (std::string str);
52
+ void println (char c);
53
+ void println (int num);
54
+ void println (float num);
55
+ int read_s (); // read_s() is used instead of read() because read() is already taken by the C library. >:(
56
+ size_t readBytes (char *buffer, size_t length);
57
+ size_t readBytesUntil (char terminator, char *buffer, size_t length);
58
+ std::string readString ();
59
+ std::string readStringUntil (char terminator);
60
+ void setTimeout (unsigned long timeout);
61
+ size_t write_s (char byte); // Named like read_s for the same reason
62
+ size_t write_s (char *buffer, size_t size);
63
+ };
0 commit comments