hello,
I have determined the data coming from the serial port as "-----1[abcdefghijklmno] " with such data as "------2[abcdefgh hijklmno] "
"1[" start character "]" end character.
I want to take the part of this data starting with "1[" and send it to serial-1.
I also need to send the part starting with "2[" to Serial-2.
It didn't work somehow.
I've edited your code so that it compiles, and added some debug Serial.println()s to see what's going on. I've not fixed the final bug, which is looking for "1[" or "2[" in message after you already stripped them off in receive_message()
Ran the code on an Uno R3. Serial monitor line ending is set to Newline. The serial monitor output for typing (without the quotes) "1[blah" and then "2[blah]" is as follows:
line 20 rx_char = 1
line 20 rx_char = [
line 27 rx_char = b
line 43 rx_char = l
line 43 rx_char = a
line 43 rx_char = h
line 43 rx_char =
receive: blah
Message received at line 79 = blah
line 98 neither strncmp returned 0. message = "blah"
receive:
line 20 rx_char = 2
line 20 rx_char = [
line 27 rx_char = b
line 43 rx_char = l
line 43 rx_char = a
line 43 rx_char = h
line 43 rx_char =
receive: blah
Message received at line 79 = blah
line 98 neither strncmp returned 0. message = "blah"
receive:
I don't have any more time to look at this today. I might take another look tomorrow. In the mean time someone else might be able to help.
[Edit, what do you mean by "strange 6 characters"? I just saw 6 dashes in your post.]
When sending data from the device to serial 1, I can put a start character in front of the data I send and a stop character at the end.
I found "1[" and "2[" suitable as starts.
I want to print the ones starting with 1 to serial1 and the ones starting with 2 to serial2. on mega. But it doesn't work.
Why not have the message configured to be [1abcdefghijklmno] or [2abcdefghijklmno]. After the complete message is received, test for the first character, and if it is a '1' or '2' print the rest of the message to the correct serial port.
When the serial port indicator needs to be inside the message which is received between the start and end chars. The message should look like [1abcdefg] or [2abcdefg].
Then test for the index at message[0].
Try this modification to loop() of your code.
void loop ()
{
receive_message (Serial);
//if (strncmp (message, "1[",2) == 0)
if(message[0] == '1')
{
Serial.println("message sent on Serial1");
#ifdef Mega
send_message (Serial1);
#endif
}
//else if (strncmp (message, "2[",2) == 0)
else if(message[0] == '2')
{
Serial.println("message sent on Serial2");
#ifdef Mega
send_message (Serial2);
#endif
}
}
While such code can be made more optimal, it's not very difficult to write a state machine to decode formal protocols ➜ you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction
this could look like this (sending everything to Serial in this example but target port is identified)
enum {WAIT_FOR_DESTINATION, WAIT_FOR_START_MARKER, SENDING} state = WAIT_FOR_DESTINATION;
const char * validDestinations = "12";
const char startMarker = '[';
const char endMarker = ']';
int destination;
void checkInput() {
if (Serial.available()) {
char r = Serial.read();
switch (state) {
case WAIT_FOR_DESTINATION:
if (strchr(validDestinations, r) != nullptr) { // check if the input is par of the valid destinations
destination = r - '0';
state = WAIT_FOR_START_MARKER;
}
break;
case WAIT_FOR_START_MARKER:
if (r == startMarker) {
Serial.print("Sending to Serial"); Serial.print(destination);
Serial.print("\t{");
state = SENDING;
} else {
state = WAIT_FOR_DESTINATION; // ill formed entry
}
break;
case SENDING:
if (r == endMarker) {
Serial.println("}");
state = WAIT_FOR_DESTINATION; // entry finished
} else {
Serial.write(r);
}
}
}
}
void setup() {
Serial.begin(115200);
Serial.println("READY");
}
void loop() {
checkInput();
}
try typing in
hello1hello[2those are false entries but this one is ok 1[this text will go to Serial 1]more crap2[this text will go to Serial 2]
if all goes well, the invalid stuff is discarded and you should see
Sending to Serial1 {this text will go to Serial 1}
Sending to Serial2 {this text will go to Serial 2}
I think the code is pretty self explanatory but if you have any question, feel free to ask.
Also this is totally non blocking so you deal with the data when it's there and you can do other stuff in the loop (as long as it's non blocking and does not take too long)