Serial Data with CR/LF

I've asked this before so I thought I'd start again with just the basic and get it printing correctly before moving on.

I've gone through the Serial tutorial by Robin2 and this how I've got this far.
It all seems to be working but sometimes there seem to be a 3/4 second delay in it displaying the data according to the serial monitor where I've added a time stamp. All the data seems to be displaying correctly but it's just the delay in between it displaying the data.
This is from the manual.

The Pro 3600 has an ASCII-format RS-232 compatible serial port for remote angle readout.
The T&B Ansley 609-1027 connector on the back of the Pro3600 mates with industry
standard cables. Angles are calculated and transmitted every 8/15 second (533 msec).
1 GND N/A Signal Ground.
2 TD Output Transmits serial data bits (9600 baud, one stop bit, no
parity). Hi impedance except during transmission bursts.
5 REQ Input RS-232 input; high signal requests data output.
9 BATT+ Either Power input, or output if battery is installed in unit.
3, 4, 6-8, 10 Unused.
If REQ is high when power is applied or the unit is turned on, and it remains high, an output occurs every 8/15 second.
If REQ is ever low or floating and the unit is on, outputs occur as a result of subsequent low-to-high transitions on REQ,
provided REQ remains high for at least 100 msec. The output rate does not exceed one every 8/15 second.

This is the code that I've got working

// Example 3 - Receive with start- and end-markers
/*********
   #### TX CODE ####
  Wireless SPI-TRONIC Pro 3600 Digital Protractor
  General Information
  The Pro 3600 has an ASCII-format RS-232 compatible serial port for remote angle readout.
  The T&B Ansley 609-1027 connector on the back of the Pro3600 mates with industry
  standard cables. Angles are calculated and transmitted every 8/15 second (533 msec)
  Angle Output Format:
  The ASCII angle output may be read by a computer, or may directly drive a printer. Measured
  angles cover a full 360° range and the readout is between -180.00° and +180.00°.
  Format:
  <sign> XXX.XX <carriage return><line feed>
  examples:
  + 124.50
  + 32.70
  + 9.38
  - 4.32
  - 179.9
*********/
#include <HardwareSerial.h>
HardwareSerial MySerial(1);// MySerial.begin(9600, SERIAL_8N1, 16, 17);
const byte numChars = 9;
char receivedChars[numChars];
uint8_t broadcastAddress[] = {0X24, 0X6F, 0X28, 0XB3, 0XF3, 0XE0}; //MAC adress been sent to
boolean newData = false;
typedef struct Data_struct {
  int test = 10;
  char receivedChars[numChars];   // an array to store the received data
} Data_struct;
Data_struct Data;

void setup() {
  Serial.begin(115200);
  MySerial.begin(9600, SERIAL_8N1, 16, 17);
  Serial.println("<Arduino is ready>");

}

void loop() {
  recvWithStartEndMarkers();
  showNewData();
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char Plus_sign = '+';
  char Minus_sign = '-';
  char endMarker =  '\n';
  char rc;

  while (MySerial.available() > 0 && newData == false) {
    rc = MySerial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        Data.receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        Data.receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;

      }
    }

    else if ((rc == Plus_sign) ||  (Minus_sign)) {// Start Maker either plus/minus start bit
      recvInProgress = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(Data.receivedChars);
    newData = false;
  }
}

And this is the data been displayed.

