#include <queue>
std::queue<String> gcodeQueue;
bool waitingForOk = false;
void loop()
{
webSocket.loop();
// Check for "ok" response on Serial2
while (Serial2.available())
{
String response = Serial2.readStringUntil('\n');
response.trim();
if (response == "ok")
{
Serial.println("Received OK");
waitingForOk = false;
}
else if (response == "[Pgm End]")
{
Serial.println("Program End");
webSocket.broadcastTXT("Program End");
}
}
sendNextGCodeCommand();
}
void changePenPos(int pos)
{
if (pos == 0)
{
Serial.println("Changed Pen Position to Down");
}
else if (pos == 1)
{
Serial.println("Changed Pen Position to Up");
}
}
void ReceivedGCodeData(uint8_t *payload, size_t length)
{
// Tokenize the payload into lines
char *token;
char *saveptr;
token = strtok_r((char *)payload, "\n", &saveptr);
while (token != NULL)
{
if (strcmp(token, "M7") == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strcmp(token, "M8") == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strcmp(token, "M2") == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strncmp(token, "G1", 2) == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strncmp(token, "G0", 2) == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strcmp(token, "G21") == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strcmp(token, "G90") == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
else if (strcmp(token, "F1000") == 0)
{
// Add each line to the queue
gcodeQueue.push(String(token));
}
token = strtok_r(NULL, "\n", &saveptr);
}
}
void sendNextGCodeCommand()
{
if (!waitingForOk && !gcodeQueue.empty())
{
int packetSize = 2; // Number of lines to send at once
for (int i = 0; i < packetSize; i++)
{
if (!gcodeQueue.empty())
{
String command = gcodeQueue.front();
gcodeQueue.pop();
Serial.println("Sending GCode: " + command);
Serial2.println(command);
}
else
{
break;
}
}
waitingForOk = true;
delay(200);
}
}
Habe jetzt schonmal etwas versucht kann man das optimieren?
Also so funktioniert das schonmal ohne Datenverlust