IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Problème réception trame Port Série


Sujet :

VB.NET

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Août 2013
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Août 2013
    Messages : 3
    Points : 1
    Points
    1
    Par défaut Problème réception trame Port Série
    Bonjour à tous,

    Je me permets de vous écrire pour vous demander de l'aide pour le projet que je suis en train de créer.
    En effet mon projet consiste à dialoguer avec ma centrale d'alarme sur port série RS232.
    Le but est d'interpréter la réception des trames reçues de la centrale. A ce jour j'ai créé un Windows form pour mes essais, j'arrive bien à dialoguer avec ma centrale c'est-à-dire que pour qu'elle me réponde il faut que je lui envoie une trame avec un code intégré puis celle-ci me répond, le problème est que dans le SerialPort1_DataReceived avec SerialPort1.ReadExisting je peux interpréter la réponse à l'initialisation de ma centrale mais si je renvoie une nouvelle trame pour par exemple lui demander si mon système est armé je n'arrive pas à récupérer cette trame pour ensuite arrêter un service.
    Le but après sera de créer un service Windows qui gérera automatiquement les données reçues de ma centrale: interrogation en permanence de la centrale pour avoir l'état de marche arrêt, si système à l'arrêt alors service Y suspendu et si système en marche alors service Y démarré.
    Voici le code que j'utilise :

    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
    Imports System
    Imports System.Threading
    Imports System.IO.Ports
    Imports System.ComponentModel
    Imports System.Text
    Imports System.ServiceProcess
     
     
     
    Public Class Form1
     
        <BrowsableAttribute(True)> _
        Public Property Handshake As Handshake
        Delegate Sub SetTextCallback(ByVal [text] As String)
        Dim readbuffer As String
        Dim sc As New ServiceController("ivsVideoServerService")
        Dim holder As String
     
     
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
            With SerialPort1
                .PortName = "COM1"
                .BaudRate = 4800
            End With
            SerialPort1.DtrEnable = True
            Button2.Enabled = False
     
     
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
            SerialPort1.Open()
            If SerialPort1.IsOpen Then
                SerialPort1.Close()
                SerialPort1.Open()
            End If
            Button1.Enabled = False
            Button2.Enabled = True
            Button4.Enabled = True
     
        End Sub
        '------------------------------------------------
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            SerialPort1.Encoding = System.Text.Encoding.GetEncoding(28591)
            SerialPort1.Write(Init)
            ' Thread.Sleep(3000)
            ' SerialPort1.Write(DemandeEtatGroup)
     
        End Sub
     
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            SerialPort1.Close()
            Button1.Enabled = True
            Button2.Enabled = False
            Button4.Enabled = False
        End Sub
     
        Private Init() As Char = (Chr(94) & Chr(94) & Chr(94) & Chr(94) & Chr(32) & Chr(0) & Chr(1) & Chr(6) & Chr(1) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(215) & Chr(112))
        Private RepInit() As Char = (Chr(94) & Chr(65) & Chr(0) & Chr(49) & Chr(224))
        Private DemandeEtatGroup() As Char = (Chr(94) & Chr(32) & Chr(0) & Chr(96) & Chr(2) & Chr(1) & Chr(1) & Chr(120) & Chr(235))
        Private G1arme() As Char = (Chr(94) & Chr(64) & Chr(0) & Chr(97) & Chr(2) & Chr(1) & Chr(128) & Chr(176))
        Private G1desarme() As Char = (Chr(94) & Chr(64) & Chr(0) & Chr(97) & Chr(2) & Chr(1) & Chr(0) & Chr(177) & Chr(119))
     
     
        Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim tramerecu As String
            tramerecu = SerialPort1.ReadExisting()
            Thread.Sleep(3000)
                If tramerecu = RepInit Then
                SerialPort1.Write(DemandeEtatGroup)
            Else : SerialPort1.Write(DemandeEtatGroup)
            End If
            Thread.Sleep(3000)
                If tramerecu = G1desarme Then
                    sc.Pause()
                End If
            Thread.Sleep(3000)
                If tramerecu = G1arme Then
                    sc.Continue()
                End If
        End Sub
     
     
     
    End Class
    Merci d'avance pour votre aide

  2. #2
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2013
    Messages
    235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 235
    Points : 359
    Points
    359
    Par défaut
    Bonjour,

    Pour moi ton projet est à refaire.
    Peux-tu par exemple expliquer ces lignes?:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    If tramerecu = RepInit Then
       SerialPort1.Write(DemandeEtatGroup)
    Else : SerialPort1.Write(DemandeEtatGroup)
    End If
    '______________
    SerialPort1.Open()
    If SerialPort1.IsOpen Then
       SerialPort1.Close()
       SerialPort1.Open()
    End If
    Essaye sur un nouveau projet, sur la form1 tu insères un bouton, un timer, et un serialport, puis colle ce 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
     
    Option Explicit On
    Option Strict On
     
    Imports System
    Imports System.Threading
    Imports System.IO.Ports
    Imports System.ComponentModel
    Imports System.Text
    Imports System.ServiceProcess
     
    Public Class Form1
        Dim Init As String = (Chr(94) & Chr(94) & Chr(94) & Chr(94) & Chr(32) & Chr(0) & Chr(1) & Chr(6) & Chr(1) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(215) & Chr(112))
        Dim RepInit As String = (Chr(94) & Chr(65) & Chr(0) & Chr(49) & Chr(224))
        Dim DemandeEtatGroup As String = (Chr(94) & Chr(32) & Chr(0) & Chr(96) & Chr(2) & Chr(1) & Chr(1) & Chr(120) & Chr(235))
        Dim G1arme As String = (Chr(94) & Chr(64) & Chr(0) & Chr(97) & Chr(2) & Chr(1) & Chr(128) & Chr(176))
        Dim G1desarme As String = (Chr(94) & Chr(64) & Chr(0) & Chr(97) & Chr(2) & Chr(1) & Chr(0) & Chr(177) & Chr(119))
        Dim sc As New ServiceController("ivsVideoServerService")
     
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Try
                SerialPort1.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 200
            Timer1.Enabled = False
            With SerialPort1
                .PortName = "COM1"
                .BaudRate = 4800
                .StopBits = CType(1, StopBits)
                .DataBits = 8
                .Parity = IO.Ports.Parity.None
            End With
            Try
                SerialPort1.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
     
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Timer1.Enabled = True
        End Sub
     
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            Dim TrameRecu As String = SerialPort1.ReadExisting()
            Traitement(TrameRecu)
        End Sub
     
        Private Sub Traitement(ByVal Recu As String)
            Me.Text = "Reçu : " & Recu
     
            'Ici je doute de l'éfficacité de ton code
            If Recu = RepInit Then SerialPort1.Write(DemandeEtatGroup)
     
            Select Case Recu
                Case G1desarme
                    sc.pause()
                Case G1arme
                    sc.continue()
            End Select
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort1.Write(Init)
        End Sub
    End Class
    Bien sur que je n'ai pas testé car je ne trouve aucune centrale d'alarme sous la mais en ce moment...
    Par nature les mots, ils sont flous, c'est une fois alignés qu'ils se précisent.

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Août 2013
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Août 2013
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Salut ACIVE CS merci de ta réponse alors en fait tu vas te moquer de moi,
    j'avais écris cela pour avoir une redondance de demande état de groupe suite à la réponse de l'initialisation.
    Ensuite pour l'histoire du port ouvert fermé j'ai vu quelque code où il fermait le port puis le ré ouvrait.
    En ce qui concerne ton code je vais l'essayer de suite et te tiens au courant, encore merci à toi.

  4. #4
    Nouveau Candidat au Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Août 2013
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Août 2013
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Re,
    alors en fait j'ai essayé ton code le problème que je rencontre est que la centrale ne me répond pas.
    En parallèle j'utilise "drive monitor" pour voir les échanges de trames et la seule façon pour qu'elle me réponde c'est de mettre dans serialport1_datareceived quelque chose comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Dim tr As String = SerialPort1.ReadExisting
            If tr = RepInit Then
                SerialPort1.Write(DemandeEtatGroup)
    Aurais tu une solution ?

Discussions similaires

  1. [C#]Problème avec le port série sous Win98
    Par Polux63 dans le forum Windows Forms
    Réponses: 6
    Dernier message: 29/09/2006, 09h14
  2. problème ouverture de port série
    Par philippe13 dans le forum Entrée/Sortie
    Réponses: 9
    Dernier message: 26/04/2006, 16h42
  3. Emission / Réception par port série
    Par odSen dans le forum C
    Réponses: 28
    Dernier message: 06/01/2006, 18h45
  4. [Débutant] Réception sur port série
    Par Tophe59 dans le forum Langage
    Réponses: 43
    Dernier message: 28/06/2004, 11h04
  5. Problème avec le port série sous Windows XP
    Par didou2dek dans le forum Composants VCL
    Réponses: 6
    Dernier message: 02/09/2003, 19h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo