2017-01-12 2 views
0

読むシリアルポート体重枢機卿規模、C#の私は自分のコンピュータへのインジケータのデータを表示することができました210

出力ルックのような、この

「[SPACE] [SPACE] 978 0キロ」

[SPACE]は(空の)スペース、テキスト、私は数字だけを表示したい

、ある

次のスクリプトを使用します。最後の数字のゼロが入力されていません

private delegate void Closure(); 
    private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs) 
    { 
     if (InvokeRequired)  //<-- Makes sure the function is invoked to work properly in the UI-Thread 
      BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));  //<-- Function invokes itself 
     else 
     { 
      while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty 
      { 
       String tampung = _serialPort.ReadExisting(); 
       Regex regex = new Regex(@"[^\d|\.]"); 

       tampung = regex.Replace(tampung, ""); 


       textBox1.Text += string.Format("{0:X2} ", tampung); 
      } 
     } 
    } 

が、それは不完全な数字を表示し、

出力:

私はインジケータをhttp://www.cardinalscale.com/cs_product/210-storm/

が使用しています何か間違っている?

答えて

0

正規表現を使用して文字列から数値を抽出し、次にTrim空白を削除することができます。

string input = " 940 0Kg"; 
string result = Regex.Replace(input, @"[^\d]", "").Trim(); 

そして、あなたは数としてそれを必要とする場合、当然の

int weight = int.Parse(result); 
0

string tampung = _serialPort.ReadExisting(); 
string pattern = @"(\d+\.\,\d+)"; 
MatchCollection matches = Regex.Matches(tampung, pattern); 

String tampung = _serialPort.ReadExisting(); 
Regex regex = new Regex(@"[^\d|\.]"); 

を交換してください

関連する問題