Bonjour à tous, j'essaie depuis quelque temps de réaliser une petite application en VB.NET.
L'application consistera à lire automatique le poids présent sur l'afficheur d'une balance Mettler toledo ind310.
J'utilise un port serie RS232.
Je suis très bien avancé car j'arrive à lire les données venues de la balance.
Mais mon problème je veux qu'il m'affiche comme ceci :
.
Ou encore que l'affichage soit identique à celui d'hyperTerminal.
Je vous met le code et le résultat Nom : mit 2.PNG
Affichages : 1774
Taille : 50,0 Ko en image.

code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
 
 
Public Class Form1
    Dim WithEvents Timer1 As New System.Windows.Forms.Timer()
    Dim myPort As Array 'COM Ports detected on the system will be stored here
    Dim WithEvents SerialPort1 As New SerialPort
    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)
 
        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
        cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
        btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
    End Sub
 
    Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
 
        SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
 
        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8 'Open our serial port
        SerialPort1.Open()
 
        btnConnect.Enabled = False 'Disable Connect button
        btnDisconnect.Enabled = True 'and Enable Disconnect button
    End Sub
 
    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        'btnDisconnect.Click()
        SerialPort1.Close() 'Close our Serial Port
 
        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub
 
    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        SerialPort1.Write("P" & vbCr)
        'The text contained in the txtText will be sent to the serial port as ascii
        'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
    End Sub
    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        'MsgBox([text].Length)
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [text]
        End If
    End Sub
 
    Private Sub cmbPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
        Else 'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub
 
    Private Sub cmbBaud_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbBaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
        Else 'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        rtbReceived.Text = " "
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        SerialPort1.Write("P" & vbCr)
 
    End Sub
 
    Private Sub rtbReceived_TextChanged(sender As Object, e As EventArgs) Handles rtbReceived.TextChanged
        'pds.Text = rtbReceived.Text(rtbReceived.TextLength - 1)
    End Sub
End Class