Affichage Avec deux chiffres dans TextBox
Bonjour les développeurs,
Voila j'ai une question. Je récupère une données Rs232 en héxadécimal et je les affiches ensuite dans une TextBox et mon problème et l'orsque je reçois ma donnée hexa (par exemple : "05") ben il m'affiche juste "5", pour les autres aucun soucis (par exemple :"88" s'affiche "88"). je voudrais donc que dans ma TextBox il m'affiche bien le 0 devant les chiffres.
Je vous montre des Parties de mon code. Plus partique pour voir les variables:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Public Class Form1
Dim Data As Byte
Dim don1(900000) As String
Dim i As Integer = 1
'-------------------------------Reception des données du port série -----------------------
Private Sub SerialPort1_DataReceived() Handles SerialPort1.DataReceived
Do While SerialPort1.BytesToRead <> 0
Data = Data & Hex(SerialPort1.ReadByte)
don1(i) = Data
i = i + 1
Data = 0
Loop
Me.Invoke(New EventHandler(AddressOf DoUpdate))
End Sub
Public Sub DoUpdate()
TextBox2.Text = don1(1) & " " & don1(2) & " " & don1(3) & " " & don1(4) |
Voila si certain on une petite astuce ou la solution...
Merci d'avance
Affichage Avec deux chiffres dans TextBox ,stringbuilder
bonjour crokflot,croque bytes
Ce ne serait pas plus expeditif de passer par stringbuilder comme ceci.
code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
Imports System.Text
Imports System.IO.Ports
Public Class Form2
Dim Data(100000) As Byte
Dim I As Integer = 0
'à virer
'Dim don1(900000) As String
'-------------------------------Reception des données du port série -----------------------
Private Sub SerialPort1_DataReceived() Handles SerialPort1.DataReceived
Do While SerialPort1.BytesToRead <> 0
Data(I) = SerialPort1.ReadByte
'à virer
' 'don1(i) = Data
I = I + 1
Loop
Me.Invoke(New EventHandler(AddressOf DoUpdate))
End Sub
Public Sub DoUpdate()
'TextBox2.Text = don1(1) & " " & don1(2) & " " & don1(3) & " " & don1(4)
TextBox2.Text = ByteArrayToString(Data)
End Sub
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
Dim i As Integer
Dim sOutput As New StringBuilder(arrInput.Length)
For i = 0 To arrInput.Length - 1
sOutput.Append(arrInput(i).ToString("X2") & "-")
Next
Return sOutput.ToString()
End Function
'Pour Test
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AfficheHex()
End Sub
Private Sub AfficheHex()
Do While I < 255
Data(I) = Convert.ToByte(I)
I = I + 1
Me.Invoke(New EventHandler(AddressOf DoUpdateBis))
If I > 255 Then
'vide Data
ReDim Data(100000)
I = 0
End If
Loop
End Sub
Public Sub DoUpdateBis()
TextBox2.Text = ByteArrayToString(Data)
End Sub
End Class |
Bon code....