UART write and read command not working

I am working with the UART protocol to read data from the distance sensor. The data byte consists of data bits, even parity and a stop bit.

image

I am trying to send header (ascii value of "INIT") and i try to read it using logic analyzer and serial terminal but the values are not correct.

the output on Realterm serial capture:

Here is the code

#include <Arduino.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define RX_PIN 0 // GPIO13
#define TX_PIN 1 // GPIO14

double binaryToDouble(uint64_t binaryValue)
{
  // Copy the binary value into a double variable
  double doubleValue;
  memcpy(&doubleValue, &binaryValue, sizeof(double));

  return doubleValue;
}

void sendData(const char *alphabet, uint32_t payloadSize, const uint8_t *payloadData)
{

  // Send the alphabet
  Serial.write((const uint8_t *)alphabet, strlen(alphabet));
//  Serial.print("Received Alphabet: ");
//  Serial.println(alphabet);
// Serial.println("before size ");

  // Send the payload size
  Serial.write((const uint8_t *)&payloadSize, sizeof(uint32_t));
  // Serial.println("After size ");

  // Send the payload data
  Serial.write(payloadData, payloadSize);
}

uint64_t receiveMessage()
{
  char alphabet[5];
  uint32_t receivedPayloadSize;

  // Read the alphabet
  Serial.readBytes(alphabet, 4);
  alphabet[4] = '\0'; // Null-terminate the string

  // Read the payload size
  Serial.readBytes((char *)&receivedPayloadSize, sizeof(uint32_t));
  uint32_t payloadSize = receivedPayloadSize; // Convert back to host byte order

  // Read the payload data
  uint8_t payload[payloadSize];
  // uint64_t payloaddata;
  Serial.readBytes(payload, payloadSize);

  // Process the received message
  Serial.print("Received Alphabet: ");
  Serial.println(alphabet);

  Serial.print("Received Payload Size: ");
  Serial.println(payloadSize);

  Serial.print("Received Payload Data: ");
  for (uint32_t i = 0; i < payloadSize; i++)
  {
    Serial.print(payload[i], HEX);
    Serial.print(" ");
  }
  Serial.println();

  uint64_t payloaddata = 0;
  for (uint32_t i = 0; i < payloadSize; i++)
  {
    payloaddata <<= 8 * i;
    payloaddata |= payload[i]; // OR with the new byte
  }

  return payloaddata;
}

void setup()
{
  //Serial.begin(115200);
  Serial.begin(9600, SERIAL_8E1);
  uint8_t payload[] = {0x01};
  uint32_t payloadSize = sizeof(payload);
  uint64_t receivedata;
  char alphabet[] = "INIT";

  sendData(alphabet, payloadSize, payload);
  delay(5000);

  while (!Serial.available())
  {
    Serial.println("Waiting for data: ");
  }

  // Read and process the acknowledgment message
  receiveMessage();

  // Wait for data message
  while (!Serial.available())
  {
    Serial.println("Waiting for data: ");
  }

  // Read and process the data message
  receiveMessage();
}

void loop()
{
  uint8_t payload[] = {0x01, 0x00, 0x00, 0x00};
  uint32_t payloadSize = sizeof(payload);
  uint64_t receivedata;
  float result;

  sendData("GNFD", payloadSize, payload);
  while (!Serial.available())
  {
    Serial.println("Waiting for data: ");
  }

  // Read and process the acknowledgment message
  receiveMessage();

  // Wait for data message
  while (!Serial.available())
  {
    Serial.println("Waiting for data: ");
  }

  // Read and process the data message
  receivedata = receiveMessage();
  result = binaryToDouble(receivedata);

  // Print the result
  Serial.print("Binary: ");
//  Serial.println(receivedata, BIN);
  Serial.print("Distance: ");
  Serial.println(result, 4);

  // Other loop code
}

Hello,

Show us the code and the circuit as well as details on your arduinos

Which serial port on the board are you using ? Is it Serial or Serial1 ?

Seeing your code and circuit will show which pins you are using which is important

I have update the details.

You are using pins 0 and 1 so you need to use Serial1 rather than Serial in the sketch

I have only initialize them not use them.

Serial.begin(9600, SERIAL_8E1);

Really ?
Your code uses Serial in multiple places

I don't have the complete hardware and i am using esp32 and trying to read serial data in hex using Realterm software to make sure that the serial data send is correct.

I have change serial to serial1 and it shows nothing on Realterm.

The hardware serial for the Nano ESP is Serial0 which uses by default D0 for RX and D1 for TX.
Initialization is Serial0.begin([baudrate]) and the link for additional information is @ https://docs.arduino.cc/tutorials/nano-esp32/cheat-sheet

EDIT I also recommend downloading this image for reference

I have provided all the details.

You are also using Serial.print to communicate with the serial monitor. Use Serial1 instead for the "payload".

  // Send the payload data
  Serial.write(payloadData, payloadSize);

Currently i don't want to print the statements i put the delay after the sendData and just try to read what serial data it send for trouble shooting.

sendData(alphabet, payloadSize, payload);
delay(5000);

or a

This is important since the Arduino team managed to mess up the pin assignments on the nano ESP.

Still you should use one of the 2 other UARTS for connecting to the sensor and use 'Serial' (which is Serial0) just for the debug output. You can assign any usable pin to any of the UARTs.

#define RX_PIN 2 
#define TX_PIN 3 

Serial1.begin(9600, SERIAL_8E1, RX_PIN, TX_PIN);

Serial.begin(115200); // for the debug messages

Also some ESP boards have interference from the USB to TTL adapter on the default RX pin of UART0, i don't know if this is also the case for the nano ESP, but using alternate pin assignments will cover that issue as well.

Post the picture of the ESP32 you are using and the name/picture of the sensor you have connected with ESP32.

I am using esp32-wroom-32D and the sensor is not connected yet. I only want to read what data Serial.write(payloadData, payloadSize); is sending. I am using Readterm for reading the serial data.

so you want to mimic the distance sensor by using Realterm to send and receive commands over Serial (and you know you'll have to move to Serial1 when you receive the real device?)


You posted in the Nano ESP32 category.... Should we understand that you don't have the board yet and don't have the sensor yet and so are messing around with a regular ESP32 for the time being?

1 Like

What exactly do you mean by this and which board have you got selected in the IDE ?

image

is this a wrong category?

I thought it was closely related to my issue.

Are you using this 30-pin ESP32 Dev Board?