TR-mbed 1.0
Loading...
Searching...
No Matches
SerialCommunication.h
Go to the documentation of this file.
1#ifndef SerialCommunication_hpp
2#define SerialCommunication_hpp
3#include "mbed.h"
4#include <cctype>
5#include <ratio>
6
7
8class SerialCommunication : BufferedSerial {
9 private:
10 char comdata[6];
11 int index = 0;
12
13 public:
20 SerialCommunication(PinName TX, PinName RX, int baud) : BufferedSerial(TX, RX, baud) {
21
22 }
23
29 bool PCRead(char message[]) {
30 if (comdata[0] == '\\') { // Clear previous message if backslash was hit
31 int i = 0;
32 while(message[i] != '\0')
33 message[i++] = '\0';
34 }
35
36 if (update(comdata, 6, 1)) {
37 int z = 0;
38 if (comdata[0] == '\\') { // Return true once backslash is hit
39 index = 0;
40 printf("\n");
41 return true;
42 } else {
43 while (comdata[z] != '\0') {
44 printf("%s", comdata);
45 fflush(stdout); // Immediately print out what it receieved
46 message[index] = comdata[z];
47 z++;
48 index++;
49 }
50 }
51 }
52 return false;
53 }
54
62 bool update(char message[], int sizeOfMessage, int sleep_fordelay) {
63 if (readable()) {
64 for (int i = 0; i < sizeOfMessage; i++)
65 message[i] = 0;
66
67 ThisThread::sleep_for(std::chrono::milliseconds(sleep_fordelay));
68 read(message, sizeOfMessage);
69 return 1;
70 }
71 return 0;
72 }
73
79 int toNum(char message[]) {
80 int i = 0;
81 if (message[i] == '-')
82 i++;
83 while(message[i] != '\0') {
84 if (!isdigit(message[i]))
85 return -999; // Cannot use NULL because 0 is a number that we may want to return
86 i++;
87 }
88 return std::atoi(message);
89 }
90
91 bool strCompare(char word1[], char word2[]) {
92 int i = 0;
93 while (word1[i++] != '\0') {
94 if (word1[i] != word2[i])
95 return false;
96 }
97 return true;
98 }
99
100};
101#endif
int i
Definition BiCGSTAB_step_by_step.cpp:9
Definition SerialCommunication.h:8
int toNum(char message[])
Check whether message is a number. Works with negatives as well.
Definition SerialCommunication.h:79
bool update(char message[], int sizeOfMessage, int sleep_fordelay)
Check for new data into Serial.
Definition SerialCommunication.h:62
bool PCRead(char message[])
Check for new input into Serial monitor.
Definition SerialCommunication.h:29
SerialCommunication(PinName TX, PinName RX, int baud)
Construct a new Serial Communication Object.
Definition SerialCommunication.h:20
bool strCompare(char word1[], char word2[])
Definition SerialCommunication.h:91