2016-05-16 156 views
1

我正在從秤(串行端口)接收數據爲十六進制。我想將它轉換成ASCII碼,只需要它的重量。 代碼:如何將HEX轉換爲ASCII並僅從中獲取數字?

private void button1_Click(object sender, EventArgs e) 
    { 

     if (_serialPort != null && _serialPort.IsOpen) 
      _serialPort.Close(); 
     if (_serialPort != null) 
      _serialPort.Dispose(); 

     _serialPort.BaudRate = Convert.ToInt16(cbBaud.Text); 
     _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One); 

     _serialPort.DataReceived += SerialPortOnDataReceived; 
     _serialPort.Open(); 
     textBox1.Text = string.Format("Listening on {0}...", comboBox1.Text); 

    } 
    private delegate void Closure(); 
    private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs) 
    { 
     if (InvokeRequired)  
      BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));  
     else 
     { 
      while (_serialPort.BytesToRead > 0) 
      { 
       textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte()); 
      } 
     } 

這是我的輸出: 上偵聽COM1 ... 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20。 ...

preview

這是ASCII碼我手動轉換從網站:

0 
    ( q p 0   8 0   0 
    ( q p 0   8 0   0 
    ( q p 0   8 0   0 
    ( q p 0   8 0   0 
    ( q p 0   8 0   0 
    ( q p 0   8 0   0 
    ( q p 0   8 0  
+1

你有個字節數組,所以你可以使用這個:System.Text.Encoding.SomeEncoding.GetString(字節) –

回答

1

您已經複製了一些expample但也有更適合的方法從能更好地滿足你的目的串口讀取。

首先您需要從手冊中確定權重指標的數據格式。如果你想閱讀所有

string myData = _serialPort.ReadTo("\r"); 

其中"\r"重量記錄分隔符(也可能是其他還有像琴絃<CR><LF><STX><ETX>等)

:然後你就可以用這個(而不是while循環)從串口緩衝區可用的輸入數據,可以使用

string myData = _serialPort.ReadExisting(); 

,並通過指定的字符

分割字符串
string[] weightRecords = myData.Split('\r'); 

然後,您從所獲得的字符串中分析權重數值。所以你不需要從字節轉換數據,你可以直接讀取字符串。如果您需要更多靈感,請檢查this project


如果你想嘗試一下,你可以重寫你的DataReceived處理程序的身體是這樣的:

if (InvokeRequired)  
     BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));  
    else 
    { 
     //There may be multiple records available in the Serial Port buffer 
     string myData = _serialPort.ReadExisting(); 
     //Suppose that 0D character (equals to \r) marks the end of record, see manual of the indicator 
     string[] weightRecords = myData.Split('\r'); 
     foreach (var s in weightRecords) 
     { 
       //9,5 = Position and length of the numeric weight value, for exact values see your weight indicator manual 
       textBox1.Text += string.Format("{0} kg \n", s.Substring(9,5)); 
     } 
    } 

要由包括多個字符的分隔符分割你的字符串,你可以使用這樣的事情:

string[] stringSeparators = new string[] { 
    System.Text.Encoding.ASCII.GetString(new byte[] {0x0d, 0x28}) 
}; 
string[] weightRecords = myData.Split(stringSeparators, StringSplitOptions.None); 
+1

謝謝你的幫助。我其實是編程的新手,只是試圖找到一些代碼並對其進行編輯。我應該只刪除while循環還是應該刪除整個if和else塊? –

+1

@ArdaIskender查看編輯答案... –