2017-04-10 2 views
0

Ich bin neu in VB und ich versuche, ein Programm zur Ausgabe von Text statt Hex. Ich habe den Code online gefunden, ein Programm namens maxiCOM. Hier ist der Link http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm. Der Quellcode kann am Ende der Seite heruntergeladen werden.

Leider ist das Niveau der Codierung im Programm weit über mein Verständnisniveau, und ich bekomme nicht wirklich, wie man die Ausgabe von Hex zu Text ändert. Ich würde es sehr schätzen, wenn mir jemand erklären könnte, wie man das macht. Das Snippet des Codes istKonvertieren von Byte in String in 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 

Antwort

0

Beachten Sie die erste Zeile nach Do in Ihrer inneren Schleife:

RXByte = COMPort.ReadByte 

Dies ist der einzige Punkt in Ihrem Code, in dem Sie den aktuellen Byte-Wert haben gelesen vom COM-Port. Nach diesem Punkt konvertiert der Code das Byte in zwei Hex-Werte unter Verwendung von Bit-Verschiebung. Sie sollten eine globale Variable vom Typ string erstellen und den Wert des gelesenen Bytes an diese Zeichenfolge anhängen, bevor sie verloren geht.

Fügen Sie die folgende Zeile unmittelbar nach der obigen Zeile:

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

wo YourString Ihre globale string variabel ist.

Beachten Sie, dass Sie einige Effizienz erreichen können, indem Sie StringBuilder anstelle von string verwenden, aber ich lasse das für Sie als Übung.

+0

Danke für die Hilfe! Ich habe es funktioniert, nachdem Sie Ihre Anweisungen befolgt und auch einige der Anzeige-Routine geändert haben. –

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

Sie sollten erklären, wie diese Einzeiler in Lage des OP passen. – dotNET

+1

@dotNET Chancen sind sie nicht die Frage gelesen, nur der Titel. – Jaxi