Application de recuperation du poids d'un objet

jeanjacquespic Messages postés 5 Date d'inscription mercredi 8 juin 2016 Statut Membre Dernière intervention 9 juin 2016 - Modifié par NHenry le 8/06/2016 à 18:34
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 - 9 juin 2016 à 17:05
bonjours à tous. j'ai un serieux problème. j'explique: je developpe une application qui consiste à recuperer le poids d'un objet venant d'un indicateur precia molen i30 à partir d'un port serie. mon problème est que je n'arrive à afficher le poids dans mon application. voici le code de mon application

Imports System
Imports System.IO.Ports
Imports System.Threading


Public Class Form1


    Private Sub InitPortCom()
        Try
            'ferme le port com
            If PortdeCom.IsOpen = True Then
                PortdeCom.Close()
            End If
            'affectation du n° du port com
            PortdeCom.PortName = choixdeport.Text

            'affectation de la parité
            Select Case choixparite.Text

                Case "Aucun"
                    PortdeCom.Parity = IO.Ports.Parity.None
                Case "Impair"
                    PortdeCom.Parity = IO.Ports.Parity.Odd
                Case "Pair"
                    PortdeCom.Parity = IO.Ports.Parity.Even
                Case Else
                    MsgBox("Erreur parité")
                    Exit Sub
            End Select

            'affectation de la vitesse
            PortdeCom.BaudRate = CInt(choixvitesse.Text)

            'affectation nombre de bits de données
            PortdeCom.DataBits = CInt(choixnbrebits.Text)

            'affectation du nombre de bits de stop à 1
            PortdeCom.StopBits = IO.Ports.StopBits.One

            'echange de données RTS
            PortdeCom.Handshake = IO.Ports.Handshake.RequestToSend

            'Ouvrir le portcom une fois que l'inintialisation est bonne
            'important: ajouter toujours un try/catch autour des fonctions
            'on essai d'ouvrir et si impossible, alors message d'erreur

            Try
                PortdeCom.Open()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        Catch ex As Exception
            MsgBox("Erreur durant l'initialisation du port com" & vbCrLf & vbCrLf & ex.ToString)
        End Try
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        choixdeport.Items.Clear()
        For Each PortName As String In My.Computer.Ports.SerialPortNames
            choixdeport.Items.Add(PortName)
        Next
        AddHandler PortdeCom.DataReceived, AddressOf PortdeCom_DataReceived
    End Sub

    Private Sub bt_connect_Click(sender As Object, e As EventArgs) Handles bt_connect.Click
        InitPortCom()
        
    End Sub

    Private comBuffer As Byte()
    Private Delegate Sub UpdateFormDelegate()
    Private UpdateFormDelegate1 As UpdateFormDelegate

    Private Sub PortdeCom_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles PortdeCom.DataReceived

        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        Dim n As Integer = PortdeCom.BytesToRead
        comBuffer = New Byte(n - 1) {}
        PortdeCom.Read(comBuffer, 0, n)
        Me.Invoke(UpdateFormDelegate1)
    End Sub

    Private Sub updateDisplay()

        If txt_recevoir.Text = " " Then
            txt_recevoir.Text = CStr(comBuffer(0))
        End If
    End Sub

End Class

8 réponses

Whismeril Messages postés 19012 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 12 avril 2024 654
8 juin 2016 à 18:48
Bonsoir

à vue de nez, ça a l'air bien.
Ça affiche quoi?
0