22:37:30.929 -> This just in ... -  0.47
22:37:30.969 -> This just in ... -  0.47
22:37:32.009 -> This just in ... -  0.47 // 2 second dely
22:37:32.009 -> This just in ... -  0.47
22:37:34.689 -> This just in ... -  0.47
22:37:34.689 -> This just in ... -  0.47
22:37:34.689 -> This just in ... -  0.47
22:37:34.689 -> This just in ... -  0.47
22:37:34.689 -> This just in ... -  0.47
22:37:35.729 -> This just in ... -  0.47
22:37:35.729 -> This just in ... -  0.47
22:37:39.442 -> This just in ... -  0.47
22:37:39.482 -> This just in ... -  0.47
22:37:39.482 -> This just in ... -  0.47
22:37:39.482 -> This just in ... -  0.47
22:37:39.482 -> This just in ... -  0.47
22:37:39.482 -> This just in ... -  0.47
22:37:39.482 -> This just in ... -  0.47
22:37:40.522 -> This just in ... -  0.47
22:37:40.522 -> This just in ... -  0.47 //4 second delay
22:37:44.254 -> This just in ... -  0.47
22:37:44.254 -> This just in ... -  0.47
22:37:44.254 -> This just in ... -  0.47
22:37:44.254 -> This just in ... -  0.47
22:37:44.254 -> This just in ... -  0.47
22:37:44.254 -> This just in ... -  0.47
22:37:44.254 -> This just in ... -  0.47
22:37:45.334 -> This just in ... -  0.47
22:37:45.334 -> This just in ... -  0.47
22:37:46.374 -> This just in ... -  0.47
22:37:46.374 -> This just in ... -  0.47
22:37:49.056 -> This just in ... -  0.47
22:37:49.056 -> This just in ... -  0.47
22:37:49.056 -> This just in ... -  0.47
22:37:49.056 -> This just in ... -  0.47
22:37:49.056 -> This just in ... -  0.47
22:37:51.176 -> This just in ... -  0.47
22:37:51.176 -> This just in ... -  0.47
22:37:51.176 -> This just in ... -  0.47
22:37:51.176 -> This just in ... -  0.47
22:37:51.696 -> This just in ... -  0.47
22:37:53.816 -> This just in ... -  0.47
22:37:53.816 -> This just in ... -  0.47
22:37:53.816 -> This just in ... -  0.47
22:37:53.856 -> This just in ... -  0.47
22:37:54.899 -> This just in ... -  0.47
22:37:54.899 -> This just in ... -  0.47
22:37:55.939 -> This just in ... -  0.47
22:37:55.978 -> This just in ... -  0.47
22:37:57.568 -> This just in ... -  0.47
22:37:57.568 -> This just in ... -  0.47
22:37:57.568 -> This just in ... -  0.47

I did read this topic here which I found very interesting and tried some of the code, Reading the string type code seemed to work great but then it messed up the PCD8544 by printing it over 2 lines.
https://forum.arduino.cc/index.php?topic=733177.0

Am I missing something or doing something wrong ?

... ||  (Minus_sign) ...

― ? ―

I've corrected that but still get the same behaviour ?
I’m trying to do it that way as from my understanding the start marker can either be a +/- sign
New code

// Example 3 - Receive with start- and end-markers
/*********
   #### TX CODE ####
  Wireless SPI-TRONIC Pro 3600 Digital Protractor
  General Information
  The Pro 3600 has an ASCII-format RS-232 compatible serial port for remote angle readout.
  The T&B Ansley 609-1027 connector on the back of the Pro3600 mates with industry
  standard cables. Angles are calculated and transmitted every 8/15 second (533 msec)
  Angle Output Format:
  The ASCII angle output may be read by a computer, or may directly drive a printer. Measured
  angles cover a full 360° range and the readout is between -180.00° and +180.00°.
  Format:
  <sign> XXX.XX <carriage return><line feed>
  examples:
  + 124.50
  + 32.70
  + 9.38
  - 4.32
  - 179.9
*********/
#include <HardwareSerial.h>
HardwareSerial MySerial(1);// MySerial.begin(9600, SERIAL_8N1, 16, 17);
const byte numChars = 9;
char receivedChars[numChars];
uint8_t broadcastAddress[] = {0X24, 0X6F, 0X28, 0XB3, 0XF3, 0XE0}; //MAC adress been sent to
boolean newData = false;
typedef struct Data_struct {
  int test = 10;
  char receivedChars[numChars];   // an array to store the received data
} Data_struct;
Data_struct Data;

void setup() {
  Serial.begin(115200);
  MySerial.begin(9600, SERIAL_8N1, 16, 17);
  Serial.println("<Arduino is ready>");

}

void loop() {
  recvWithStartEndMarkers();
  showNewData();
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char Plus_sign = '+';
  char Minus_sign = '-';
  char endMarker =  '\n';
  char rc;

  while (MySerial.available() > 0 && newData == false) {
    rc = MySerial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        Data.receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        Data.receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;

      }
    }

    else if ((rc == Plus_sign) ||  (rc == Minus_sign)) {// Start Maker either plus/minus start bit
      recvInProgress = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(Data.receivedChars);
    newData = false;
  }
}

and still get the same results

