Hi, I am new in coding in arduino and I am currently working on a project. I am having a dilemma on how to manage the output data of serial monitor. I need to call out the data from multiple lists that was received from a mobile app.
Here is my preliminary codes:
#include <Wire.h>
#include <SoftwareSerial.h>
#include <HardwareSerial.h>
SoftwareSerial B(0, 1); // RX, TX of bluetooth
SoftwareSerial mySerial(12, 13); // RX, TX of Thermal Printer
unsigned long startTime;
char incoming_results;
// String results;
void setup()
{
startTime = millis();
Serial.begin(9600);
B.begin(9600);
mySerial.begin(9600);
Wire.begin();
}
void loop()
{
unsigned long elapsedTime = millis() - startTime;
if (Serial.available())
{
incoming_results = Serial.read();
Serial.print(incoming_results);
delay(100);
}
if (elapsedTime >= 20000)
{
mySerial.println(incoming_results);
}
}
Here is the output of the said codes: ["0"]["1", "3", "AGAP"]["2"]["3", "120", "80"]["4", "Normal"]["5", "N", "N", "Y", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N"]["6"]
The Mega has 4 hardware UARTs (Serial interfaces) so you don't need to use SoftwareSerial at all. I really do suggest that you use Serial1, Serial2 or Serial3 to free up pins 0 and 1 for the Serial interface to the Serial monitor
As to your main question, when you say
Do you mean that you need to put the values received into variables, or something else ?
Will the received data always be in the same format ?
What does this mean? Does 'calling out' mean sending data from the Arduino to something else? Where/what to?
What's the longest message you need to receive, how many messages does the Arduino need to retain at any given moment?
You could make a 2-dimensional array of characters and store it.
Or maybe use a list (https://cplusplus.com/reference/list/list/) or a similar C++ container if that's available for your Arduino core.
Without reading your code, I just want to make sure you're not on a wild goose chase.
You don't manage the output data of the serial monitor, generally.
The serial monitor exists to report on what you told the Arduino to do. You can format the data, sure, pass it along to something else, but it's just your readings/debugging/ASCII artwork/whatever.
What I mean "calling out" is making it a variable so I would be able to use it on the latter part of my codes. To be honest, I don't know how long the message will it received but it will be always on the same format.
Do you have any influence on which data element you receive at which time? Do the data elements always receive in the same order? Do they follow a specific format?
I do have the influence to change the data type either string or lists but I want to go with the list since it will just follow the same format. Using string may vary the length of the string it will receive.
You mean that the device also has the ability to send all data in one stream instead of as separate messages?
In principle, this doesn't really make much of a difference. You still have to handle the same kind of parsing challenge.
In the single-stream ('string') scenario, what does a typical string from the device look like?
Note that I'm cautious in adopting your use of 'string'. The word has a particular meaning in a C++ context, and there's even a big difference between 'string' and 'String'. They are specific data types, with a 'string' being an array of characters and a 'String' being an object container offering a set of functions - and some concerns w.r.t. memory management.
You provided a sample of a message. Using that sample please explain how many what type of variables would you you like the data to be in after parsing the message