1 | Public Class Form1
|
2 |
|
3 | Dim StrBufferOut As String
|
4 | Dim StrBufferIn As String
|
5 |
|
6 | Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
|
7 | Exit_Program()
|
8 | End Sub
|
9 |
|
10 | Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
11 | tb_Status.Text = "Nicht verbunden"
|
12 | StrBufferOut = ""
|
13 | StrBufferIn = ""
|
14 | btn_Connect.Enabled = False
|
15 | btn_SendData.Enabled = False
|
16 | tmr_Timer.Enabled = False
|
17 | cbo_Ports.Enabled = False
|
18 | cbo_BaudRate.Enabled = False
|
19 | tb_BufferOut.Enabled = False
|
20 | tb_BufferIn.Enabled = False
|
21 | End Sub
|
22 |
|
23 | Private Sub btn_GetPorts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_GetPorts.Click
|
24 | cbo_Ports.Items.Clear()
|
25 |
|
26 | For Each FreePorts As String In My.Computer.Ports.SerialPortNames
|
27 | cbo_Ports.Items.Add(FreePorts)
|
28 | Next
|
29 |
|
30 | If cbo_Ports.Items.Count > 0 Then
|
31 | tb_Status.Text = "Mindestens ein freier Port verfügbar"
|
32 | btn_Connect.Enabled = True
|
33 | cbo_Ports.Text = cbo_Ports.Items(0)
|
34 | cbo_BaudRate.Text = cbo_BaudRate.Items(5)
|
35 | cbo_Ports.Enabled = True
|
36 | cbo_BaudRate.Enabled = True
|
37 | Else
|
38 | tb_Status.Text = "Kein Port verfügbar"
|
39 | cbo_Ports.Items.Clear()
|
40 | btn_Connect.Enabled = False
|
41 | btn_SendData.Enabled = False
|
42 | cbo_Ports.Enabled = False
|
43 | cbo_BaudRate.Enabled = False
|
44 | tb_BufferOut.Enabled = False
|
45 | tb_BufferIn.Enabled = False
|
46 | End If
|
47 | End Sub
|
48 |
|
49 | Private Sub btn_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Connect.Click
|
50 | If btn_Connect.Text = "Verbinden" Then
|
51 | Try
|
52 | sp_SerialPort.PortName = cbo_Ports.Text
|
53 | sp_SerialPort.BaudRate = cbo_BaudRate.Text
|
54 | sp_SerialPort.Open()
|
55 | tmr_Timer.Enabled = True
|
56 | btn_SendData.Enabled = True
|
57 | btn_Connect.Text = "Trennen"
|
58 | btn_GetPorts.Enabled = False
|
59 | cbo_Ports.Enabled = False
|
60 | cbo_BaudRate.Enabled = False
|
61 | tb_BufferOut.Enabled = True
|
62 | tb_BufferIn.Enabled = True
|
63 | tb_Status.Text = "Verbindung mit " & cbo_Ports.Text & " hergestellt. Übertragungsrate: " & cbo_BaudRate.Text & " Baud"
|
64 | Catch ex As Exception
|
65 | tb_Status.Text = "Die Verbindung konnte nicht hergestellt werden"
|
66 | End Try
|
67 | ElseIf btn_Connect.Text = "Trennen" Then
|
68 | tb_Status.Text = "Verbindung getrennt"
|
69 | sp_SerialPort.Close()
|
70 | tmr_Timer.Enabled = False
|
71 | btn_SendData.Enabled = False
|
72 | btn_Connect.Text = "Verbinden"
|
73 | btn_GetPorts.Enabled = True
|
74 | cbo_Ports.Enabled = True
|
75 | cbo_BaudRate.Enabled = True
|
76 | tb_BufferOut.Enabled = False
|
77 | tb_BufferIn.Enabled = False
|
78 | End If
|
79 | End Sub
|
80 |
|
81 | Private Sub btn_SendData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SendData.Click
|
82 | sp_SerialPort.DiscardOutBuffer()
|
83 | StrBufferOut = tb_BufferOut.Text
|
84 | sp_SerialPort.Write(StrBufferOut)
|
85 | End Sub
|
86 |
|
87 | Private Sub tmr_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_Timer.Tick
|
88 | StrBufferIn = sp_SerialPort.ReadExisting
|
89 |
|
90 | If StrBufferIn <> "" Then
|
91 | tb_BufferIn.Text = StrBufferIn
|
92 | sp_SerialPort.DiscardInBuffer()
|
93 | End If
|
94 | End Sub
|
95 |
|
96 | Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click
|
97 | Exit_Program()
|
98 | End Sub
|
99 |
|
100 | Private Sub Exit_Program()
|
101 | If sp_SerialPort.IsOpen = True Then
|
102 | Dim Message As String = "Es besteht eine aktive Verbindung mit " & cbo_Ports.Text & ". Trotzdem beenden?"
|
103 | Dim Caption As String = "Aktive Verbindung"
|
104 | Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
|
105 | Dim Result As DialogResult
|
106 | Result = MessageBox.Show(Message, Caption, Buttons)
|
107 |
|
108 | If Result = DialogResult.Yes Then
|
109 | tmr_Timer.Enabled = False
|
110 | sp_SerialPort.Close()
|
111 | Me.Close()
|
112 | End If
|
113 | Else
|
114 | Me.Close()
|
115 | End If
|
116 | End Sub
|
117 |
|
118 | End Class
|