I am sending data from a GUI (designed using C#) to a PIC microcontroller. Each data is separated by a "," and each data string is terminated by a "\n". This is how I am doing it.
Code:
send_s = "#DCMC#" + "," + kp + "," + ki + "," + kd + "," + v + "," + i + "," + t + "," + s + "," + "\n";
if (!serialPort1.IsOpen)
return;
buff = send_s.ToCharArray();
serialPort1.Write(buff, 0, buff.Length);
The values kp, ki, kd, v, i, t, s are integers converted into characters. The phrase #DCMC# acts as an identifier so if the incoming string starts with "#DCMC#" then accept the string.
How do I obtain the integer data from this string? (I am using mikroC PRO for PIC)
Receive the whole string into an array if #DCMC# is received.
Now loop the array for 1st comma.
Read the data into variable till another comma is encountered.
Do this to get all parameters into different variables (arrays)
Convert the string value to float value or integer.