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
| Option Explicit On
Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.Text
Imports Microsoft.VisualBasic
Public Class Form1
Private MonPort As New PortSerie
Dim returnStr As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoginForm1.ShowDialog()
Me.Text = "Terminal VIZ 100"
Me.WindowState = FormWindowState.Maximized
Send_Num.Enabled = False
Num_serie_recup.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.ShowDialog()
End Sub
Private Sub Send_Num_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send_Num.Click
ProgressBar1.Value = 0
SendSerialData(Form2.N_Serie)
End Sub
Private Sub Receive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Receive.Click
ProgressBar1.Value = 0
ReceiveSerialData()
End Sub
Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 200
End Sub
Sub SendSerialData(ByVal data As String)
MonPort.configuration()
Using PortSerie As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
MonPort.ouverture()
PortSerie.WriteLine(data)
ProgressBar1.Value += 100
PortSerie.Close()
ProgressBar1.Value += 100
MonPort.fermeture()
End Using
End Sub
Function ReceiveSerialData() As String
MonPort.configuration()
Using PortSerie As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
MonPort.ouverture()
If (PortSerie.IsOpen()) Then
Do
MsgBox("Ouverture réussi", MsgBoxStyle.Information)
ProgressBar1.Value = 70
Dim Incoming As String = PortSerie.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
ProgressBar1.Value = 120
Loop
End If
ProgressBar1.Value = 150
MonPort.fermeture()
End Using
Return returnStr
ProgressBar1.Value = 200
End Function
End Class
#Region "Classe Port Série"
Public Class PortSerie
Public NoPort As String = "COM1" 'Numero de port qui sera utilisé
Public Debit As Integer = 9600 'debit en bits/sec sur le port COM
Public NbBits As Integer = 8 'bits de donnée
Public BitStop As String = IO.Ports.StopBits.One 'bit de stop
Public Parite As String = IO.Ports.Parity.None 'parité
Dim i As Integer
Function configuration() As Boolean
Form1.SerialPort.PortName = NoPort
Form1.SerialPort.BaudRate = Debit
Form1.SerialPort.DataBits = NbBits
Form1.SerialPort.StopBits = BitStop
Form1.SerialPort.Parity = Parite
End Function
Function ouverture() As Boolean
' verifie que le port est fermé avant de l'ouvrir
If (Not (Form1.SerialPort.IsOpen) Or (NoPort = "NULL")) Then
Form1.SerialPort.Open()
End If
ouverture = EtatPort()
End Function
Function fermeture() As Boolean
' verifie que le port est ouvert avant de le fermer
If (Form1.SerialPort.IsOpen()) Then
Form1.SerialPort.Close()
End If
fermeture = EtatPort()
End Function
Function EtatPort() As Boolean
' test le port et retourn 0 ou 1
If (Form1.SerialPort.IsOpen()) Then
EtatPort = True
Else
EtatPort = False
End If
End Function
End Class
#End Region |
Partager