I have a Nextion with four buttons (b0-b3) and the buttons write out serially as follows:
b0 = 31 30
b1 = 31 31
b2 = 31 32
b3 = 31 33
These are hex values received via Serial1 on the Micro.
Ok, what I'm having trouble with is figuring out the logic of how to parse the incoming data so the program can "do something" with it.
i.e. if Serial1.available(){
if the second byte received = 30 then execute a
if the second byte received = 31 then execute b
if the second byte received = 32 then execute c
if the second byte received = 33 then execute d
So, how do I scan and read the second byte of an incoming two-byte packet?
Wait until serial.available >= 2. Then read each byte into a separate variable and act on the second. It's probably worth verifying that the first is 31 too.
No, the buffer can contain up to 64 bytes before it overflows. You're expecting a button press to give you two bytes, so the >= 2 is intended to wait until you have two before reading them.
Individual variables is one way to do it and probably easiest at this stage. Later on you may want a more clever parser if you add other stuff to your display.