2016-05-15 2 views
-1

主な質問はこちらHow to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?このコードに基づいて16進数をASCIIに変換するには?

ここで、16進数の値を取得してアスキーと表示に変換しようとしています。

主なコードは、この

public partial class MainForm : Form 
{ 
    private SerialPort _serialPort; // formda kullanilacak degisken 
    private const int BaudRate = 9600; // BaudRate Constant. default 9600 ile oynanabilir 
    public MainForm() 
    { 
     InitializeComponent(); 
    } 

    private void MainForm_Load(object sender, EventArgs e) 
    { 
     this.MinimizeBox = false; 
     string[] portNames = SerialPort.GetPortNames(); // bütün kullanilabilecek com portlari okur 
     foreach (var portName in portNames) 
     { 
      comboBox1.Items.Add(portName); // Adds Ports to combobox 
     } 
     if (comboBox1.SelectedIndex != -1) 
     { 
      comboBox1.SelectedIndex = 0; // Selects first entry (convenience purposes) 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // This block ensures that no exceptions happen 
     if (_serialPort != null && _serialPort.IsOpen) 
      _serialPort.Close(); 
     if (_serialPort != null) 
      _serialPort.Dispose(); 
     // End of Block 

     _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One); //<-- Creates new SerialPort using the name selected in the combobox 

     _serialPort.DataReceived += SerialPortOnDataReceived; //<-- this event happens everytime when new data is received by the ComPort 
     _serialPort.Open(); //<-- make the comport listen 
     textBox1.Text = string.Format("Listening on {0}...", comboBox1.Text); 

    //here i am trying @Adam Casey 's code and serialReceived thing doesn't work. 
     byte[] serialReceived; 
     string reading = Encoding.UTF8.GetString(serialReceived); 
     textBox2.Text = reading.Substring(13); 
    } 
    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 
      { 
       textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte()); //<-- bytewise adds inbuffer to textbox 
      } 
     } 
    } 

答えて

0

私はあなたの問題を詳しく説明している場合、私は助けることができ、COMポートとRS232での経験をしましたです。

問題が文字列ベースの16進値をASCIIに変換するだけの場合は、次のようにします。

033 ID_00あなたのコメントによると

  //41 is ACII 'A' 
     String hs = "41"; 
     var x = Convert.ToUInt32(hs, 16); 
     StringBuilder sb= new StringBuilder(); 
     sb.Append(Convert.ToChar(x)) 
     String s = sb.ToString(); 

EDIT:10.6キロは、あなたが任意の初期文字を持っている場合は、あなたが最初にそれをサブストリングかもしれませんが、この正規表現は意志コード

String hs = "x30 30 33 33 20 49 44 5F 30 30 3A 20 20 20 31 30 2E 36 20 6B 67 20 0D 0A 0D 0A"; 
      System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(hs, "([A-Z-0-9]{2}) "); 
      StringBuilder sb = new StringBuilder(); 
      System.Text.RegularExpressions.Match m = match.NextMatch(); 
      while(m.Success) 
      { 
       var x = Convert.ToUInt32(m.Value.Trim(),16); 
       sb.Append(Convert.ToChar(x)); 
       m= m.NextMatch(); 
      } 


      String s = sb.ToString(); 

次によって収集されますあなたのためにmach par hexを使用してください。これに注意して、: (.+?) kgなどのkg情報を収集するregextをコード化することがASCII表現からkg情報を取得するパターンであることに注意してください。

+0

私はポート1をリッスンし、COM1でリッスンするような値を取得しています... 30 30 33 33 20 49 44 5F 30 30 3A 20 20 20 31 30 2E 36 20 6B 67 20 0D 0A 0D 0A。私はそれらを取得し、ascii値に変更し、ラベルに表示したい。 –

+0

sorrymyの出力はこのようです。私はデバイスから複数行のテキストボックスに出力を取得しています。私のプログラムは1秒後にフリーズしています。ここに出力です。 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 38 30 20 20 20 20 20 30 D 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 30 30 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 38 30 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0 28 28 30 71 70 30 20 20 20 38 30 20 20 イメージ:http://s32.postimg.org/mke80rwtx/IMG_20160515_143350.jpg –

+0

このようにスケールを統合することはできませんが、私はRS232パラメータをチェックするように提案することができます。ボーレート、一部、フロー制御などそれはキャッシャースケールですか? –

関連する問題