I need a little guidance with my program.
Currently I am using an Arduino Mega with a HC-06 bluetooth module connected to Serial1.
I receive the data from the HC-06 like so:
while (Serial1.available() > 0) {
raw_serial_data = Serial1.readStringUntil('!');
switch (raw_serial_data.charAt(0)) {
case 'I':
// process data furter
But now I would like to connect a wifi module to Serial2. I will then have two serial devices who will be able to talk to the Arduino. They will NOT be able to talk at the same time, I will take care of this elsewhere.
My problem now is...how can I change the sketch so it will be able to find out which serial port is currently being written to, and how can I then take this data (either from Serial1 or Serial2) and save it to raw_serial_data?
Something like:
while (Serial1.available() > 0 || Serial2.available() > 0) {
if (Serial1.available() > 0) {
raw_serial_data = Serial1.readStringUntil('!');
}
if (Serial2.available() > 0) {
raw_serial_data = Serial2.readStringUntil('!');
}
switch (raw_serial_data.charAt(0)) {
case 'I':
// process data furter
You need a different non-blocking approach to receiving the data. Have a look at the examples in Serial Input Basics.
For example if you are using the second example you can have a copy of the function receiveWithEndMarker() for Serial1 and another copy for Serial2. Where the example causes the variable newData to be true you should have two separate variables newData1 and newData2 and then you can easily tell which port has received the data.
Robin2:
You need a different non-blocking approach to receiving the data. Have a look at the examples in Serial Input Basics.
For example if you are using the second example you can have a copy of the function receiveWithEndMarker() for Serial1 and another copy for Serial2. Where the example causes the variable newData to be true you should have two separate variables newData1 and newData2 and then you can easily tell which port has received the data.
...R
Hmm would there be a more simpler way? Like I said, Serial1 and Serial2 will NOT be sending data at the same time. Either it will be Serial1 OR Serial2 who will be sending data the whole time.
I just need to find a way to select the port which is sending the data.
will that tell you, the data is coming from Serial1?
@Robin's tutorial is a great start to write non blocking serial read. It might seems complex at first but that's the right approach.
I'm sorry. I didnt test the second part I posted, it's getting late here. I was just wondering if something like that could work when keeping in mind that only one device will talk to the Arduino.