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.
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.
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.
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?
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.
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.