[SOLVED] How to check if Serial.write() sent data ?

GeorgeFlorian1
Posts: 123
Joined: Thu Jan 31, 2019 2:32 pm

[SOLVED] How to check if Serial.write() sent data ?

Postby GeorgeFlorian1 » Wed Oct 30, 2019 2:40 pm

Board: ESP32-EVB

I am trying to send some commands to a radar through a serial connection.
Right now I am capable of reading what the radar sends to the ESP through the same serial.

I've received the command and parameter list from the Radar's manufacturer and I have enabled Serial Port Commands from the Radar settings.

A command looks like this: SET detectionDirection 0

As such, I've written in code:

Code: Select all

USE_SERIAL1.flush();
USE_SERIAL1.write("SET detectionDirection 0");
But it doesn't change the settings of the Radar.

I have tried without using USE_SERIAL1.flush(); an also with print instead of write

USE_SERIAL1.availableForWrite() returns 127. SET detectionDirection 0 should be 24 bytes, so there shouldn't any problems here.

How can I check if I actually sent any data at all ?
Last edited by GeorgeFlorian1 on Thu Oct 31, 2019 1:49 pm, edited 1 time in total.

tommeyers
Posts: 108
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: How to check if Serial.write() sent data ?

Postby tommeyers » Wed Oct 30, 2019 6:37 pm

What baud rate, etc?

Also, maybe use a dumb terminal emulator to connect to the device to simplify the system; just to test comm.
IT Professional, Maker
Santiago, Dominican Republic

idahowalker
Posts: 150
Joined: Wed Aug 01, 2018 12:06 pm

Re: How to check if Serial.write() sent data ?

Postby idahowalker » Wed Oct 30, 2019 9:59 pm

You could set up another serial port, Serial2, on your ESP32.

Connect a wire from Serial1tx to Serial2rx. Send some data out Serial1, print Serial2 receive data to Serial, did you get sumpting printed on the serial monitor? If you did serial1 sent sumpting, and Serial2 got it.

tommeyers
Posts: 108
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: How to check if Serial.write() sent data ?

Postby tommeyers » Wed Oct 30, 2019 11:39 pm

Check that write and the double quotes

Double quotes:Single quotes are characters ( char ), double quotes are null-terminated strings ( char * ). Single quotes are for a single character. Double quotes are for a string (array of characters). You can use single quotes to build up a string one character at a time, if you like.

Did you mean to send a null as terminator? do you want to send 'kjhkhkhkhk stuff \n' ??

Tom
IT Professional, Maker
Santiago, Dominican Republic

GeorgeFlorian1
Posts: 123
Joined: Thu Jan 31, 2019 2:32 pm

Re: How to check if Serial.write() sent data ?

Postby GeorgeFlorian1 » Thu Oct 31, 2019 10:34 am

tommeyers wrote:
Wed Oct 30, 2019 6:37 pm
What baud rate, etc?

Also, maybe use a dumb terminal emulator to connect to the device to simplify the system; just to test comm.
Baud rate is 9600. Why does this matter ? I mentioned that I have successfully established a serial connection and that I am able to read whatever data the radar sends.

I don't know how to emulate a serial connection on Linux. And I can't seem to find something relevant.
tommeyers wrote:
Wed Oct 30, 2019 11:39 pm
Check that write and the double quotes

Double quotes:Single quotes are characters ( char ), double quotes are null-terminated strings ( char * ). Single quotes are for a single character. Double quotes are for a string (array of characters). You can use single quotes to build up a string one character at a time, if you like.

Did you mean to send a null as terminator? do you want to send 'kjhkhkhkhk stuff \n' ??

Tom
I really do not know. The only documentation I got about the radar is the command list and parameter list. I have no example of how to send it. I don't know if I can send the command in a whole string or if I need to send it char by char. I don't know if I need to use Serial.print() or Serial.write().
Last edited by GeorgeFlorian1 on Thu Oct 31, 2019 11:02 am, edited 1 time in total.

GeorgeFlorian1
Posts: 123
Joined: Thu Jan 31, 2019 2:32 pm

Re: How to check if Serial.write() sent data ?

Postby GeorgeFlorian1 » Thu Oct 31, 2019 11:02 am

idahowalker wrote:
Wed Oct 30, 2019 9:59 pm
You could set up another serial port, Serial2, on your ESP32.

