SafeString  4.1.27
SafeString is a safe, robust and debuggable replacement for string processing in Arduino
SerialComs.h
Go to the documentation of this file.
1 #ifndef SERIAL_COMS_H
2 #define SERIAL_COMS_H
3 /*
4  SerialComs.h a send/receive lines of text between Arduinos via Serial
5  by Matthew Ford
6  (c)2021 Forward Computing and Control Pty. Ltd.
7  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
8  This code may be freely used for both private and commercial use.
9  Provide this copyright is maintained.
10 **/
11 // SafeStringReader.h include defines for Stream
12 #include "SafeStringReader.h"
13 #include "millisDelay.h"
14 
15 
16 // This class uses the SafeString::Output for debug and error messages.
17 // call SafeString::setOutput(Stream..); to set where the debug and error msgs are to be sent to
18 // NOT to the stream pass to the connect( ) :-(
19 
20 // these define allow you to access the SafeString& like they were class variables
21 #define textToSend getTextToSend()
22 #define textReceived getTextReceived()
23 
24 // handle namespace arduino
26 
27 
34 
84 class SerialComs : public SafeString {
85  public:
86  // SerialComs coms(); // creates default coms with send/recieve fo 60char each
87  // this default will tolerate very long delays() on either side with out losing data
88  // SerialComs coms(120,200); // set sendSize to 120 and receiveSize to 200
89  // on the other side you need SerialComs coms(200,120) to match send <-> receive
90  SerialComs(size_t sendSize = 60, size_t receiveSize = 60);
91 
92  // One side (and only one side) must be set as the controller
93  // SoftwareSerial board should be set as the controller
94  void setAsController(); // default no the controller
95 
96  // connect sets the Stream to send to/receive from
97  // connect returns false if there is not enough memory for the linesizes specified in the constructor
98  // you should always check the return.
99  bool connect(Stream &io);
100 
101  // these three methods are all that you need to use in the loop() code.
102 
103  // sendAndReceive() MUST be called every loop.
104  // is sends any data if we can send, receives any data if we are waiting for the other side
105  // each call to sendAndReceive() first clears the textReceived SafeString, so you must process the received text in the loop() it is received.
106  // sendAndReceive() clears the textToSend SafeString once it is sent.
108 
109  // in the loop() you can use
110  // coms.textReceived and coms.textToSend to access the SafeStrings that hold the received text and the text to send.
111  // instead of coms.getTextReceived() and coms.getTextToSend()
112  SafeString& getTextReceived(); // alias textReceived
113  SafeString& getTextToSend(); // alias textToSend
114 
115  bool isConnected();
116 
117  // default is to add checkSum and check incoming checksum
118  // calling noCheckSum() disables both of these.
119  // need to do the same on the other side as well.
120  void noCheckSum();
121 
122  ~SerialComs(); // frees memory
123  private:
124  char *receive_INPUT_BUFFER;
125  char *receiver_TOKEN_BUFFER;
126  char *send_BUFFER;
127  SafeString* textToSendPtr;
128  SafeStringReader* textReceivedPtr;
129  SafeString* receiver_SF_INPUT;
130 
131  void receiveNextMsg();
132  void sendNextMsg();
133  void deleteSerialComs();
134  void clearIO_Available();
135  void checkConnectionTimeout();
136  bool checkCheckSum(SafeString& msg);
137  void calcCheckSum(SafeString& msg, SafeString& chkHexStr);
138  void lostConnection();
139  void setConnected();
140  void resetConnectionTimer();
141 
142  bool connected;
143  bool clearToSendFlag;
144  bool isController;
145  bool notUsingCheckSum;
146 
147  millisDelay connectionTimeout;
148  bool outOfMemory;
149  bool memoryLow;
150  size_t _receiveSize;
151  size_t _sendSize;
152  Stream *stream_io_ptr;
153  unsigned long connectionTimeout_ms;// = 250; // 0.25 sec
154  static char emptyCharArray[0];
155 };
156 
157 #include "SafeStringNameSpaceEnd.h"
158 
159 #endif // SERIAL_COMS_H
To create SafeStrings use one of the four (4) macros createSafeString or cSF, createSafeStringFromCha...
Definition: SafeString.h:307
To create a SafeStringReader use the macro createSafeStringReader see the detailed description.
The SerialComs class sends/recieves lines of text between Arduinos via Serial, see the detailed descr...
Definition: SerialComs.h:84
SafeString & getTextToSend()
void setAsController()
bool connect(Stream &io)
SafeString & getTextReceived()
void noCheckSum()
bool isConnected()
void sendAndReceive()
SerialComs(size_t sendSize=60, size_t receiveSize=60)
millisDelay** implements a non-blocking, repeatable delay, see the detailed description.
Definition: millisDelay.h:35