Split multiple values from one line received in the serial connection

I'm working on with Current and Voltage sensor circuit with Arduino Mega.
The current and voltage circuit sends the multiple parameters data into a single line using serial communication(Rx/Tx).

Output Format: C99.9D0V99.9D

C = Current: 99.9, Measured Current values,
D = D for DC can be used as a delimiter,
0 = 0 if No Over-Current Detect, 1 if Over-Current Detect.
V = Voltage, 99.9. Measured Voltage, D: DC

I want to separate all the values into different variables:

float current, voltage;
bool OverCurrent;

please help me out, thanks in advance.
sensor details: https://www.amazon.in/gp/product/B098TVS8S1/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1

I already tried: reading/splitting multiple values in one line received in the serial connection

are you receiving this in a String instance or in a cString ?

Assuming that the results are always the same length.
Make a few global variables for current, over_current, and voltage
Make a function that reads the value or receives it as a parameter.

Ignore the C
Copy the next 4 characters into a buffer c-string, use strtoF(buffer) and store the returned value to the current variable.

Ignore the D
If the next character is '0' store false in over_current otherwise store true.

Ignore the V
Store the next 4 characters into the same buffer c-string, use strtoF(buffer) and store the returned value to the current variable.

Ignore the D

You will not need to return anything from this function.

However, if the length of the responses can vary, then it gets a bit more complex, but not too much.

What about the following?

char const* line = "C99.9D0V99.9D";  // Assume this is given to us.

char* ptr = const_cast<char*>(line);
float current = strtod(ptr + 1, &ptr);         // Skip one character, read a double, convert to float.
bool overCurrent = strtol(ptr + 1, &ptr, 10);  // Skip one character, read an integer, convert to bool.
float voltage = strtod(ptr + 1, &ptr);         // Skip one character, read a double, convert to float.

Serial.println(current);      // Prints "99.90".
Serial.println(overCurrent);  // Prints "0".
Serial.println(voltage);      // Prints "99.90".

If you receive a String instead:

String line = "C99.9D0V99.9D";  // Assume this is given to us.

char* ptr = const_cast<char*>(line.c_str());
// The rest remains the same.

thanks, everyone for your valuable response.
I have a piece of code from the sensor Module manufacturer. but I'm facing a problem with SoftwareSerial. I'm working on Arduino Mega, but this code is suitable for Arduino Uno.

please help me guys, Thanks in advance.


String IS , VS;
int DPos1, DPos2, CPos, VPos;
char solarByte;
String solar, solar2, solar3;

SoftwareSerial mySerial(D1, D2); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() // run over and over
{// Read all data from VVM sensor and store to string "solar"
  while (mySerial.available()) {
    solarByte = mySerial.read();
    solar.concat(solarByte);
  }
 Serial.println(solarByte);
//Find the index of first "D" in the string
  DPos1 = solar.indexOf('D');
//Find the index of second "D" in the string
  DPos2 = solar.indexOf('0', DPos1 + 1);
//Find the index of "C" in the string
  CPos = solar.indexOf('C');
//Find the index of "V" in the string
  VPos = solar.indexOf('V');
//Extract Current value inbetween "C" and first "D" position
  IS = solar.substring(CPos+1, DPos1);
//Extract Voltage value inbetween "V" and second "D" position
  VS = solar.substring(VPos + 1, DPos2);
  
  Serial.println(IS);
  Serial.println(VS);
//Clear the sting
  solar = "";
//Wait for a second before next poll
  delay(1000);


}

please tell me how can I define the rx/tx pins

1 Like

Then why use SoftwareSerial when the Mega has 4 hardware UARTs ?

Declare D1 and D2 as the required pin numbers

I note too that you have not #included SoftwareSerial.h

Then using SoftwareSerial is unnecessary, counter-productive.
You have 4 UARTs available - use them.

Unless i'm mistaken, you've grabbed one of the pins for serial (D1), and assigned it to SoftwareSerial. Not going to work. D0, D1 are used for Serial.

I'd suggest you get your device communication working on one of the other hardware serials, 1,2 or 3 during this debugging phase, even if it means reassigning other hardware.
Get your whole app working. Then when ready, move it to the Uno and use software serial.
In the meantime, maybe play with software serial for a much simpler sketch on the Uno, just to work out the basics.
Finally, merge the two.
Just my 2 cents worth, but more than you paid for it.
C

How can i implement in this code?? UART's
Please help :smiling_face_with_tear:

How can i use UART's, Please help me.

Arduino Mega 2560 R3 - Serial Port Basics - Arduino Project Hub

Use Serial1, Serial2 or Serial3 and the associated pins for Rx and Tx

https://www.arduino.cc/reference/en/language/functions/communication/serial/

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