07:40:11.175 -> This just in ...   4.27
07:40:11.175 -> This just in ...   4.69
07:40:11.175 -> This just in ...   4.88
07:40:12.215 -> This just in ...   4.95
07:40:12.215 -> This just in ...   5.09
07:40:13.254 -> This just in ...   5.09
07:40:13.254 -> This just in ...   5.05
07:40:14.334 -> This just in ...   5.05
07:40:14.334 -> This just in ...   5.05
07:40:18.055 -> This just in ...   4.98
07:40:18.055 -> This just in ...   4.92
07:40:18.055 -> This just in ...   4.69
07:40:18.055 -> This just in ...   4.38
07:40:18.055 -> This just in ...   4.22
07:40:18.055 -> This just in ...   3.88
07:40:18.055 -> This just in ...   3.39
07:40:21.255 -> This just in ...   2.36
07:40:21.255 -> This just in ...   1.96
07:40:21.255 -> This just in ...   1.65
07:40:21.255 -> This just in ...   0.87
07:40:21.255 -> This just in ...   0.51
07:40:21.255 -> This just in ...   0.51
07:40:22.296 -> This just in ...   0.51
07:40:22.296 -> This just in ...   0.51
07:40:24.456 -> This just in ...   0.51

And I’ve just noticed since I’ve changed the code the +/- sign is now missing from the serial monitor

Are you just looking to parse something like this?

<sign> XXX.XX <carriage return><line feed>

Or did I miss something here?

-jim lee

jimLee:
Are you just looking to parse something like this?

<sign> XXX.XX <carriage return><line feed>

Or did I miss something here?

-jim lee

Yeah that's what I'm trying to achieve and then send the data using 2 ESP32'S from the sender to the receiver.
But need to make this side of things work before moving on.
After reading through that tutorial i mentioned, I thought it would be the best approach so I can make sure I capturer all the data from start to finish and make sure I got it all before sending the data out and not missing any data or only capture bits of the data coming in.
If I connect the digital level direct to the computer serial port I can see the exactly the same data coming in without the longer delays

Steveiboy:
If I connect the digital level direct to the computer serial port...

You do or do not have a converter between your microcontroller and that serial port?

Connected direct to the digital level is a RS232 MAX232DR Line Transceiver running from 5V's and this connects to the ESP32 through a level shifter, I've also tired running the MAX232DR direct from 3V and get the exact same results, I still use the MAX232DR Line Transceiver from the controller and then this connects to a RS232 to USB Serial converter as my pc does not have a direct serial port on it.
This is the data coming on into the serial monitor

08:00:34.153 -> -  0.29
08:00:34.673 -> -  0.29
08:00:35.230 -> -  0.29
08:00:35.750 -> -  0.29
08:00:36.273 -> -  0.34
08:00:36.828 -> -  0.18
08:00:37.351 -> -  1.60
08:00:37.872 -> -  2.11
08:00:38.429 -> -  3.13
08:00:38.951 -> -  3.21
08:00:39.473 -> -  3.40
08:00:39.995 -> -  1.40
08:00:40.552 -> -  0.26
08:00:41.074 -> -  0.26
08:00:41.596 -> -  0.26
08:00:42.154 -> -  0.26
08:00:42.675 -> -  0.26
08:00:43.199 -> -  0.26
08:00:43.720 -> -  0.26
08:00:44.278 -> -  0.26
08:00:44.794 -> -  0.26
08:00:45.347 -> -  0.26

This is also the other part of data out the manual

DC Electrical Characteristics:
Parameter               Min. Nominal Max. Unit
REQ Input low voltage   -25    —     0.4  volts
REQ Input high voltage  2.4    —      25 volts
REQ Input resistance     3     5      7    kΩ
TD Output low voltage     —   -5      —   volts
TD Output high voltage   —    +5      —   volts
BATT+ Input voltage    4.25    9      10  volt

I've also attached a copy of the user manual

PRO3600_Instructions.pdf (449 KB)

Did you get it working?

Yeah Thanks,
In the end it was an hardware issue, I tried adding a 10K resistors the the RX pin on the ESP32 while I was using the 5V supply with the level shifter and got the same problem the I went back to running the RS232 of the 3V supply and added a 10K resistor to the RX pin on the ESP32 and this cured the problem with the 2-4 second delay and the data comes in with no delays now

