Serial In Hex Data Issues

Hi All,

I have used heaps of the Atmega 2560 based boards and am now playing around with a Ardbox 20IO relay which is powered using a Leonardo. This is connected to a Nextion display which every 1 second outputs the following serial data to the leonardo:

23 02 54 00
23 02 54 01

I have confirmed the Nextion is working correctly as I have connected my oscilloscope to the RX pin on the Leonardo (directly to it, not through the Ardbox ports) and I am seeing the correct data interpretation on the Oscilloscope.

However, when I run the code below, I am getting some different data from the serial monitor connected to the Leonardo.

#include <Arduino.h>
#include "EasyNextionLibrary.h"
#include <SoftwareSerial.h>

// put function declarations here:

#define SerialMon Serial
EasyNex myNex(Serial1);

// Pinout definitions:
int sensorPin = 23;       // IO 9
int doorSolenoid = 6;     // R3
int doorLockedRelay = 12;  // IO 1
int DoorLocked = 0;
float SensorRaw = 0;

bool doorSolenoidState = LOW;

void setup() {
  myNex.begin(115200);
    delay(10);
    SerialMon.begin(115200);
    delay(10);

    // Serial timeout for up to 5 seconds
    unsigned long startMillis = millis();
    while (!Serial && millis() - startMillis < 5000) {
        // Wait for Serial connection or timeout
    }

    if (!Serial) {
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, HIGH);  // Indicate no Serial connection
    }

    // Check Serial1 availability
    if (!Serial1) {
        SerialMon.println("No HMI Serial");
    } else {
        SerialMon.println("HMI Serial Ready");
    }

    SerialMon.println("[BOOT] >>> Initialization Complete");
    pinMode(sensorPin, INPUT);
    pinMode(doorSolenoid, OUTPUT);
    pinMode(doorLockedRelay, INPUT);
}

void loop() {
    while (Serial1.available() > 0) {
        byte incomingByte = Serial1.read();
        SerialMon.print("Received Byte: 0x");
        SerialMon.println(incomingByte, HEX);
    }
}

The serial monitor is giving me:

Received Byte: 0x6E
Received Byte: 0x3F
Received Byte: 0x15
Received Byte: 0x0
Received Byte: 0x6E
Received Byte: 0x3F
Received Byte: 0x15
Received Byte: 0x7F

I am expecting to see:
Received Byte: 0x23
Received Byte: 0x2
Received Byte: 0x54
Received Byte: 0x0
Received Byte: 0x23
Received Byte: 0x2
Received Byte: 0x54
Received Byte: 0x1

Using the Oscope I have verified that the Nextion is sending at a baud of 115200. For completeness, I can confirm that both the Leonardo and Nextion have a common GND.

What could be causing this issue?

Look for loose ground or ground loop (multiple paths to ground) causing 1 bits to be 0 or 0 bits to be 1. Send a series the same value (0000 0001) to see how it changes.

There does not seem to be a pattern.

Some look inverted:
0x7f .. 0x01 >> 0111 1111 .. 0000 0001

Some look LSB/MSB swapped:
0x15 .. 0x54 >> 0001 0101... 0101 0100

Some look inverted and bit shifted.

None look like words are shifted.
Send walking 1s... walking 0s....

0000 0001, 0000 0010, 0000 0100...
1111 1110, 1111 1101, 1111 1011...

You mean that the Ardbox has a Leonardo internally?

Is that supposed to be ASCII or straight binary? Is the "space" really there, or did you add it for clarity? If the space is included, then it must be ASCII. Trouble is, you're printing as if it were binary. Still doesn't explain your strange output, but you'll have to deal with it once other problem is fixed.

That's right, the Ardbox is designed by industrial shields and is essentially a cape for the Arduino Leonardo.

The space is there and should be as HEX. When decoding on the scope it is reading it as HEX correctly

Thanks, I will look for this. It would make sense that it is a ground issue. I'll test the board more thoroughly

HEX (really binary) or ASCII representation of HEX?

HEX representation is something a huge number of newbies get wrapped around the axle over. All numbers in a processor are binary. HEX, DECIMAL, OCTAL are just human-readable representations of those numbers.

@newone96
Do you see the same issue if you slow down the Serial1 baud rate?

The Nextion output format for a printh command associated with a Nextion event is the ascii representation of the HEX byte.

From the Nextion Instruction Set

printh
Send raw byte or multiple raw bytes over Serial to MCU

  • printh is one of the few commands that parameter uses space char 0x20
  • when more than one byte is being sent a space separates each byte
  • byte is represented by 2 of (ASCII char of hexadecimal value per nibble)
  • qty may be limited by serial buffer (all data < 1024)
  • print/printh does not use Nextion Return Data, user must handle MCU side
    usage: printh [<hexhex][...<hexhex]|
    | --- | --- |
    | is hexadecimal value of each nibble. 0x34 as 34
    is a space char 0x20, used to separate each pair|
    |printh 0d // send single byte: value 13 hex: 0x0d
    printh 0d 0a // send two bytes: value 13,10 hex: 0x0d0x0a|

Hi @gfvalvo, I think you are right about me getting it muddled up, however I am still not clear on it exactly. All I can say with confidence is that on my Rigol scope, when I set the decode to ASCI it outputs garbage and when I set it to HEX it matches exactly what I have programmed the Nextion to output. Does this make sense?

Hi @cattledog , thanks for your input, I should have added that info in from the Nextion documentation so thank you for adding it.

RE slowing the serial down, I have tried setting both Nextion and Leonardo to 9600 and still getting the same problem.

That is good if "same" means bitwise. Keep eliminating parts of the equation to find the minimum needed to reproduce the error.

What pin is this?

This line should be removed.

#include <SoftwareSerial.h>

Not really as it contradicts the information in @cattledog's Post #9.

I also wonder how you're seeing the "space" between digit groups from your original post if the scope's decoder isn't set to ASCII. Post a screen shot from the scope.

Can you please provide a specific link to the device you are using. When I surf around looking at the different models and the one based on the Leonardo, it's not clear to me that there are indeed input pins for Serial1. I also see reference to some switch setting for different communication protocols.

Thanks @cattledog, it is as per the attached. I have made the switch and jumper changes documented in here.

Regarding Serial 1 pins, I have opened the Ardbox up and have connected the scope probe directly to the leonardo RX pin which is how I am verifying that the data is getting to the correct place in the right format and baud.

Industrial Shields PLC.pdf (2.4 MB)

If you run that wire to another Leonardo (or another board with Serial1 like one of the Megas you used with a Nextion previously) outside of the Ardbox is the correct trigger code being seen by that Arduino?

If the correct Hex trigger code is present at the Serial1 Rx then there must be something peculiar or defective about the Leonardo or the Ardbox environment.