2017-04-10 8 views
0

私はVBには新しく、16進ではなくテキストを出力するプログラムを取得しようとしています。 maxiCOMというプログラムをオンラインで見つけました。ここにリンクhttp://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htmがあります。ソースコードは、ページの下部にダウンロードできます。

残念ながら、プログラムのコーディングレベルは私の理解度をはるかに上回っており、出力を16進数からテキストに変更する方法は実際にはありません。もし誰かが私にこのことをどうやって説明することができれば、私はとても感謝しています。コードのスニペットがVisual Basicでバイトを文字列に変換する

Public Class MaxiTester 

Dim SpaceCount As Byte = 0 
Dim LookUpTable As String = "ABCDEF" 
Dim RXArray(2047) As Char ' Text buffer. Must be global to be accessible from more threads. 
Dim RXCnt As Integer  ' Length of text buffer. Must be global too. 

' Make a new System.IO.Ports.SerialPort instance, which is able to fire events. 
Dim WithEvents COMPort As New SerialPort 

Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived 
    Dim RXByte As Byte 
    Do 
     '----- Start of communication protocol handling ----------------------------------------------------------- 
     ' The code between the two lines does the communication protocol. In this case, it simply emties the 
     ' receive buffer and converts it to text, but for all practical applications, you must replace this part 
     '  with a code, which can collect one entire telegram by searching for the telegram 
     '  delimiter/termination. In case of a simple ASCII protocol, you may just use ReadLine and receive 
     '   in a global string instead of a byte array. 
     ' Because this routine runs on a thread pool thread, it does not block the UI, so if you have any data 
     ' convertion, encryption, expansion, error detection, error correction etc. to do, do it here. 
     RXCnt = 0 
     Do 
      RXByte = COMPort.ReadByte 
      RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters 
      RXCnt = RXCnt + 1 
      RXArray(RXCnt) = LookUpTable(RXByte And 15) 
      RXCnt = RXCnt + 1 
      RXArray(RXCnt) = " " 
      RXCnt = RXCnt + 1 
      SpaceCount = (SpaceCount + 1) And 31  ' Insert spaces and CRLF for better readability 
      If SpaceCount = 0 Then     ' Insert CRLF after 32 numbers 
       RXArray(RXCnt) = Chr(13) ' CR 
       RXCnt = RXCnt + 1 
       RXArray(RXCnt) = Chr(10) ' LF 
       RXCnt = RXCnt + 1 
      Else 
       If (SpaceCount And 3) = 0 Then  ' Insert two extra spaces for each 4 numbers 
        RXArray(RXCnt) = " " 
        RXCnt = RXCnt + 1 
        RXArray(RXCnt) = " " 
        RXCnt = RXCnt + 1 
       End If 
      End If 
     Loop Until (COMPort.BytesToRead = 0) 
     '----- End of communication protocol handling ------------------------------------------------------------- 
     Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread 
    Loop Until (COMPort.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime 
End Sub 

' Text display routine, which appends the received string to any text in the Received TextBox. 

Private Sub Display() 
    Received.AppendText(New String(RXArray, 0, RXCnt)) 
End Sub 

答えて

0

お知らせあなたの内側のループでDo後の最初の行です:

RXByte = COMPort.ReadByte 

これは、あなたが読んで、実際のバイト値を持っているあなたのコード内の唯一のポイントですCOMポートから。このポイントの後、コードはビットシフトを使用してバイトを2つの16進値に変換します。 stringタイプのグローバル変数を作成し、読み込みバイト値をこの文字列に追加してから失われている必要があります。 YourStringがあなたのグローバルstring変数である

YourString &= Convert.ToChar(RXByte).ToString() 

は、すぐ上の行の後に次の行を追加します。

stringの代わりにStringBuilderを使用して効率を上げることができますが、私は運動としてそれを残しています。

+0

助けてくれてありがとう!あなたの指示に従い、表示ルーチンのいくつかを修正した後、私はそれを動作させました。 –

-1
System.Text.Encoding.Unicode.GetString(bytes) 'bytes is your byte array' 
+2

このワンライナーがOPの状況にどのように適合するか説明してください。 – dotNET

+1

@dotNETチャンスは彼らが質問を読んでいない、タイトルだけです。 – Jaxi

関連する問題