2013-03-26 50 views
1

我正在創建一個Windows窗體應用程序,我遇到了一個問題。我正在從串口讀取數據。串口數據的字符串操作

string RxString = serialPort1.ReadExisting(); 

這工作得很好,但我現在想要做的是提取值從我RxString,並把它們轉化爲自己的字符串數組。

此所述RxString格式:

GPS:050.1347,N,00007.3612,WMAG:+231\r\n 

它自身重複,更多數據從串行端口的數字改變添加,但保持相同的長度,並且+變爲 - 。我想將GPS:和N之間的數字放入一個字符串數組中,其他字符串數組中的N和W之間的數字以及第三個字符串數組中的+和\ r \ n之間的數字。

我該怎麼做呢?

+0

這可以很容易地用正則表達式來完成。你試過什麼了? – tnw 2013-03-26 19:11:13

+0

嘗試String.Split(...) – 2013-03-26 19:11:22

+1

當懷疑是真的時,沒有問題是愚蠢的。 – 2013-03-26 19:12:40

回答

3

好了,正則表達式的解決方案:

 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(); 
+0

這是真棒感謝的人:-)有一個問題,我忘了我的問題,但WMAG後的值225提這樣的:1,2和3位數字+之間變化。 在字符串模式,我怎麼說數字的WMAG後的長度爲1或2或3個數字長。我試過這個,但它不起作用: 'string pattern = @「^ GPS :(?。{8}),N,(?。{10}),WMAG:(\ + | \ - )(? {3 | 2 | 1})\\ř\\ N $。「;' – 2013-03-27 16:11:23

+0

@JamesArcher將其更改爲'( {1,3})' – 2013-03-27 18:24:54

+0

@JamesArcher任何反饋到目前爲止??不要忘記標記爲最能幫助你的怒雁。 – 2013-03-28 14:24:53

0

如果字符串總是長度相同,最好的方法是使用substring()方法。

+1

我不確定它是否是_best_解決方案,但它可能是最容易編碼的。 – 2013-03-26 19:14:14

+0

我最好的意思是最快的。 RegEx比'substring()'慢。 – 2013-03-26 19:23:35

+0

事實上,在大多數情況下。 – 2013-03-26 19:24:49

0

我確定有一些正則表達式可以使這個更漂亮,但我在正則表達式不是很好,所以我會簽出C#的String.Split函數。如果您知道數字的長度相同,子串將起作用,但如果這不是保證,Split將是您最好的選擇。 您可以在逗號分隔每一行,創建一個數組,然後使用替換刪除額外的文本(如GPS:和WMAG :),如果你知道這將每次都是相同的。

1

試試這個:

string RxString = serialPort1.ReadExisting(); 

string latitude = RxString.Split(',')[0].Substring(4); 
string longitude = RxString.Split(',')[2]; 
string mag = RxString.Split(',')[3].Substring(6).Trim(); 
+0

感謝這個工作得很好:-) – 2013-03-27 16:12:32

+0

確定。享受它:-) – 2013-03-27 16:14:34

+0

@JamesArcher這如果WMAG從1到3個字符長度的變化將無法正常工作。 – 2013-04-02 20:30:57

0

這是不是最好的解決方案,因爲它使用「魔術」號和子 - 但因爲你的情況可能工作表示字符串的長度總是相同的。

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);