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 |
Partager