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 :

Envois et reception d'une trame via Port Serie (com)


Sujet :

VB.NET

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    46
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2015
    Messages : 46
    Points : 0
    Points
    0
    Par défaut Envois et reception d'une trame via Port Serie (com)
    Bonjour tous le monde,

    Là je me retourne vers vous pour solliciter votre aide.
    Je suis bloqué dans mon projet, c'est la réception et l'envois d'une trame par le port série Com usb.
    Voila mon 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
    Imports System
    Imports System.IO.Ports
     
    Class PortDataReceived
        Public Shared Sub Main()
     
     
            Dim mySerialPort As New SerialPort("COM3")
     
            mySerialPort.BaudRate = 38400
            mySerialPort.Parity = Parity.None
            mySerialPort.StopBits = StopBits.One
            mySerialPort.DataBits = 8
            mySerialPort.Handshake = Handshake.None
     
            AddHandler mySerialPort.DataReceived, AddressOf DataReceivedHandler
     
            mySerialPort.Open()
     
            Console.WriteLine("Press any key to continue...")
            Console.WriteLine()
            Console.ReadKey()
            mySerialPort.Close()
        End Sub
     
        Private Shared Sub DataReceivedHandler(
                            sender As Object,
                            e As SerialDataReceivedEventArgs)
            Dim sp As SerialPort = CType(sender, SerialPort)
            Dim indata As String = sp.ReadExisting()
            Console.WriteLine("Data Received:")
            Console.Write(indata)
        End Sub
        Dim data As String
        Sub SendSerialData(ByVal data As String)
            ' Send strings to a serial port.
            Using com1 As IO.Ports.SerialPort =
                    My.Computer.Ports.OpenSerialPort("COM3")
                com1.WriteLine(data)
            End Using
        End Sub
    End Class
    la trame a recevoir et a envoyer est de cette manière:
    Le format d’interrogation/ réponse est :

    Adresse Commande Nb Données Donnée 1 Donnée 2 … Checksum

    Nbre d’octet par donnée : 2

    Ordre des données : poids fort en premier
    Vitesse de communication : 38400 bauds

    Merci d'avance pour votre aide.

  2. #2
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2007
    Messages
    758
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Février 2007
    Messages : 758
    Points : 279
    Points
    279
    Par défaut
    Pourquoi tu envoi ceci sur le port serie ?

    Console.WriteLine("Press any key to continue...")

    Et pourquoi tu écrit aussi dans le datareceived ???

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    46
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2015
    Messages : 46
    Points : 0
    Points
    0
    Par défaut
    Bonjour,
    Je tiens a vous remercier pour votre réponse. Oui là vous avez raison, j'ai rien a faire avec Console.WriteLine("Press any key to continue...")

    et écrir dans le datareceived, je les ai enlevé.

    Est ce que vous avez une autre solution a me proposer? Avec se que j'ai fait jusque là, j'ai arrivé a recevoir la trame qui est envoyée en hexadécimale en décimale.
    Et je veux aussi envoyer une trame mais malheureusement j'ai pas réussi.

    Merci pour votre réponse.

  4. #4
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2007
    Messages
    758
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Février 2007
    Messages : 758
    Points : 279
    Points
    279
    Par défaut
    voila un exemple de se que j'ai pu faire pour la communication sur le port série avec un modem GPRS :

    connexion avec le modem :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    'configuration du port RS232 COM B pour le modem GSM
            SerialPort1.BaudRate = 9600 'vitesse du port
            SerialPort1.Parity = IO.Ports.Parity.None 'pas de parité
            SerialPort1.StopBits = IO.Ports.StopBits.One 'un bit d'arrêt par octet
            SerialPort1.DataBits = 8 'nombre de bit par octet
            SerialPort1.RtsEnable = True 'ligne Rts désactivé
            SerialPort1.DtrEnable = True 'ligne Dtr désactivé
            SerialPort1.ReadTimeout = 800
            SerialPort1.WriteTimeout = 800
            SerialPort1.ReceivedBytesThreshold = 1
            SerialPort1.PortName = "COM2"
            SerialPort1.Open()
    On affiche les données recu par le port série dans une textbox avec cette fonction :
    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
     
        Private Delegate Sub _Affiche_ASCII_GSM(ByVal donnee As String)
     
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim nb_octet As Integer = SerialPort1.BytesToRead ' on récupère le nombre d'octet présent dans le tampon
            Dim trame(nb_octet - 1) As Byte ' Tableau acceuillant les données au format byte
     
            SerialPort1.Read(trame, 0, nb_octet) 'on lit le port
     
            Dim donnee As String = System.Text.Encoding.ASCII.GetString(trame) ' on récupère les données au format ASCII
            ecrire_txtbox1(donnee)
     
            'L'évènement DataReceived se situe sur un thread différent que le form, nous devons donc appelé un délégué pour afficher le résultat à l'écran
            If Me.InvokeRequired Then
                Me.Invoke(New _Affiche_ASCII_GSM(AddressOf Affiche_ASCII_GSM), donnee)
            End If
        End Sub
     
        Private Sub Affiche_ASCII_GSM(ByVal donnee As String)
            donnee_recu += donnee
            TextBox1.Text = donnee_recu 'affiche les données dans la textbox1
        End Sub
    Et enfin pour l'envoi de sms vers le modem on utilise la commande write :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        Public Sub envoi_message(ByVal numero As String)
            If Form1.SerialPort1.IsOpen = False Then
                MsgBox("Veuillez d'abord ouvrir le port COM")
                Exit Sub
            Else
                SerialPort1.WriteLine("AT+CMGS=" & numero & "test envoi un sms, veuillez accusé reception svp")
            End If
        End Sub
    j’espère que cet exemple peu vous aider

Discussions similaires

  1. envoyer une Trame via port com
    Par samirraoui dans le forum VB.NET
    Réponses: 9
    Dernier message: 21/05/2015, 21h31
  2. Envoyer une trame via port série
    Par Lacmé dans le forum API standards et tierces
    Réponses: 6
    Dernier message: 21/04/2015, 08h52
  3. Reception d'une trame avec port com (threads ?)
    Par vinowan dans le forum VB.NET
    Réponses: 11
    Dernier message: 13/10/2011, 11h26
  4. envoi et reception d'une trame sous rs232
    Par azert2010 dans le forum C++
    Réponses: 7
    Dernier message: 25/03/2011, 07h26

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