-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial_Obj.cpp~
More file actions
195 lines (168 loc) · 4.82 KB
/
Serial_Obj.cpp~
File metadata and controls
195 lines (168 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "Arduino.h"
#include "Serial_Obj.h"
/*
Grbl Controller for the Grbl on a arduino uno.
This class handles the communication with the Grbl Controller.
Auth: J. Jansen
Date: 14-03-2015
Version: 0.2
*/
TextFinder finder(Serial3, 2);
const int NUMBER_OF_FIELDS = 3; // 3 comma seperated fields expected
float values[NUMBER_OF_FIELDS]; // array holding values for all the fields
MatchState ms; // Regexp class
char buf [100]; // Buffer to store serial3 input
Serial_Obj::Serial_Obj(int A)
{
int _A = A;
Serial3.begin(115200);
Serial.println ("Serial3 started");
}
/*
Send system commands to Grbl
Results in System command output window
*/
int Serial_Obj::sendsyscom(String syscom )
{
String Charstring = "SCommand_response: ";
Serial3.println(syscom);
while (Serial3.available() ==0) {
};
while (Serial3.available() >0) {
// read the incoming byte:
delay(3);
char incomingByte = Serial3.read();
Charstring += incomingByte;
}
Serial.println(Charstring);
return(10);
}
/********************************************
Send gcodes to Grbl
*********************************************
*/
int Serial_Obj::sendgcode(String gcode )
{
Serial3.println(gcode);
String Charstring = "Sgcode_response_current : ";
String CharstringS = "Sgcode_response_previous: "; // previous status
ms.Target (buf); // set its address
gcode.toCharArray(buf,50); // covert the gcode into the buf array
Serial.println(buf);
unsigned int count = ms.MatchCount ("[MXYZF]");
Serial.println (buf);
Serial.print ("Found ");
Serial.print (count); // 11 in this case
Serial.println (" matches.");
//Serial3.println(gcode);
delay(3);
//Serial.println("na delay response Grbl");
while (Serial3.available() ==0) {
};
//Serial.println("voor response Grbl");
// Check Grbl's response.. if no or an error response printline after 1 second
long Zeit = millis();
finder.find("ok");
// check errors
if((millis()-Zeit)>1000) {Serial.println("timeout response Grbl");
Serial.print("na zeit Grbl : ");Serial.println(millis()-Zeit);
Serial3.println("?");// request status
while (Serial3.available() ==0) {
};
while (Serial3.available() >0) {
// read the incoming byte:
delay(5);
char incomingByte = Serial3.read();
Charstring += incomingByte;
}
//Serial.println(CharstringS);
Serial.println(Charstring);
}
/*
// store previous reading
Serial3.println("?");// request status
while (Serial3.available() ==0) {
};
while (Serial3.available() >0) {
// read the incoming byte:
delay(5);
char incomingByte = Serial3.read();
CharstringS += incomingByte;
}
*/
delay(1); // give Grbl some time before starting a new command
/*
while (Serial3.available() ==0) {
};
while (Serial3.available() >0) {
// read the incoming byte:
delay(5);
char incomingByte = Serial3.read();
Charstring += incomingByte;
}
Serial.println(Charstring);
*/
return(10);
}
/********************************************
receivebyte Grbl
*********************************************
*/
int Serial_Obj::receiveByte(){
//Serial.begin(115200);
while (Serial3.available() > 0) {
// read the incoming byte:
char incomingByte = Serial3.read();
Serial.println(incomingByte);
}
}
/********************************************
real time status Grbl
*********************************************
*/
int Serial_Obj::send_real_time_status_request(String syscom )
{ Serial3.println(syscom);
while (Serial3.available() ==0) {
};
const int NUMBER_OF_FIELDS = 3; // 3 comma seperated fields expected
float values[NUMBER_OF_FIELDS]; // array holding values for all the fields
//Serial.println("net voor de textfinder");
int fieldIndex = 0; // the current field being received
while (Serial3.available() ==0) {
};
finder.find("MPos:");
//Serial.println("net na de textfinder");
while(fieldIndex < NUMBER_OF_FIELDS){
values[fieldIndex++] = finder.getFloat(); }
Serial.print("X Y Z :");
for(fieldIndex=0; fieldIndex < NUMBER_OF_FIELDS; fieldIndex++)
Serial.print(values[fieldIndex]);
Serial.println();
finder.find(">"); // empty the "?" request
return(10);
}
/*
flush the input buffer
Results in information window
*/
int Serial_Obj::flush()
{
Serial3.flush();
Serial.println("flush done");
return(10);
}
/* pass serial interface to Serial3 interface
This is necessary to have one USB connection for both the Grbl controller and Mega.
*/
void Serial_Obj::connectSerial_Serial3(){
// read from port 3, send to port 0:
if (Serial3.available()) {
int inByte = Serial3.read();
Serial.write(inByte);
}
// read from port 0, send to port 3:
if (Serial.available()) {
int inByte = Serial.read();
Serial3.write(inByte);
}
}