Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Threading
Imports System
Imports Snap7
Public Class Form1
    Dim AppSettings = ConfigurationManager.AppSettings
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Dim ClientS7 As Snap7.S7Client

    Dim ThrendPLCrw As Threading.Thread
    Dim ThrendMainProcess As Threading.Thread

    Delegate Sub UpdateFormDelegate()
    Public UpdateForm As UpdateFormDelegate

    Dim FromPLCClock1S, FromPLCAttPalletM30 As Boolean
    Dim Plcstatus As Integer

    Dim semthread As New Object

    Private Structure StructFromPLCM30
        Public NPallet As Integer
        Public Svuotalinea As Boolean

        Public DPNRicetta As Integer
        Public DPGuida As Integer
        Public DPTipoavvitatura As Integer
        Public DPPrisma As Integer
        Public DPPcb As String
        Public DPMostrina As String
        Public DPBriglia As String
        Public DPFiltroluce As Integer
        Public DPFiltroMY21 As Integer

        Public PRNRicetta As Integer
        Public PRGuida As Integer
        Public PRTipoavvitatura As Integer
        Public PRPrisma As Integer
        Public PRPcb As String
        Public PRMostrina As String
        Public PRBriglia As String
        Public PRFiltroluce As Integer
        Public PRFiltroMY21 As Integer

        Public CLPcb As String
        Public CLBriglia As String
        Public CLMostrina As String

        Public RSM10Buono As Boolean
        Public RSM10Scarto As Boolean

        Public RSA15aAvv1Buono As Boolean
        Public RSA15aAvv1Scarto As Boolean
        Public RSA15aAvv2Buono As Boolean
        Public RSA15aAvv2Scarto As Boolean
        Public RSA15aAvv3Buono As Boolean
        Public RSA15aAvv3Scarto As Boolean
        Public RSA15aAvv4Buono As Boolean
        Public RSA15aAvv4Scarto As Boolean
        Public RSA15aAvv5Buono As Boolean
        Public RSA15aAvv5Scarto As Boolean
        Public RSA15aAvv6Buono As Boolean
        Public RSA15aAvv6Scarto As Boolean

        Public CKA15bVitibuono As Boolean
        Public CKA15bVitiscarto As Boolean
        Public BWA15bBuono As Boolean
        Public BWA15bScarto As Boolean

        Public RSM20Buono As Boolean
        Public RSM20Scarto As Boolean

        Public MLA25aBuono As Boolean
        Public MLA25aScarto As Boolean
        Public BWA25bBuono As Boolean
        Public BWA25bScarto As Boolean

        Public RSM30Buono As Boolean
        Public RSM30Scarto As Boolean

        Public IXM30Attiva As Boolean
        Public IVM30Attiva As Boolean

        Public BCM30MostrinaAttiva As Boolean
        Public BCM30BrigliaAttiva As Boolean
        Public BCM30PcbAttiva As Boolean

        Public RSA35Avv1Buono As Boolean
        Public RSA35Avv1Scarto As Boolean
        Public RSA35Avv2Buono As Boolean
        Public RSA35Avv2Scarto As Boolean
        Public RSA35Avv3Buono As Boolean
        Public RSA35Avv3Scarto As Boolean
        Public RSA35Avv4Buono As Boolean
        Public RSA35Avv4Scarto As Boolean
        Public RSA35Avv5Buono As Boolean
        Public RSA35Avv5Scarto As Boolean
        Public RSA35Avv6Buono As Boolean
        Public RSA35Avv6Scarto As Boolean

    End Structure

    Dim FromPLCM30 As New StructFromPLCM30


    Public Sub New()
        InitializeComponent()
        ClientS7 = New S7Client
        If IntPtr.Size = 4 Then
            Text = Text + " - Esecuzione Codice a 32 bit"
        Else
            Text = Text + " - Esecuzione Codice a 64 bit"
        End If
    End Sub

    Sub PLCrw()
        Dim ReadDbM30(256) As Byte
        Dim ReadMerker(1) As Byte
        Dim ReadAttM30(1) As Byte

        Dim DbPLC As Integer


        Try
            DbPLC = CInt(AppSettings("DB_COMM"))
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        Do

            ClientS7.PlcGetStatus(Plcstatus)

            SyncLock semthread

                If Plcstatus = 8 Then
                    'Read Merker From PLC
                    'ClientS7.MBRead(1, 1, ReadMerker)
                    'FromPLCClock1S = S7.GetBitAt(ReadMerker, 0, 5)

                    'Read Data DB Actuator M30
                    ClientS7.DBRead(17, 124, 1, ReadAttM30)
                    FromPLCAttPalletM30 = S7.GetBitAt(ReadAttM30, 0, 1)

                    'Read Data DB From PLC
                    ClientS7.DBRead(DbPLC, 1536, 256, ReadDbM30)
                    FromPLCM30.NPallet = S7.GetIntAt(ReadDbM30, 0)
                    FromPLCM30.Svuotalinea = S7.GetBitAt(ReadDbM30, 20, 0)

                    FromPLCM30.DPNRicetta = S7.GetIntAt(ReadDbM30, 22)
                    FromPLCM30.DPGuida = S7.GetIntAt(ReadDbM30, 24)
                    FromPLCM30.DPTipoavvitatura = S7.GetIntAt(ReadDbM30, 26)
                    FromPLCM30.DPPrisma = S7.GetIntAt(ReadDbM30, 28)
                    FromPLCM30.DPPcb = S7.GetStringAt(ReadDbM30, 30)
                    FromPLCM30.DPMostrina = S7.GetStringAt(ReadDbM30, 48)
                    FromPLCM30.DPBriglia = S7.GetStringAt(ReadDbM30, 66)
                    FromPLCM30.DPFiltroluce = S7.GetIntAt(ReadDbM30, 84)
                    FromPLCM30.DPFiltroMY21 = S7.GetIntAt(ReadDbM30, 86)

                    FromPLCM30.PRNRicetta = S7.GetIntAt(ReadDbM30, 88)
                    FromPLCM30.PRGuida = S7.GetIntAt(ReadDbM30, 90)
                    FromPLCM30.PRTipoavvitatura = S7.GetIntAt(ReadDbM30, 92)
                    FromPLCM30.PRPrisma = S7.GetIntAt(ReadDbM30, 94)
                    FromPLCM30.PRPcb = S7.GetStringAt(ReadDbM30, 96)
                    FromPLCM30.PRMostrina = S7.GetStringAt(ReadDbM30, 114)
                    FromPLCM30.PRBriglia = S7.GetStringAt(ReadDbM30, 132)
                    FromPLCM30.PRFiltroluce = S7.GetIntAt(ReadDbM30, 150)
                    FromPLCM30.PRFiltroMY21 = S7.GetIntAt(ReadDbM30, 152)

                    FromPLCM30.CLPcb = S7.GetStringAt(ReadDbM30, 154)
                    FromPLCM30.CLBriglia = S7.GetStringAt(ReadDbM30, 186)
                    FromPLCM30.CLMostrina = S7.GetStringAt(ReadDbM30, 218)

                    FromPLCM30.RSM10Buono = S7.GetBitAt(ReadDbM30, 250, 0)
                    FromPLCM30.RSM10Scarto = S7.GetBitAt(ReadDbM30, 250, 1)

                    FromPLCM30.RSA15aAvv1Buono = S7.GetBitAt(ReadDbM30, 250, 2)
                    FromPLCM30.RSA15aAvv1Scarto = S7.GetBitAt(ReadDbM30, 250, 3)
                    FromPLCM30.RSA15aAvv2Buono = S7.GetBitAt(ReadDbM30, 250, 4)
                    FromPLCM30.RSA15aAvv2Scarto = S7.GetBitAt(ReadDbM30, 250, 5)
                    FromPLCM30.RSA15aAvv3Buono = S7.GetBitAt(ReadDbM30, 250, 6)
                    FromPLCM30.RSA15aAvv3Scarto = S7.GetBitAt(ReadDbM30, 250, 7)
                    FromPLCM30.RSA15aAvv4Buono = S7.GetBitAt(ReadDbM30, 251, 0)
                    FromPLCM30.RSA15aAvv4Scarto = S7.GetBitAt(ReadDbM30, 251, 1)
                    FromPLCM30.RSA15aAvv5Buono = S7.GetBitAt(ReadDbM30, 251, 2)
                    FromPLCM30.RSA15aAvv5Scarto = S7.GetBitAt(ReadDbM30, 251, 3)
                    FromPLCM30.RSA15aAvv6Buono = S7.GetBitAt(ReadDbM30, 251, 4)
                    FromPLCM30.RSA15aAvv6Scarto = S7.GetBitAt(ReadDbM30, 251, 5)

                    FromPLCM30.CKA15bVitibuono = S7.GetBitAt(ReadDbM30, 251, 6)
                    FromPLCM30.CKA15bVitiscarto = S7.GetBitAt(ReadDbM30, 251, 7)
                    FromPLCM30.BWA15bBuono = S7.GetBitAt(ReadDbM30, 252, 0)
                    FromPLCM30.BWA15bScarto = S7.GetBitAt(ReadDbM30, 252, 1)

                    FromPLCM30.RSM20Buono = S7.GetBitAt(ReadDbM30, 252, 2)
                    FromPLCM30.RSM20Scarto = S7.GetBitAt(ReadDbM30, 252, 3)

                    FromPLCM30.MLA25aBuono = S7.GetBitAt(ReadDbM30, 252, 4)
                    FromPLCM30.MLA25aScarto = S7.GetBitAt(ReadDbM30, 252, 5)
                    FromPLCM30.BWA25bBuono = S7.GetBitAt(ReadDbM30, 252, 6)
                    FromPLCM30.BWA25bScarto = S7.GetBitAt(ReadDbM30, 252, 7)

                    FromPLCM30.RSM30Buono = S7.GetBitAt(ReadDbM30, 253, 0)
                    FromPLCM30.RSM30Scarto = S7.GetBitAt(ReadDbM30, 253, 1)
                    FromPLCM30.IXM30Attiva = S7.GetBitAt(ReadDbM30, 253, 2)
                    FromPLCM30.IVM30Attiva = S7.GetBitAt(ReadDbM30, 253, 3)
                    FromPLCM30.BCM30MostrinaAttiva = S7.GetBitAt(ReadDbM30, 253, 4)
                    FromPLCM30.BCM30BrigliaAttiva = S7.GetBitAt(ReadDbM30, 253, 5)
                    FromPLCM30.BCM30PcbAttiva = S7.GetBitAt(ReadDbM30, 253, 6)

                    FromPLCM30.RSA35Avv1Buono = S7.GetBitAt(ReadDbM30, 253, 7)
                    FromPLCM30.RSA35Avv1Scarto = S7.GetBitAt(ReadDbM30, 254, 0)
                    FromPLCM30.RSA35Avv2Buono = S7.GetBitAt(ReadDbM30, 254, 1)
                    FromPLCM30.RSA35Avv2Scarto = S7.GetBitAt(ReadDbM30, 254, 2)
                    FromPLCM30.RSA35Avv3Buono = S7.GetBitAt(ReadDbM30, 254, 3)
                    FromPLCM30.RSA35Avv3Scarto = S7.GetBitAt(ReadDbM30, 254, 4)
                    FromPLCM30.RSA35Avv4Buono = S7.GetBitAt(ReadDbM30, 254, 5)
                    FromPLCM30.RSA35Avv4Scarto = S7.GetBitAt(ReadDbM30, 254, 6)
                    FromPLCM30.RSA35Avv5Buono = S7.GetBitAt(ReadDbM30, 254, 7)
                    FromPLCM30.RSA35Avv5Scarto = S7.GetBitAt(ReadDbM30, 255, 0)
                    FromPLCM30.RSA35Avv6Buono = S7.GetBitAt(ReadDbM30, 255, 1)
                    FromPLCM30.RSA35Avv6Scarto = S7.GetBitAt(ReadDbM30, 255, 2)
                Else
                    ClientS7.Connect()
                End If

            End SyncLock

            Threading.Thread.Sleep(500)

        Loop
    End Sub

    Sub MainProcess()
        'INIT


        'MAIN
        Do

            SubUpdateForm()

            Threading.Thread.Sleep(1000)

        Loop

    End Sub

    Sub CloseProcess()
        ThrendMainProcess.Abort()
        If ThrendPLCrw Is Nothing = False Then
            ThrendPLCrw.Abort()
        End If

        Try
            ClientS7.Disconnect()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

    End Sub

    Private Sub form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
    End Sub

    Private Sub ChiudiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChiudiToolStripMenuItem.Click
        Select Case MsgBox("Sei Sicuro Di Volere Chiudere L'Applicazione?", MsgBoxStyle.YesNoCancel, "Chiusura Applicazione")
            Case MsgBoxResult.Yes
                CloseProcess()
                Application.Exit()
        End Select
    End Sub

    Private Sub SettingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SettingToolStripMenuItem.Click
        FormSetting.ShowDialog()
    End Sub

    Public Sub PLC_Connect()
        Dim Result As Integer
        Dim Rack As Integer = System.Convert.ToInt32(AppSettings("PLC_RACK"))
        Dim Slot As Integer = System.Convert.ToInt32(AppSettings("PLC_SLOT"))
        Result = ClientS7.ConnectTo(AppSettings("PLC_IP"), Rack, Slot)

        If ClientS7.ErrorText(Result) = "OK" Then
            ConnettiToolStripMenuItem.Enabled = False
            DisconnettiToolStripMenuItem.Enabled = True
            LabelPLCStConn.Text = "OK"
            LabelPLCStConn.BackColor = Color.FromArgb(128, 255, 128)

            ThrendPLCrw = New Threading.Thread(AddressOf PLCrw)
            ThrendPLCrw.Start()


        Else
            ConnettiToolStripMenuItem.Enabled = True
            DisconnettiToolStripMenuItem.Enabled = False
            LabelPLCStConn.Text = "KO"
            LabelPLCStConn.BackColor = Color.FromArgb(255, 128, 128)
            MsgBox(ClientS7.ErrorText(Result))
        End If
    End Sub

    Public Sub PLC_Disconnect()
        Try
            ClientS7.Disconnect()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

        If ThrendPLCrw Is Nothing = False Then
            ThrendPLCrw.Abort()
            LabelPLCStConn.Text = "KO"
            LabelPLCStConn.BackColor = Color.FromArgb(255, 128, 128)
        End If


        ConnettiToolStripMenuItem.Enabled = True
        DisconnettiToolStripMenuItem.Enabled = False



    End Sub



    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'CheckForIllegalCrossThreadCalls = False

        ThrendMainProcess = New Threading.Thread(AddressOf MainProcess)
        ThrendMainProcess.Start()

        PLC_Connect()


    End Sub


    Public Sub SubUpdateForm()
        If Me.InvokeRequired Then
            UpdateForm = New UpdateFormDelegate(AddressOf SubUpdateForm)
            Me.BeginInvoke(UpdateForm)
        Else

            SyncLock semthread

                'Label Update

                Select Case Plcstatus
                    Case 8
                        LabelPLCStConn.Text = "OK"
                        LabelPLCStConn.BackColor = Color.FromArgb(128, 255, 128)
                    Case 4
                        LabelPLCStConn.Text = "KO"
                        LabelPLCStConn.BackColor = Color.FromArgb(255, 128, 128)
                    Case 0
                        LabelPLCStConn.Text = "?"
                        LabelPLCStConn.BackColor = Color.FromArgb(255, 128, 128)
                    Case Else
                        LabelPLCStConn.Text = "?"
                        LabelPLCStConn.BackColor = Color.FromArgb(255, 128, 128)
                End Select


                If Plcstatus = 8 Then

                    If FromPLCAttPalletM30 = False Then
                        GroupBoxFifoM30.Visible = False
                        GroupBoxNPallet.Visible = False
                        GroupBoxPR.Visible = False
                        GroupBoxDP.Visible = False
                        PictureBoxMostrina.Visible = False
                        PictureBoxPR.Image = My.Resources.Trasferimento
                        PictureBoxDP.Image = My.Resources.Trasferimento
                        LabelLStDpSx.Visible = False
                        LabelLStDpDx.Visible = False
                        LabelLStDpPcb.Visible = False
                        LabelLStPrSx.Visible = False
                        LabelLStPrDx.Visible = False
                        LabelLStPrPcb.Visible = False
                        LabelStDescMostrina.Visible = False
                    Else
                        GroupBoxFifoM30.Visible = True
                        GroupBoxNPallet.Visible = True
                        GroupBoxPR.Visible = True
                        GroupBoxDP.Visible = True
                        PictureBoxMostrina.Visible = True
                        PictureBoxPR.Image = My.Resources.Supp_Interno
                        PictureBoxDP.Image = My.Resources.Supp_Esterno
                        LabelLStDpSx.Visible = True
                        LabelLStDpDx.Visible = True
                        LabelLStDpPcb.Visible = True
                        LabelLStPrSx.Visible = True
                        LabelLStPrDx.Visible = True
                        LabelLStPrPcb.Visible = True
                        LabelStDescMostrina.Visible = True
                        'Label DP
                        LabelDPStNRicetta.Text = FromPLCM30.DPNRicetta
                        LabelDPStDRicetta.Text = ""

                        Select Case FromPLCM30.DPGuida
                            Case 10
                                LabelDPStGuida.Text = "SINISTRA"
                            Case 20
                                LabelDPStGuida.Text = "DESTRA"
                            Case Else
                                LabelDPStGuida.Text = CStr(FromPLCM30.DPGuida) & " Nessuna Descrizione"
                        End Select

                        Select Case FromPLCM30.DPTipoavvitatura
                            Case 1
                                LabelDPStAvvitatura.Text = "4 Avvitature Lucida"
                            Case 2
                                LabelDPStAvvitatura.Text = "4 Avvitature Opaca"
                            Case 3
                                LabelDPStAvvitatura.Text = "6 Avvitature Lucida"
                            Case 4
                                LabelDPStAvvitatura.Text = "6 Avvitature Opaca"
                            Case Else
                                LabelDPStAvvitatura.Text = CStr(FromPLCM30.DPTipoavvitatura) & " Nessuna Descrizione"
                        End Select

                        Select Case FromPLCM30.DPPrisma
                            Case 10
                                LabelDPStPrisma.Text = "168345200"
                            Case 20
                                LabelDPStPrisma.Text = "MR146431-6320"
                            Case Else
                                LabelDPStPrisma.Text = CStr(FromPLCM30.DPPrisma) & " Nessuna Descrizione"
                        End Select

                        LabelDPStPcb.Text = FromPLCM30.DPPcb

                        LabelDPStMostrina.Text = FromPLCM30.DPMostrina

                        LabelDPStBriglia.Text = FromPLCM30.DPBriglia

                        Select Case FromPLCM30.DPFiltroluce
                            Case 10
                                LabelDPStFiltrolucemostrina.Text = "ASSENTE"
                            Case 20
                                LabelDPStFiltrolucemostrina.Text = "5Q8440400"
                            Case 30
                                LabelDPStFiltrolucemostrina.Text = "MR146431-6420"
                            Case Else
                                LabelDPStFiltrolucemostrina.Text = CStr(FromPLCM30.DPFiltroluce) & " Nessuna Descrizione"
                        End Select

                        Select Case FromPLCM30.DPFiltroMY21
                            Case 10
                                LabelDPStFiltromy21.Text = "ASSENTE"
                            Case 20
                                LabelDPStFiltromy21.Text = "AMBRA"
                            Case 30
                                LabelDPStFiltromy21.Text = "ASSENTE"
                            Case Else
                                LabelDPStFiltromy21.Text = CStr(FromPLCM30.DPFiltroMY21) & " Nessuna Descrizione"
                        End Select

                        'Label PR
                        LabelPRStNRicetta.Text = FromPLCM30.PRNRicetta
                        LabelPRStDRicetta.Text = ""


                        Select Case FromPLCM30.PRGuida
                            Case 10
                                LabelPRStGuida.Text = "SINISTRA"
                            Case 20
                                LabelPRStGuida.Text = "DESTRA"
                            Case Else
                                LabelPRStGuida.Text = CStr(FromPLCM30.PRGuida) & " Nessuna Descrizione"
                        End Select

                        Select Case FromPLCM30.PRTipoavvitatura
                            Case 1
                                LabelPRStAvvitatura.Text = "4 Avvitature Lucida"
                            Case 2
                                LabelPRStAvvitatura.Text = "4 Avvitature Opaca"
                            Case 3
                                LabelPRStAvvitatura.Text = "6 Avvitature Lucida"
                            Case 4
                                LabelPRStAvvitatura.Text = "6 Avvitature Opaca"
                            Case Else
                                LabelPRStAvvitatura.Text = CStr(FromPLCM30.PRTipoavvitatura) & " Nessuna Descrizione"
                        End Select

                        Select Case FromPLCM30.PRPrisma
                            Case 10
                                LabelPRStPrisma.Text = "168345200"
                            Case 20
                                LabelPRStPrisma.Text = "MR146431-6320"
                            Case Else
                                LabelPRStPrisma.Text = CStr(FromPLCM30.PRPrisma) & " Nessuna Descrizione"
                        End Select

                        LabelPRStPcb.Text = FromPLCM30.PRPcb

                        LabelPRStMostrina.Text = FromPLCM30.PRMostrina

                        LabelPRStBriglia.Text = FromPLCM30.PRBriglia

                        Select Case FromPLCM30.PRFiltroluce
                            Case 10
                                LabelPRStFiltrolucemostrina.Text = "ASSENTE"
                            Case 20
                                LabelPRStFiltrolucemostrina.Text = "5Q8440400"
                            Case 30
                                LabelPRStFiltrolucemostrina.Text = "MR146431-6420"
                            Case Else
                                LabelPRStFiltrolucemostrina.Text = CStr(FromPLCM30.PRFiltroluce) & " Nessuna Descrizione"
                        End Select

                        Select Case FromPLCM30.PRFiltroMY21
                            Case 10
                                LabelPRStFiltromy21.Text = "ASSENTE"
                            Case 20
                                LabelPRStFiltromy21.Text = "AMBRA"
                            Case 30
                                LabelPRStFiltromy21.Text = "ASSENTE"
                            Case Else
                                LabelPRStFiltromy21.Text = CStr(FromPLCM30.PRFiltroMY21) & " Nessuna Descrizione"
                        End Select

                        'Label Pallet
                        LabelStPallet.Text = FromPLCM30.NPallet

                        'Label FIFO
                        If FromPLCM30.RSM10Buono = True And FromPLCM30.RSM10Scarto = False Then
                            LabelFStM10Res.Text = "OK"
                            LabelFStM10Res.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSM10Buono = False And FromPLCM30.RSM10Scarto = True Then
                            LabelFStM10Res.Text = "KO"
                            LabelFStM10Res.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStM10Res.Text = "/"
                            LabelFStM10Res.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSA15aAvv1Buono = True And FromPLCM30.RSA15aAvv1Scarto = False Then
                            LabelFStA15aAvv1.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSA15aAvv1Buono = False And FromPLCM30.RSA15aAvv1Scarto = True Then
                            LabelFStA15aAvv1.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15aAvv1.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSA15aAvv2Buono = True And FromPLCM30.RSA15aAvv2Scarto = False Then
                            LabelFStA15aAvv2.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSA15aAvv2Buono = False And FromPLCM30.RSA15aAvv2Scarto = True Then
                            LabelFStA15aAvv2.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15aAvv2.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSA15aAvv3Buono = True And FromPLCM30.RSA15aAvv3Scarto = False Then
                            LabelFStA15aAvv3.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSA15aAvv3Buono = False And FromPLCM30.RSA15aAvv3Scarto = True Then
                            LabelFStA15aAvv3.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15aAvv3.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSA15aAvv4Buono = True And FromPLCM30.RSA15aAvv4Scarto = False Then
                            LabelFStA15aAvv4.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSA15aAvv4Buono = False And FromPLCM30.RSA15aAvv4Scarto = True Then
                            LabelFStA15aAvv4.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15aAvv4.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSA15aAvv5Buono = True And FromPLCM30.RSA15aAvv5Scarto = False Then
                            LabelFStA15aAvv5.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSA15aAvv5Buono = False And FromPLCM30.RSA15aAvv5Scarto = True Then
                            LabelFStA15aAvv5.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15aAvv5.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSA15aAvv6Buono = True And FromPLCM30.RSA15aAvv6Scarto = False Then
                            LabelFStA15aAvv6.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSA15aAvv6Buono = False And FromPLCM30.RSA15aAvv6Scarto = True Then
                            LabelFStA15aAvv6.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15aAvv6.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.CKA15bVitibuono = True And FromPLCM30.CKA15bVitiscarto = False Then
                            LabelFStA15bCkV.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.CKA15bVitibuono = False And FromPLCM30.CKA15bVitiscarto = True Then
                            LabelFStA15bCkV.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15bCkV.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.BWA15bBuono = True And FromPLCM30.BWA15bScarto = False Then
                            LabelFStA15bCkB.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.BWA15bBuono = False And FromPLCM30.BWA15bScarto = True Then
                            LabelFStA15bCkB.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA15bCkB.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.RSM20Buono = True And FromPLCM30.RSM20Scarto = False Then
                            LabelFStM20Res.Text = "OK"
                            LabelFStM20Res.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.RSM20Buono = False And FromPLCM30.RSM20Scarto = True Then
                            LabelFStM20Res.Text = "KO"
                            LabelFStM20Res.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStM20Res.Text = "/"
                            LabelFStM20Res.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.MLA25aBuono = True And FromPLCM30.MLA25aScarto = False Then
                            LabelFStA25aRes.Text = "OK"
                            LabelFStA25aRes.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.MLA25aBuono = False And FromPLCM30.MLA25aScarto = True Then
                            LabelFStA25aRes.Text = "KO"
                            LabelFStA25aRes.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA25aRes.Text = "/"
                            LabelFStA25aRes.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        If FromPLCM30.BWA25bBuono = True And FromPLCM30.BWA25bScarto = False Then
                            LabelFStA25bRes.Text = "OK"
                            LabelFStA25bRes.BackColor = Color.FromArgb(128, 255, 128)
                        ElseIf FromPLCM30.BWA25bBuono = False And FromPLCM30.BWA25bScarto = True Then
                            LabelFStA25bRes.Text = "KO"
                            LabelFStA25bRes.BackColor = Color.FromArgb(255, 128, 128)
                        Else
                            LabelFStA25bRes.Text = "/"
                            LabelFStA25bRes.BackColor = Color.FromArgb(255, 255, 192)
                        End If

                        'Animation



                        Select Case FromPLCM30.PRBriglia
                            Case "A72000500"
                                LabelLStPrSx.Text = "LAMP"
                                LabelLStPrDx.Text = "LAMP"
                                LabelLStPrSx.BackColor = Color.Orange
                                LabelLStPrDx.BackColor = Color.Orange
                            Case "MR246510-0820"
                                LabelLStPrSx.Text = "LED"
                                LabelLStPrDx.Text = "LED"
                                LabelLStPrSx.BackColor = Color.Blue
                                LabelLStPrDx.BackColor = Color.Blue
                            Case Else
                                LabelLStPrSx.Text = "/"
                                LabelLStPrDx.Text = "/"
                                LabelLStPrSx.BackColor = Color.White
                                LabelLStPrDx.BackColor = Color.White
                        End Select

                        Select Case FromPLCM30.DPBriglia
                            Case "A72000500"
                                LabelLStDpSx.Text = "LAMP"
                                LabelLStDpDx.Text = "LAMP"
                                LabelLStDpSx.BackColor = Color.Orange
                                LabelLStDpDx.BackColor = Color.Orange
                            Case "MR246510-0820"
                                LabelLStDpSx.Text = "LED"
                                LabelLStDpDx.Text = "LED"
                                LabelLStDpSx.BackColor = Color.Blue
                                LabelLStDpDx.BackColor = Color.Blue
                            Case Else
                                LabelLStDpSx.Text = "/"
                                LabelLStDpDx.Text = "/"
                                LabelLStDpSx.BackColor = Color.White
                                LabelLStDpDx.BackColor = Color.White
                        End Select

                        Select Case FromPLCM30.DPPcb
                            Case "A42301100"
                                LabelLStDpPcb.Text = "LAMP"
                                LabelLStDpPcb.BackColor = Color.Orange
                            Case "MR146450-2980"
                                LabelLStDpPcb.Text = "LED"
                                LabelLStDpPcb.BackColor = Color.Blue
                            Case Else
                                LabelLStDpPcb.Text = "/"
                                LabelLStDpPcb.BackColor = Color.White
                        End Select

                        Select Case FromPLCM30.PRPcb
                            Case "A42301100"
                                LabelLStPrPcb.Text = "LAMP"
                                LabelLStPrPcb.BackColor = Color.Orange
                            Case "MR146450-2980"
                                LabelLStPrPcb.Text = "LED"
                                LabelLStPrPcb.BackColor = Color.Blue
                            Case Else
                                LabelLStPrPcb.Text = "/"
                                LabelLStPrPcb.BackColor = Color.White
                        End Select


                        Select Case FromPLCM30.DPMostrina
                            Case "5Q8440600"
                                LabelStDescMostrina.Text = "LUCIDA MONT BLANK"
                                If FromPLCM30.DPFiltroluce = 30 Then
                                    PictureBoxMostrina.Image = My.Resources.Mostrina_4
                                ElseIf FromPLCM30.DPFiltroluce = 20 Then
                                    PictureBoxMostrina.Image = My.Resources.Mostrina_1
                                Else
                                    PictureBoxMostrina.Image = Nothing
                                End If
                                PictureBoxDP.Image = My.Resources.Supp_Esterno
                                LabelLStDpPcb.Visible = True
                            Case "5Q4745100"
                                LabelStDescMostrina.Text = "CONDIZIONATA OPACA NERA"
                                PictureBoxMostrina.Image = My.Resources.Mostrina_2
                                PictureBoxDP.Image = My.Resources.Supp_Esterno
                                LabelLStDpPcb.Visible = True
                            Case "1B6445100"
                                LabelStDescMostrina.Text = "RISCALDATA OPACA NERA"
                                PictureBoxMostrina.Image = My.Resources.Mostrina_3
                                PictureBoxDP.Image = My.Resources.Esterno_no_PCB
                                LabelLStDpPcb.Visible = False
                            Case "MR146431-6290"
                                LabelStDescMostrina.Text = "RISCALDATA LUCE LED"
                                PictureBoxMostrina.Image = My.Resources.Mostrina_5
                                PictureBoxDP.Image = My.Resources.Esterno_no_PCB
                                LabelLStDpPcb.Visible = False
                            Case Else
                                LabelStDescMostrina.Text = ""
                                PictureBoxMostrina.Image = Nothing
                                PictureBoxDP.Image = My.Resources.Supp_Esterno
                                LabelLStDpPcb.Visible = True
                        End Select



                    End If
                Else
                    GroupBoxFifoM30.Visible = False
                    GroupBoxNPallet.Visible = False
                    GroupBoxPR.Visible = False
                    GroupBoxDP.Visible = False
                    PictureBoxMostrina.Visible = False
                    PictureBoxPR.Image = My.Resources.lostconnection
                    PictureBoxDP.Image = My.Resources.lostconnection
                    LabelLStDpSx.Visible = False
                    LabelLStDpDx.Visible = False
                    LabelLStDpPcb.Visible = False
                    LabelLStPrSx.Visible = False
                    LabelLStPrDx.Visible = False
                    LabelLStPrPcb.Visible = False
                    LabelStDescMostrina.Visible = False
                End If


            End SyncLock

        End If

    End Sub



    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        MessageBox.Show(
                "Applicazione: Supervisor Station M30" & vbCrLf &
                "Versione: 1.0" & vbCrLf &
                "Sviluppatore: Vincenzo Niespolo" & vbCrLf &
                "Email: niespotech@gmail.com" & vbCrLf &
                "Cell: +39 3349113322"
                )
    End Sub


    Private Sub ConnettiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ConnettiToolStripMenuItem.Click
        PLC_Connect()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) 
        If OpenFileDialog1.ShowDialog = ShowDialog.OK Then
            PictureBoxDP.ImageLocation = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub DisconnettiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DisconnettiToolStripMenuItem.Click
        PLC_Disconnect()
    End Sub

End Class
