Option Strict On
Option Explicit On
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.IO.Ports

Public Class Rückantwort_Test_DataReceived

    Dim PufferString As String
    Private Delegate Sub DelegateSub()
    Private Datenanzeigen As New DelegateSub(AddressOf Anzeigen)

    Private Sub btnVerbinden_Click(sender As Object, e As System.EventArgs) Handles btnVerbinden.Click

        SerialPort1.PortName = "COM3"
        SerialPort1.Parity = Parity.None
        SerialPort1.BaudRate = 115200
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Open()
        liboTHG.Items.Add("Verbunden mit COM 3")

        btnVerbinden.Enabled = False
        btnTrennen.Enabled = True

    End Sub

    Private Sub btnTrennen_Click(sender As Object, e As System.EventArgs) Handles btnTrennen.Click

        SerialPort1.Close()
        liboTHG.Items.Add("COM 3 getrennt")

    End Sub

    Private Sub Rückantwort_Test_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        btnTrennen.Enabled = False

    End Sub

    Private Sub Rückantwort_Test_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        SerialPort1.Close()

    End Sub

    Private Sub btnLeeren_Click(sender As Object, e As System.EventArgs) Handles btnLeeren.Click

        liboTHG.Items.Clear()

    End Sub

    Private Sub btnVor_Click(sender As Object, e As System.EventArgs) Handles btnVor.Click

        Dim Takt As String = txtTakt.Text

        For i = 1 To 3 Step 1

            SerialPort1.Write("T0," & vbCr)
            SerialPort1.Write("L1,x" & Takt & vbCr)
            liboTHG.Items.Add("L1,x" & Takt)
            liboTHG.Items.Add("----------------------")

        Next

    End Sub

    Private Sub btnZurück_Click(sender As Object, e As System.EventArgs) Handles btnZurück.Click

        Dim Takt As String = txtTakt.Text

        SerialPort1.Write("T0," & vbCr)
        SerialPort1.Write("L1,x-" & Takt & vbCr)
        liboTHG.Items.Add("L1,x-" & Takt)
        liboTHG.Items.Add("----------------------")

    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
                                ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                Handles SerialPort1.DataReceived

        Dim enc As System.Text.Encoding = New System.Text.ASCIIEncoding()

        With SerialPort1

            Try
                ' Alle Bytes einzeln lesen
                For I As Integer = 1 To .BytesToRead
                    ' 1 Byte lesen
                    Dim ByteArray() As Byte = {CByte(.BaseStream.ReadByte)}
                    ' Array in String
                    PufferString = enc.GetString(ByteArray)
                    ' Delegaten aufrufen
                    Me.Invoke(Datenanzeigen)
                Next

            Catch ex As Exception

                MessageBox.Show(ex.Message, "Fehler beim Empfangen ...")

            End Try

        End With

    End Sub

    Private Sub Anzeigen()

        ' Text am Ende hinzufügen
        liboTHG.Items.Add(Asc(PufferString))
        liboTHG.Items.Add("----------------------")

    End Sub