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 :

envoi par rs232 VB.NET


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 19
    Par défaut envoi par rs232 VB.NET
    bjr à tous,
    svp j'ai besoin de votre aide pour achevé mon mini projet,(VB.NET).
    le projet consiste a envoyer un fichier soit .txt ou excel qui contient des coordonnées X,Y par le port serie rs232 et a la reception ontrouve ces coordonnées sous forme d'un graphe représenté sur un repère.
    j'ai commencé par ce code là qui permet de dessiner les axes en cliquant sur un bouton et dessiner la courbe en cliquant sur un autre:
    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
    Public Class Courbe 
     
    Private Sub Courbe_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize 
    PictureBox1.Size = Me.Size 
     
    End Sub 
     
    Private Sub QuitterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitterToolStripMenuItem.Click 
    End 
    End Sub 
     
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click 
    Dim g As Graphics 
    g = PictureBox1.CreateGraphics 
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 
    Dim stylo As Pen = Pens.Black 
    g.DrawLine(stylo, 20, 30, 200, 300) 
    'stylo.Dispose() 
    'g.Dispose() 
     
    End Sub 
     
    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click 
    PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height) 
    Dim myBitmap1 As Bitmap = PictureBox1.Image 
    Dim g As Graphics = Graphics.FromImage(myBitmap1) 
    g.PageUnit = GraphicsUnit.Millimeter 
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 
    'Dim stylo As Pen = Pens.Red 
    'g.DrawLine(stylo, 50, 40, 200, 300) 
    'stylo.Dispose() 
     
     
    Axe_H(g, 10, 150) 
    g.Dispose() 
     
    End Sub 
     
     
    Private Sub Axe_H(ByRef g As Graphics, ByVal x0 As Single, ByVal y0 As Single) 
    Dim stylo As Pen = New Pen(Color.Blue, 0.1F) 
    Dim stylo2 As Pen = New Pen(Color.Red, 0.1F) 
    Dim Fnt As Font = New Font("MS Sans Serif", 8.25F) 
     
     
    g.DrawLine(stylo, x0, y0, x0 + 200, y0) 
     
    For j = 0 To 100 
    g.DrawLine(stylo, x0 + j, y0, x0 + j, y0 + 3) 
    Next 
     
     
     
    For j = 0 To 100 Step 10 
    g.DrawLine(stylo2, x0 + j, y0, x0 + j, y0 + 5) 
    g.DrawString(Str(j / 10), Fnt, Brushes.Black, x0 + j - Fnt.GetHeight(g) / 2, y0 + 6) 
    Next 
     
     
    End Sub 
     
    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click 
    Dim x1 As Single = 200 * Rnd() 
    Dim y1 As Single = 150 * Rnd() 
    ' PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height) 
    Dim myBitmap1 As Bitmap = PictureBox1.Image 
    Dim g As Graphics = Graphics.FromImage(myBitmap1) 
    g.PageUnit = GraphicsUnit.Millimeter 
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 
    g.DrawRectangle(Pens.Green, x1, y1, 0.1F, 0.1F) 
    g.Dispose() 
    PictureBox1.Refresh() 
    End Sub 
     
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 
    e.Graphics.PageUnit = GraphicsUnit.Millimeter 
    Axe_H(e.Graphics, 10, 150) 
    End Sub 
     
    Private Sub ImprimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImprimerToolStripMenuItem.Click 
    PrintDocument1.Print() 
    End Sub 
    End Class
    mon probléme est comment dessiner à partir des coordonnées et comment accéder à eux dans un fichier txt et les utiliser..
    J'ai un autre problème c'est que j'ai le code de transfert par port série et je sais pas comment faire le lien entre le reste de 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
    101
    Public Class Form1
     
    Dim WithEvents serialPort As New IO.Ports.SerialPort
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
    For i As Integer = 0 To _
    My.Computer.Ports.SerialPortNames.Count - 1
    cbbCOMPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
    Next
    If cbbCOMPorts.Items.Count <> 0 Then cbbCOMPorts.SelectedIndex = 0
     
    btnDisconnect.Enabled = False
     
    End Sub
     
     
    '-------------------------------------------
    ' Event handler for the Connect button
    '-------------------------------------------
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
     
    If serialPort.IsOpen Then
    serialPort.Close()
    End If
     
    Try
    With serialPort
    .PortName = cbbCOMPorts.Text
    .BaudRate = 9600
    .Parity = IO.Ports.Parity.None
    .DataBits = 8
    .StopBits = IO.Ports.StopBits.One
    End With
    serialPort.Open()
     
    lblMessage.Text = cbbCOMPorts.Text & " connected."
    btnConnect.Enabled = False
    btnDisconnect.Enabled = True
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    End Sub
     
    '-------------------------------------------
    ' Event handler for the Disconnect button
    '-------------------------------------------
    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
    Try
    serialPort.Close()
    lblMessage.Text = serialPort.PortName & " disconnected."
    btnConnect.Enabled = True
    btnDisconnect.Enabled = False
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    End Sub
     
    '-------------------------------------------
    ' Event handler for the Send button
    '-------------------------------------------
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    Try
    serialPort.Write(txtDataToSend.Text & vbCrLf)
    With txtDataReceived
    .SelectionColor = Color.Black
    .AppendText(txtDataToSend.Text & vbCrLf)
    .ScrollToCaret()
    End With
    txtDataToSend.Text = String.Empty
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    End Sub
     
    '-------------------------------------------
    ' Event handler for the DataReceived
    '-------------------------------------------
    Private Sub DataReceived( _
    ByVal sender As Object, _
    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
    Handles serialPort.DataReceived
     
    txtDataReceived.Invoke(New _
    myDelegate(AddressOf updateTextBox), _
    New Object() {})
    End Sub
     
    '------------------------------------------------------
    ' Delegate and subroutine to update the Textbox control
    '------------------------------------------------------
    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
    With txtDataReceived
    .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
    .SelectionColor = Color.Red
    .AppendText(serialPort.ReadExisting)
    .ScrollToCaret()
    End With
    End Sub
     
    End Class
    j'aime aussi trouver un logiciel qui fait une liaison série virtuel pour tester tout ca.

    je suis débutant en programmation et j'ai besoin de votre aide
    merci

  2. #2
    Membre Expert Avatar de hunteshiva
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Février 2010
    Messages
    1 069
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Chef de projet en SSII
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2010
    Messages : 1 069
    Par défaut
    débutant en programmation
    ne t'excuse pas de toutes les fautes que tu fait
    *sa donne mal a la tête rien qu'a essayer de lire ton topic*

    en plus de sa ton code est pas aligné, alors dur de voir clair dans tout sa ...

    déja pour lire/écrire dans un fichier texte, tu peut aller regarde ce topic, j'y est mis l'idée *a toi de l'adapter*

    tu a déja un fichier texte ?

    et l'autre question est de savoir pourquoi tu veut utiliser un port RS232
    envoyer des données?? recevoir des données??
    * même si le nom du sujet le dit, toi tu dis pas pourquoi tu a besoin d'envoyer et surtout qu'es ce que tu a besoin d'envoyer*

  3. #3
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 19
    Par défaut
    bonjour
    j'ai terminé mon mini projet.
    j'ai utilisé un fichier texte qui contient des des coordonnées.
    à partir de l'interface que j'ai conçu je peut les afficher et les envoyer point par point et à la sortie ils sont dessinées sur un repère.
    si vous voulez je peut l'uploader!!
    à+

  4. #4
    Membre Expert Avatar de hunteshiva
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Février 2010
    Messages
    1 069
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Chef de projet en SSII
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2010
    Messages : 1 069
    Par défaut
    oublie pas

  5. #5
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 19
    Par défaut
    slt
    voici le 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
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    Imports System.Threading
    Imports System.IO.Ports
    Imports System.Text
    Imports VB = Microsoft.VisualBasic
    Public Class Form1
        Dim WithEvents port As New IO.Ports.SerialPort
        'WithEvents Indique qu'une ou plusieurs variables membres déclarées font référence à une instance d'une classe qui peut déclencher des événements
        Dim x0, y0 As Single
        Dim Echelle As Single = 10.0F
        Dim Debx As Single = 0
        Dim Finx As Single = 30
        Dim Deby As Single = 0
        Dim finy As Single = 6
        Dim chaine As String
        Dim Nom_Fichier As String
     
     
     
        Public Delegate Sub myDelegate()
     
        Public Sub updateTextBox()
            Dim abscisse As String
            Dim ordonne As String
            Dim coordonne As Object
     
            chaine = port.ReadExisting()
     
            coordonne = Split(chaine, " ")
            abscisse = coordonne(0)
            ordonne = coordonne(1)
            Dim row() As String = {abscisse, ordonne}
     
            With txtDataReceived
                .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
                .SelectionColor = Color.Red
                .Text = chaine 'Ajoute du texte au texte en cours dans une zone de texte.
                .ScrollToCaret() ' Fait défiler le contenu du contrôle vers la position indiquée par le signe insertion.
            End With
            Dim x1 As Single = CSng(abscisse)
            Dim y1 As Single = CSng(ordonne)
            Dim myBitmap1 As Bitmap = PictureBox1.Image
            Dim g As Graphics = Graphics.FromImage(myBitmap1)
            g.PageUnit = GraphicsUnit.Millimeter
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            g.DrawRectangle(Pens.Green, x1 * 10 + 20, 80 - y1 * 10, 0.1F, 0.1F)
            g.Dispose()
            PictureBox1.Refresh()
     
     
     
        End Sub
     
        Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
            'On lit le text réçu
            txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
            'Exécute le délégué spécifié sur le thread qui détient le handle de fenêtre sous-jacent du contrôle, avec la liste d'arguments spécifiée
        End Sub
     
     
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
                '  collecter les noms des ports séries existants dans l'ordinateur et les affiche dans le ComboBox control:
                ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
     
            Next
     
            'On disactive le bouton fermer lors de l'ouverture du port série
            PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
            Dim myBitmap1 As Bitmap = PictureBox1.Image
            Dim g As Graphics = Graphics.FromImage(myBitmap1)
            g.PageUnit = GraphicsUnit.Millimeter
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            Axe_H(g)
            Axe_V(g)
        End Sub
     
        Private Sub Axe_H(ByRef g As Graphics)
            Dim stylo As Pen = New Pen(Color.Black, 0.1F)
            Dim stylo2 As Pen = New Pen(Color.Red, 0.1F)
            Dim Fnt As Font = New Font("MS Sans Serif", 8.25F)
            g.DrawLine(stylo, 0, 80, 500, 80)
     
            For j = 0 To 900
                g.DrawLine(stylo, j, 80, j, 81)
            Next
            For j = 0 To 900 Step 10
                g.DrawLine(stylo2, j, 80, j, 82)
     
            Next
            For j = 0 To 900 Step 10
                g.DrawString(Str((j - 20) / 10), Fnt, Brushes.Blue, j - Fnt.GetHeight(g) / 2, 82)
            Next
     
        End Sub
     
        Private Sub Axe_V(ByRef g As Graphics)
            Dim stylo As Pen = New Pen(Color.Black, 0.1F)
            Dim stylo2 As Pen = New Pen(Color.Red, 0.1F)
            Dim Fnt As Font = New Font("MS Sans Serif", 8.25F)
            g.DrawLine(stylo, 20, 0, 20, 80)
            For j = 0 To 80
                g.DrawLine(stylo, 19, j, 20, j)
            Next
     
            For j = 0 To 80 Step 10
                g.DrawLine(stylo2, 18, j, 20, j)
     
            Next
            For j = 0 To 80 Step 10
                g.DrawString(Str(-(j - 80) / 10), Fnt, Brushes.Blue, 15, j - Fnt.GetHeight(g) / 2)
            Next
     
     
        End Sub
     
        Private Sub btnConnect_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
            'test d'etat du port
            If port.IsOpen Then
                port.Close()
            End If
     
            'configuration du port
            Try
                With port
                    .PortName = ComboBox1.Text
                    .BaudRate = 9600
                    .Parity = System.IO.Ports.Parity.None
                    .DataBits = 8
                    .StopBits = System.IO.Ports.StopBits.One
                End With
                port.Open()
                Label4.Text = ComboBox1.Text + " est ouvert"
     
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            port.Write(RichTextBox1.Text)
     
        End Sub
     
     
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Nom_Fichier = My.Application.Info.DirectoryPath & "\cord.txt"
            Lecture()
        End Sub
     
        Private Sub Lecture()
            Dim ord As String
            Dim abs As String
            Dim pos1 As String
            Dim pos0 As Object
            Dim ch As String
            FileOpen(1, Nom_Fichier, OpenMode.Input)
            Do While Not EOF(1)
                ch = LineInput(1)
                pos0 = InStr(1, ch, vbTab)
                pos1 = InStr(pos0 + 1, ch, vbTab)
                abs = Mid(ch, 1, pos0 - 1)
                ord = Mid(ch, pos1 + 1, Len(ch) - pos1)
                cb1.Items.Add(ord)
            Loop
            FileClose(1)
            If cb1.Items.Count <> 0 Then cb1.SelectedIndex = 0
     
        End Sub
     
     
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            RichTextBox1.Text = cb1.Text
        End Sub
    End Class
    n'oublie pas:
    *de mettre le fichier texte ci joint dans le bin\debug
    *les coordonnées d'un point à envoyer doivent être sepparés par un espace et non pas une tabulation
    remarque:
    Il y quelques point à régler dans ce mini projet, ce travail n'est pas totalement achevé
    n'oubliez pas de noter cette conversattion
    Fichiers attachés Fichiers attachés

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [LabVIEW 8.6] Problème envoi commande par RS232
    Par K-RK-S dans le forum LabVIEW
    Réponses: 0
    Dernier message: 30/03/2009, 09h13
  2. Probleme envoi/reception de fichiers par RS232
    Par zezitinho dans le forum C++
    Réponses: 2
    Dernier message: 10/10/2007, 10h34
  3. [Mail] envoi par mail : pb de point d'exclamation
    Par Faure dans le forum Langage
    Réponses: 12
    Dernier message: 20/03/2006, 12h02
  4. Problème d'envoi par formulaire
    Par k o D dans le forum Langage
    Réponses: 3
    Dernier message: 29/12/2005, 18h31
  5. Programmer un automate par RS232
    Par wael khalil dans le forum Langage
    Réponses: 6
    Dernier message: 25/08/2005, 16h02

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