Commit 8a4c4a1d authored by Evan Raskob's avatar Evan Raskob

added error checking in serial available function

parent 40a5168a
......@@ -114,7 +114,7 @@ void draw() {
drawIncomingPackets(); // update GUI with data received/sent
handleSerialCommands(); //<>//
handleSerialCommands(); //<>// //<>// //<>//
}
}
......
......@@ -67,34 +67,42 @@ void serialAvailable(Serial serial) {
// read input until we hit stop character (newline) and remove all leading and trailing special characters
// unfortunately, due to threading issues, this crashes the serial port when called
// too quickly!
//String input = trim( serial.readStringUntil(stopChar) );
String serialIn = serial.readString();
// Uncomment this for debugging:
//println("serial received: " + serialIn);
//String input = trim( serial.readStringUntil(stopChar) );
String serialLines[] = split(serialIn, stopChar); // we might have received a few lines of serial so we
// split them by the stop character (newline)
try {
String serialIn = serial.readString();
// for each line, parse it for commands
for (String line : serialLines) {
String serialLines[] = split(serialIn, stopChar); // we might have received a few lines of serial so we
// split them by the stop character (newline)
String input = trim( line ); // trim away space characters and extra newlines etc.
// for each line, parse it for commands
for (String line : serialLines) {
// ex: splits a serial line of "a 324\n" to ["a", "324"]
String[] msgs = split(input, ",");
String input = trim( line ); // trim away space characters and extra newlines etc.
// check that we've got valid data
if (msgs.length > 0 && msgs[0] != "-") {
synchronized(commandsReceived)
{
commandsReceived.add(msgs);
// ex: splits a serial line of "a 324\n" to ["a", "324"]
String[] msgs = split(input, ",");
// check that we've got valid data
if (msgs.length > 0 && msgs[0] != "-") {
synchronized(commandsReceived)
{
commandsReceived.add(msgs);
}
}
}
// another way of doing this:
//ex: "c,2,3,4" --> ["c", "2", "3", "4"]
//String[] msgs = split(trim(input), ',');
// Uncomment this for debugging:
//println("serial received: " + serialIn);
}
catch (Exception e) {
println(e.getMessage());
println(e.getCause());
e.printStackTrace();
}
// another way of doing this:
//ex: "c,2,3,4" --> ["c", "2", "3", "4"]
//String[] msgs = split(trim(input), ',');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment