Imports System.Threading

Public Class Form1
    Dim i As Integer = 0
    Dim mul As Integer = 0
    Dim frase As String = ""
    Dim WithEvents SerialPort As New IO.Ports.SerialPort
    Dim selPort As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MaximizeBox = False
        TextBox1.TextAlign = HorizontalAlignment.Center
        TextBox2.TextAlign = HorizontalAlignment.Center
        TextBox3.TextAlign = HorizontalAlignment.Center
        TextBox5.TextAlign = HorizontalAlignment.Center
        TextBox6.TextAlign = HorizontalAlignment.Center
        TextBox7.TextAlign = HorizontalAlignment.Center
        TextBox8.TextAlign = HorizontalAlignment.Center
        TextBox9.TextAlign = HorizontalAlignment.Center
        TextBox10.TextAlign = HorizontalAlignment.Center
        TextBox11.TextAlign = HorizontalAlignment.Center
        TextBox12.TextAlign = HorizontalAlignment.Center
        TextBox13.TextAlign = HorizontalAlignment.Center
        RefreshPorts()
        SPSetup()
    End Sub
    Private Sub RefreshPorts()
        txtPort.Items.Clear()
        For Each sp As String In My.Computer.Ports.SerialPortNames
            txtPort.Items.Add(sp)
        Next

        If txtPort.Items.Count > 0 Then
            txtPort.SelectedIndex = 0
            selPort = txtPort.Text
        End If

    End Sub
    Public Sub SPSetup()    'Serial Port Setup
        On Error Resume Next
        If SerialPort.IsOpen Then
            SerialPort.Close()
        End If
        SerialPort.PortName = selPort  ' "COM3"
        SerialPort.BaudRate = 9600
        SerialPort.DataBits = 8
        SerialPort.StopBits = IO.Ports.StopBits.One
        SerialPort.Handshake = IO.Ports.Handshake.None
        SerialPort.Parity = IO.Ports.Parity.None
        SerialPort.Open()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If (TextBox4.Text <> "") Then
            i = 0
            TextBox4.Enabled = False
            Button2.Enabled = False
            Button1.Enabled = True
            frase = TextBox4.Text
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If (i < frase.Length) Then
            Dim c = Asc(frase(i))
            TextBox13.Text = frase(i)
            i = i + 1
            Dim binary As String = Convert.ToString(c, 2)
            TextBox5.Text = 0
            TextBox6.Text = binary(0)
            TextBox7.Text = binary(1)
            TextBox8.Text = binary(2)
            TextBox9.Text = binary(3)
            TextBox10.Text = binary(4)
            TextBox11.Text = binary(5)
            TextBox12.Text = binary(6)
            'multiplex
            Button1.Enabled = False
            Button3.Enabled = True
            Send_data("d" & binary)
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Select Case mul
            Case 0
                TextBox1.Text = 0
                TextBox2.Text = 0
                TextBox3.Text = 0
                TextBox12.BackColor = Color.Red
                Send_data("m000")
            Case 1
                TextBox1.Text = 1
                TextBox2.Text = 0
                TextBox3.Text = 0
                TextBox12.BackColor = Color.LightGray
                TextBox11.BackColor = Color.Red
                Send_data("m100")
            Case 2
                TextBox1.Text = 0
                TextBox2.Text = 1
                TextBox3.Text = 0
                TextBox11.BackColor = Color.LightGray
                TextBox10.BackColor = Color.Red
                Send_data("m010")
            Case 3
                TextBox1.Text = 1
                TextBox2.Text = 1
                TextBox3.Text = 0
                TextBox10.BackColor = Color.LightGray
                TextBox9.BackColor = Color.Red
                Send_data("m110")
            Case 4
                TextBox1.Text = 0
                TextBox2.Text = 0
                TextBox3.Text = 1
                TextBox9.BackColor = Color.LightGray
                TextBox8.BackColor = Color.Red
                Send_data("m001")
            Case 5
                TextBox1.Text = 1
                TextBox2.Text = 0
                TextBox3.Text = 1
                TextBox8.BackColor = Color.LightGray
                TextBox7.BackColor = Color.Red
                Send_data("m101")
            Case 6
                TextBox1.Text = 0
                TextBox2.Text = 1
                TextBox3.Text = 1
                TextBox7.BackColor = Color.LightGray
                TextBox6.BackColor = Color.Red
                Send_data("m011")
            Case 7
                TextBox1.Text = 1
                TextBox2.Text = 1
                TextBox3.Text = 1
                Send_data("m111")
                TextBox6.BackColor = Color.LightGray
                TextBox5.BackColor = Color.Red
            Case Else
                mul = -1
                Button1.Enabled = True
                Button3.Enabled = False
                TextBox5.BackColor = Color.LightGray
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                If (i >= frase.Length) Then
                    TextBox4.Enabled = True
                    Button2.Enabled = True
                    Button1.Enabled = False
                    Button3.Enabled = False
                    TextBox5.Text = ""
                    TextBox6.Text = ""
                    TextBox7.Text = ""
                    TextBox8.Text = ""
                    TextBox9.Text = ""
                    TextBox10.Text = ""
                    TextBox11.Text = ""
                    TextBox12.Text = ""
                    TextBox13.Text = ""
                End If
        End Select
        mul = mul + 1
    End Sub
    Private Sub txtPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles txtPort.SelectedIndexChanged
        selPort = txtPort.Text
        SPSetup()
    End Sub
    Private Sub Send_data(data As String)
        Try
            If Not SerialPort.IsOpen Then SPSetup()
            'For cont As Integer = 0 To data.Length() - 1
            SerialPort.Write(data)
            'Next
        Catch
            MsgBox("Errore di connessione")
        End Try
    End Sub
    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myD1 As New myMethodDelegate(AddressOf myShowStringMethod)
    Sub myShowStringMethod(ByVal myString As String)

    End Sub
    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
        Dim str As String = SerialPort.ReadExisting
        Invoke(myD1, str)
    End Sub


End Class
