2017-04-10 11 views
0

나는 VB가 처음이므로 16 진수 대신 텍스트를 출력하는 프로그램을 얻으려고합니다. maxiCOM이라는 프로그램을 온라인에서 찾았습니다. 여기에 http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm 링크가 있습니다. 소스 코드는 페이지 하단에서 다운로드 할 수 있습니다.

불행히도 프로그램의 코딩 수준이 제 이해 수준보다 훨씬 높습니다. 실제로 16 진수에서 16 진수로 출력을 변경하는 방법은 없습니다. 누군가가 내게이 일을 어떻게 설명 할 수 있다면 크게 감사 할 것입니다.Visual Basic에서 Byte를 String으로 변환

RXByte = COMPort.ReadByte 

이것은 당신이 실제 바이트 값이 코드에서 유일한 점은 읽기 : 코드의 조각은

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 후 첫 번째 줄은 COM 포트에서. 이 지점 이후에 코드는 비트를 사용하여 바이트를 두 개의 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