10:19:13.811 -> -  0.20
10:19:14.323 -> -  0.20
10:19:14.868 -> -  0.33
10:19:15.384 -> -  5.49
10:19:15.928 -> -  6.33

I've added the the net work code now and sending and receiving data with no issues and no delays.
I just now trying to figure out to set an error flag so if the digital level stops sending the serial Data IE. if cable gets disconnected or the battery goes flat the Receiver shows this on the display
This is my latest sender code:

/*********
   #### TX CODE ####
  Wireless SPI-TRONIC Pro 3600 Digital Protractor
  General Information
  The Pro 3600 has an ASCII-format RS-232 compatible serial port for remote angle readout.
  The T&B Ansley 609-1027 connector on the back of the Pro3600 mates with industry
  standard cables. Angles are calculated and transmitted every 8/15 second (533 msec)
  Angle Output Format:
  The ASCII angle output may be read by a computer, or may directly drive a printer. Measured
  angles cover a full 360° range and the readout is between -180.00° and +180.00°.
  Format:
  <sign> XXX.XX <carriage return><line feed>
  examples:
  + 124.50
  + 32.70
  + 9.38
  - 4.32
  - 179.9
*********/
#include <esp_now.h>
#include <WiFi.h>
#include <TimedAction.h>
#include <HardwareSerial.h>
HardwareSerial MySerial(1);// MySerial.begin(9600, SERIAL_8N1, 16, 17);
const byte numChars = 9; //Number of bits to get
char receivedChars[numChars];//store the value of imcoming data
uint8_t broadcastAddress[] = {0X24, 0X6F, 0X28, 0XB3, 0XF3, 0XE0}; //MAC adress been sent to
boolean newData = false;
unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long  timeReceived = 0;
#define ECHO_TO_SERIAL 1 //change to zero = no serial output
boolean receiveUntilTimeout = false;
unsigned long timeOut = 2000;
typedef struct Data_struct {
  int test = 10;
  char receivedChars[numChars];   // an array to store the received data
} Data_struct;
Data_struct Data;

void printRecord(Print* pr, char sep = ',') {
  pr->print(Data.receivedChars[0]); // Print btye 0
  pr->print(Data.receivedChars[1]); // Print btye 1
  pr->print(Data.receivedChars[2] ); // Print btye 2
  pr->print(Data.receivedChars[3]); // Print btye 3
  pr->print(Data.receivedChars[4]); // Print btye 4
  pr->print(Data.receivedChars[5]); // Print btye 5
  pr->print(Data.receivedChars[6] ); // Print btye 6
  pr->print(Data.receivedChars[7]); // Print btye 7
  pr->print(Data.receivedChars[8]); // Print btye 7
  pr->print(sep);
  pr->print(Data.test); // Print btye 7
  pr->print(sep);
  pr->print(receiveUntilTimeout);
  pr->print(newData); // Print btye 7
  pr->print(sep);
  pr->println();

}

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  // Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  //Serial.print(macStr);
 // Serial.print(" send status:\t");
 // Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
  Serial.begin(9600);
  MySerial.begin(9600, SERIAL_8N1, 16, 17);
  Serial.println("<Arduino is ready>");
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  esp_now_register_send_cb(OnDataSent);
  // register peer
  esp_now_peer_info_t peerInfo;
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}

void loop() {
  recvWithEndMarker();
  if (newData ) { //new data received from eLevel
    newData = false;
    Data.test = 10;
#if ECHO_TO_SERIAL
    printRecord(&Serial);
#endif //ECHO_TO_SERIAL
    Send_data();
    // Send debug to serial port if enabled

  }

//send data outside the while loop
//#if ECHO_TO_SERIAL
//    printRecord(&Serial);
//#endif //ECHO_TO_SERIAL
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  if (Serial.available() > 0 && newData == false) {
    //while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    receiveUntilTimeout = true;
    timeReceived = millis();
    if (rc != endMarker) {

      Data.receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      Data.receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
      if (millis() - timeReceived >= timeOut &&  newData == true )// this neever seem to run
      {
        receiveUntilTimeout = false;
        printRecord(&Serial);
        timeReceived = millis();
      }
    }
  }
}

void Send_data() {
  esp_err_t result1 = esp_now_send(broadcastAddress, (uint8_t *) &Data, sizeof(Data));
}

Has it stand at the moment I can't get the receiveUntilTimeout boollean to change if data stops coming in

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.