Reputation: 421
I am working on creating a windows form application and I have come across a problem. I am reading data in from the serial port.
string RxString = serialPort1.ReadExisting();
This works fine but what I now want to do is extract values from my RxString
and put them into their own string arrays.
This the RxString format:
GPS:050.1347,N,00007.3612,WMAG:+231\r\n
it repeats itself as more data is added from the serial port the numbers change but stay the same length, and the + changes to -. I want to put the numbers between GPS: and ,N into one string array, the numbers between N, and ,W in another string array and finally the numbers between + and \r\n in a 3rd string array.
How would I go about doing this?
Upvotes: 1
Views: 1452
Reputation: 17402
This is not the best solution since it uses "magic" numbers and substrings - but may work for your situation since you said the string length is always the same.
var serialPortInfo = "GPS:050.1347,N,00007.3612,WMAG:+231\r\n";
private List<string> value1 = new List<string>();
private List<string> value2 = new List<string>();
private List<string> value3 = new List<string>();
private void populateValues(string s)
{
// this should give an array of the following:
// values[0] = "050.1347"
// values[1] = "N"
// values[2] = "00007.3612"
// values[3] = "WMAG:+231"
//
var values = (s.Substring(4, (s.Length - 8))).Split(',');
// populate lists
//
value1.Add(values[0]);
value2.Add(values[2]);
value3.Add(values[3].Substring(6, 3));
}
//usage
populateValues(serialPortInfo);
Upvotes: 0
Reputation: 7692
Ok, Regex solution:
string pattern = @"^GPS:(?<gps>.{8}),N,(?<n>.{10}),WMAG:(\+|\-)(?<wmag>.{3})\\r\\n$";
string gps = string.Empty;
string n = string.Empty;
string wmag = string.Empty;
string input = @"GPS:050.1347,N,00007.3612,WMAG:+231\r\n";
Regex regex = new Regex(pattern);
if (regex.IsMatch(input))
{
Match match = regex.Match(input);
foreach (Capture capture in match.Groups["gps"].Captures)
gps = capture.Value;
foreach (Capture capture in match.Groups["n"].Captures)
n = capture.Value;
foreach (Capture capture in match.Groups["wmag"].Captures)
wmag = capture.Value;
}
Console.Write("GPS: ");
Console.WriteLine(gps);
Console.Write("N: ");
Console.WriteLine(n);
Console.Write("WMAG: ");
Console.WriteLine(wmag);
Console.ReadLine();
Upvotes: 3
Reputation: 32481
Try this:
string RxString = serialPort1.ReadExisting();
string latitude = RxString.Split(',')[0].Substring(4);
string longitude = RxString.Split(',')[2];
string mag = RxString.Split(',')[3].Substring(6).Trim();
Upvotes: 1
Reputation: 198
I'm sure there's some RegEx that could make this prettier, but I'm not great at regex so I'd checkout C#'s String.Split function. Substring would work if you know the numbers will be the same length, but if that's not a guarantee, Split would be your best bet. You could split each line on the comma, creating an array, then use Replace to remove the extra text (like GPS: and WMAG:), if you know that's going to be the same every time.
Upvotes: 0
Reputation: 3171
If the string is always the same length, the best way is to use substring()
method.
Upvotes: 0