Connect a wire from Serial1tx to Serial2rx. Send some data out Serial1, print Serial2 receive data to Serial, did you get sumpting printed on the serial monitor? If you did serial1 sent sumpting, and Serial2 got it.
How am I supposed to do that ?

On the Wroom-32 Serial1 pins are RXD = GPIO9 and TXD = GPIO10, and those are Flash pins. Using these pins makes my ESP to restart continuously.

Serial0 pins are GPIO1 and 3 and I have tried this and this prints directly to the Serial Monitor.

Even so, I've tried it.

I have connected TX1 1 to RX2 16.

I have tried this on an ESP32 DevKitV4 Wrover-B.

Code: Select all

#include <Arduino.h>

HardwareSerial USE_SERIAL1(1);
HardwareSerial USE_SERIAL2(2);

#define TX1 1
#define RX1 3

#define TX2 17
#define RX2 16

void setup() {
  Serial.begin(115200);
  delay(1000);
  USE_SERIAL1.begin(9600, SERIAL_8N1, RX1, TX1);
  USE_SERIAL2.begin(9600, SERIAL_8N1, RX2, TX2);

  USE_SERIAL1.write("SET detectionDirection 1");
  String s = "SET detectionDirection 1";
  for(int i=0; i<s.length(); i++) {
    // Serial.println(s[i]);
  }


  // String Radar_String = "";

  // if(USE_SERIAL2.available() > 0) {
  //   // Serial.println("USE_SERIAL1 is available.");
  //   while (USE_SERIAL2.available()) {
  //     char radar = USE_SERIAL1.read();
  //     Radar_String += radar; // this adds up all the input
  //   }
  // }
  // Serial.println(Radar_String);
}

void loop() {
  delay(1000);
}
And this is the output:

Code: Select all

␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀
As you can see USE_SERIAL1 prints directly to Serial.

Code: Select all

  String Radar_String = "";

  if(USE_SERIAL2.available() > 0) {
    // Serial.println("USE_SERIAL1 is available.");
    while (USE_SERIAL2.available()) {
      char radar = USE_SERIAL1.read();
      Radar_String += radar; // this adds up all the input
    }
  }
  Serial.println("read: " + Radar_String);
This bit of code doesn't print a thing.

How do I make it work ? Because I literally have no idea.

tommeyers
Posts: 108
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: How to check if Serial.write() sent data ?

Postby tommeyers » Thu Oct 31, 2019 1:36 pm

1) What is your baud rate in your code and what is the device expecting; must be =;
2) Change from "..." to '...' and send a '\n' too that is the usual indicattion of end of a message but can be \r too or other; experiment.

What is the device you are connecting to? Brand, model

Later your receive may need some work; one thing at a time.

Tom
IT Professional, Maker
Santiago, Dominican Republic

GeorgeFlorian1
Posts: 123
Joined: Thu Jan 31, 2019 2:32 pm

Re: How to check if Serial.write() sent data ?

Postby GeorgeFlorian1 » Thu Oct 31, 2019 1:48 pm

tommeyers wrote:
Thu Oct 31, 2019 1:36 pm
1) What is your baud rate in your code and what is the device expecting; must be =;
2) Change from "..." to '...' and send a '\n' too that is the usual indication of end of a message but can be \r too or other; experiment.

What is the device you are connecting to? Brand, model

Later your receive may need some work; one thing at a time.

Tom
If I was able to read from it through Serial doesn't that mean that the baud rate was correctly set ? I have set the baud rate on the radar to 9600.

I've finally received some more details from the manufacturer and your guess was right. I had to add \r\n at the end of the string.
So this now works: USE_SERIAL1.write("SET detectionDirection 0\r\n");

idahowalker
Posts: 150
Joined: Wed Aug 01, 2018 12:06 pm

Re: [SOLVED] How to check if Serial.write() sent data ?

Postby idahowalker » Thu Oct 31, 2019 10:18 pm

Code: Select all

#include <HardwareSerial.h>

HardwareSerial GPSSerial ( 1 );
HardwareSerial LIDARSerial ( 2 );

setup()
{
 LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
  GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); 
}
With the hardware matrix you can define what pins on an ESP32 do or not do.

Who is online

Users browsing this forum: No registered users and 9 guests