Hola, espero que me puedan ayudar. Estoy programando una aplicacion para leer una balanza conectada al puerto COM1. La aplicacion esta desarrollada con Vb.net 2008 y para leer uso la siguiente funcion:
Código:
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
lo que llama a :
Código:
Public Delegate Sub myDelegate()
'agrega al textbox de lo que llega por balanza 1
Public Sub updateTextBox()
Dim buffer() As Byte = New Byte() {}
Dim bufSIZ As Integer
bufSIZ = serialPort.BytesToRead
Array.Resize(buffer, bufSIZ)
serialPort.Read(buffer, 0, bufSIZ)
With txtDataReceived
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.SelectionColor = Color.Red
.AppendText(ByteArrayToHexString(buffer))
.ScrollToCaret()
End With
End Sub
donde la funcion ByteArrayToHexString(buffer) es esta:
Código:
Public Shared Function ByteArrayToHexString2(ByVal bytes() As Byte) As String
Dim hexStr As String = "0x"
Dim i As Integer
For i = 0 To bytes.Length - 1
If Hex(bytes(i)).Length = 1 Then
hexStr &= 0 & Hex(bytes(i))
Else
hexStr &= Hex(bytes(i))
End If
Next i
Return hexStr
End Function
Mi problema es que por hyperterminal lee todo bien, osea por ejemplo las lecturas:
+ 1.2287 g
+ 0.8685 g
pero por vb.net me aparecen una lecturas en hexadecimal bastante extrañas (generalmente solo ceros y 8), por ejemplo esto:
0x80808080000x000000000x00000000008080800x00000000 008000800x800000000x00800080000x00000x008080000080 00
separandolas se pueden leer estas:
0x8080808000
0x00000000
0x0000000000808080
0x0000000000800080
0x80000000
0x0080008000
0x0000
0x00808000008000
Como puedo traducir esto a un formato leible, como el que me sale en el hyperterminal?
de antemano muchas gracias.