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:
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.
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);
}
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