Page 1 of 1

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

Posted: Wed Oct 30, 2019 2:40 pm
by GeorgeFlorian1
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 ?

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

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

Also, maybe use a dumb terminal emulator to connect to the device to simplify the system; just to test comm.

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

Posted: Wed Oct 30, 2019 9:59 pm
by idahowalker
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.

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

Posted: Wed Oct 30, 2019 11:39 pm
by tommeyers
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

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

Posted: Thu Oct 31, 2019 10:34 am
by GeorgeFlorian1
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().

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

Posted: Thu Oct 31, 2019 11:02 am
by GeorgeFlorian1
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.

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

Posted: Thu Oct 31, 2019 1:36 pm
by tommeyers
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

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

Posted: Thu Oct 31, 2019 1:48 pm
by GeorgeFlorian1
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");

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

Posted: Thu Oct 31, 2019 10:18 pm
by idahowalker

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.