Serial Communication.

edited June 2017 in Arduino

Hello, the following Code:

void serialEvent(Serial p) { message = myPort.readStringUntil(124); if (message != null) { int messageLength = message.length(); message = message.substring(0, messageLength - 1); println(message); try { elements = split(message, ":"); println(elements[0]+"????"); println(elements[0].equals("Panel")); if (elements[0].equals("Panel") == true) { panel = int (elements[1]); println("Panel = " + str(panel)); } } catch (Exception e) { } } }

Print out to Console:


`Panel:1 Panel???? true Panel = 1

Panel:2

Panel???? false

`

Why the second time 'elements[0].equals("Panel")' is false?

Sincerly Jörg

Answers

  • The Answer: Except in the first message there are two invisible signs (line break?) at the beginning of the string! This works, except at the beginning:

    message = message.substring(2, messageLength - 1);

  • Answer ✓

    Check the trim function in the reference. It might help.

    Kf

  • Thank you, it works!:

    message = trim(message.substring(0, messageLength - 1));

Sign In or Register to comment.