' Below code is created by  chun@icpdas.com  on Nov.28,2006

Option Explicit On   ' Each variable must be declared well

' Socket state constant:
'   MSWinsockLib.StateConstants.sckClosed
'   MSWinsockLib.StateConstants.sckClosing
'   MSWinsockLib.StateConstants.sckConnected           ' Value=7
'   MSWinsockLib.StateConstants.sckConnecting          ' Value=6
'   MSWinsockLib.StateConstants.sckConnectionPending
'   MSWinsockLib.StateConstants.sckError               ' Value=9
'   MSWinsockLib.StateConstants.sckHostResolved
'   MSWinsockLib.StateConstants.sckListening
'   MSWinsockLib.StateConstants.sckOpen
'   MSWinsockLib.StateConstants.sckResolvingHost

' Modbus TCP/IP format to set one boolean to True (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  6       1  5  HiAdr  LoAdr-1  FF   0    
' for ex.  Set Wincon's boolean variable (with Network address No = 5 , 5-1 = 4 =  "Hi=0  Lo=4" ) to True 
'   9  9  0  0  0  6       1  5    0       4     FF   0      
' Wincon will response:
'   9  9  0  0  0  6       1  5    0       4     FF   0      
'-----------------------------------------------------------------------------------------

' Modbus TCP/IP format to set one boolean to False (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  6       1  5  HiAdr  LoAdr-1   0   0    
' for ex.  Set Wincon's boolean variable (with Network address No = 259 , 259-1 = 258 =  "Hi=1  Lo=2" ) to False 
'   9  9  0  0  0  6       1  5    1       2      0   0      
' Wincon will response:
'   9  9  0  0  0  6       1  5    1       2      0   0      
'-----------------------------------------------------------------------------------------

' Modbus TCP/IP format to request 1 to 255 booleans (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  6       1  2  HiAdr  LoAdr-1   0  num
' for ex.  request 32 boolean variable's value (starting with Network address No = 1, 1-1 = 0 =  "Hi=0  Lo=0" ) 
'   9  9  0  0  0  6       1  2    0       0      0   8      (8 booleans , hex=8)
' Wincon will response:
'   9  9  0  0  0  4       1  2    1       Byte1         (8 booleans , each is 1 bit, so total 1 bytes)
'-----------------------------------------------------------------------------------------

' Modbus TCP/IP format to request 1 to 122 words (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  6       1  4  HiAdr  LoAdr-1   0  num
' for ex.  request 6 word variable's value (starting with Network address No = 101, 101-1 = 100 =  "Hi=0  Lo=64" ) 
'   9  9  0  0  0  6       1  4    0       64      0   6      
' Wincon will response:
'   9  9  0  0  0  F       1  4    C       Byte1  Byte2  Byte3  Byte4  Byte5  Byte6  Byte7  Byte8  Byte9  Byte10  Byte11  Byte12  (6 words , each is 2 bytes, so total 12 bytes)
'-----------------------------------------------------------------------------------------

' Modbus TCP/IP format to set one word value (-32768 to +32767) (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  6       1  6  HiAdr  LoAdr-1   HiByte   LoByte
' for ex.  set  769  to  word variable address  103  
'   9  9  0  0  0  6       1  4    0     66         3        1      
' Wincon will response:
'   9  9  0  0  0  6       1  4    0     66         3        1
'-----------------------------------------------------------------------------------------

' Modbus TCP/IP format to set one long value (-2147483647 to +2147483648) (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  B       1  10  HiAdr  LoAdr-1   0   2   4   LoWord-HiByte   LoWord-HiByte  HiWord-HiByte   HiWord-HiByte
' for ex.  set  1234567  to  long variable address  101 (address 102 in ISaGRAF should be empty, not used by others)  
'   9  9  0  0  0  B       1  10    0     64       0   2   4        D6              87              0             12      
' Wincon will response:
'   9  9  0  0  0  6       1  10    0     64       0   2  
'-----------------------------------------------------------------------------------------

' Modbus TCP/IP format to set one single float value  (assume Wincon's Net ID=1), Hexidecimal
'-----------------------------------------------------------------------------------------
'   9  9  0  0  0  6       1  10  HiAdr  LoAdr-1   0   2   4   LoWord-HiByte   LoWord-HiByte  HiWord-HiByte   HiWord-HiByte
' for ex.  set  1.023  to  long variable address  105 (address 106 in ISaGRAF should be empty, not used by others)  
'   9  9  0  0  0  B       1  10    0     68       0   2   4        F0              AA              3F             82      
' Wincon will response:
'   9  9  0  0  0  6       1  10    0     68       0   2  
'-----------------------------------------------------------------------------------------


' In this example, Timer 1 is for requesting booleans & words, Timer2 is for checking timeout after send_socket()

Public Class Form1

    Dim timer1_step As Int32 = 0   ' 0: request 8 boolean,  1: request 4 words

    Dim rcvbuf() As Byte   ' byte buf to receive data from Wincon

    Dim sndbuf() As Byte ' byte buf to send data or question to Wincon

    ' data in the ISaGRAF controller
    Dim boo_val(8) As Boolean  ' boolean value at Modbus No. 1 to 8
    Dim long_101 As Int32      ' Int32 value at Modbus No. 101
    Dim word_103 As Int16      ' Int16 value at Modbus No. 103
    Dim word_104 As Int16      ' Int16 value at Modbus No. 104
    Dim float_105 As Single    ' 32-bit single float value at Modbus No. 105
    Dim long_107 As Int32      ' Int32 value at Modbus No. 107


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Enable timer1 to request 32 boolean data & 16 word data of Wincon
        Timer1.Interval = 500   ' period is 0.5 seconds
        Timer1.Enabled = True

        ' set controller's protocol & IP & port No.
        AxWinsock1.RemoteHost = "10.0.0.103"    ' Wincon's IP
        AxWinsock1.RemotePort = 502             ' Modbus TCP/IP 's port No.
        AxWinsock1.Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol    ' TCP

        ' init timer2 as disabled
        Timer2.Enabled = False

        TextBox2.Text = "Communication is not ready. PC is trying to connect to controller now ... "   ' set initial value is break
        TextBox2.BackColor = Color.LightPink

        Try
            AxWinsock1.Connect()     ' try to connect Wincon again
        Catch ex As Exception
            ' process exception
        End Try

    End Sub


    ' Timer1 function
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' display current socket state
        Select Case AxWinsock1.CtlState
            Case MSWinsockLib.StateConstants.sckClosed
                TextBox1.Text = "Socket closed !"
            Case MSWinsockLib.StateConstants.sckClosing
                TextBox1.Text = "Socket closing ..."
            Case MSWinsockLib.StateConstants.sckConnected
                TextBox1.Text = "Wincon connected well"
            Case MSWinsockLib.StateConstants.sckConnecting
                TextBox1.Text = "Socket connecting ..."
            Case MSWinsockLib.StateConstants.sckConnectionPending
                TextBox1.Text = "Socket connection pending ..."
            Case MSWinsockLib.StateConstants.sckError
                TextBox1.Text = "Communication break or Socket error !"
            Case MSWinsockLib.StateConstants.sckHostResolved
                TextBox1.Text = "Socket host resolved !"
            Case MSWinsockLib.StateConstants.sckListening
                TextBox1.Text = "Socket listening ..."
            Case MSWinsockLib.StateConstants.sckOpen
                TextBox1.Text = "Socket open !"
            Case MSWinsockLib.StateConstants.sckResolvingHost
                TextBox1.Text = "Socket resolving Host ..."
        End Select

        ' if the current socket state is not connected 
        If (AxWinsock1.CtlState <> MSWinsockLib.StateConstants.sckConnected) Then
            TextBox2.Text = "Communication is not ready. PC is trying to connect to controller now ... "   ' set initial value is break
            TextBox2.BackColor = Color.LightPink
        End If


        ' if the current socket state is not connected and is not connecting
        If (AxWinsock1.CtlState <> MSWinsockLib.StateConstants.sckConnected) And _
           (AxWinsock1.CtlState <> MSWinsockLib.StateConstants.sckConnecting) Then

            Try
                AxWinsock1.Close()
                AxWinsock1.Connect()     ' try to connect Wincon again
            Catch ex As Exception
                ' process exception
            End Try

        End If

        If timer1_step = 0 Then    ' 0: request 8 boolean starting from Network addr=1

            ReDim sndbuf(0 To 11)
            sndbuf(0) = 1            ' here using ID1=1 & ID2=1 to distinguish this command & get boolean answer
            sndbuf(1) = 1
            sndbuf(2) = 0
            sndbuf(3) = 0
            sndbuf(4) = 0
            sndbuf(5) = 6            ' legth of below bytes

            sndbuf(6) = 1            ' Wincon's Net-ID
            sndbuf(7) = 2            ' Modbus call 2 to request booleans
            sndbuf(8) = 0            ' Starting Adr = 1
            sndbuf(9) = 0            ' 
            sndbuf(10) = 0           ' data number , 8 booleans
            sndbuf(11) = 8        '

        ElseIf timer1_step = 1 Then   '1: request 8 words starting from Network addr=101

            ReDim sndbuf(0 To 11)
            sndbuf(0) = 1            ' here using ID1=1 & ID2=2 to distinguish this command & get answer
            sndbuf(1) = 2
            sndbuf(2) = 0
            sndbuf(3) = 0
            sndbuf(4) = 0
            sndbuf(5) = 6            ' legth of below bytes

            sndbuf(6) = 1            ' Wincon's Net-ID
            sndbuf(7) = 4            ' Modbus call 4 to request words 
            sndbuf(8) = 0            ' Starting Adr = 101
            sndbuf(9) = 100          '
            sndbuf(10) = 0           ' data number , 8 words
            sndbuf(11) = 8           '

        End If

        ' send socket data without error display
        send_socket(sndbuf, 0)

        ' switch timer1_step to 0 --> 1 --> 0 --> 1 --> ...
        timer1_step = timer1_step + 1
        If timer1_step = 2 Then    ' *** we use only timer1_step = 0 & 1
            timer1_step = 0
        End If

    End Sub


    ' Timer2 function
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        ' disable timeout timer
        Timer2.Enabled = False

        ' display timeout message
        TextBox2.Text = "Controller no response, Timeout !"
        TextBox2.BackColor = Color.LightPink

    End Sub


    ' function for clicking button1 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' below is to turn Wincon's boolean Network address 1 to TRUE 
        ReDim sndbuf(0 To 11)
        sndbuf(0) = 2   ' ID1
        sndbuf(1) = 1   ' ID2
        sndbuf(2) = 0
        sndbuf(3) = 0
        sndbuf(4) = 0
        sndbuf(5) = 6            ' legth of below bytes

        sndbuf(6) = 1            ' Wincon's Net-ID
        sndbuf(7) = 5            ' Modbus call 5 to set boolean to True or False
        sndbuf(8) = 0            ' Adr
        sndbuf(9) = 0            ' Adr = 1   ********* this buttone set address 1 to True ***************
        sndbuf(10) = &HFF        ' Set to True
        sndbuf(11) = 0           '

        ' send socket data with display if error happens
        send_socket(sndbuf, 1)

    End Sub


    ' function for clicking button2 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ' below is to turn Wincon's boolean Network address 1 to TRUE 
        ReDim sndbuf(0 To 11)
        sndbuf(0) = 2   ' ID1
        sndbuf(1) = 2   ' ID2
        sndbuf(2) = 0
        sndbuf(3) = 0
        sndbuf(4) = 0
        sndbuf(5) = 6            ' legth of below bytes

        sndbuf(6) = 1            ' Wincon's Net-ID
        sndbuf(7) = 5            ' Modbus call 5 to set boolean to True or False
        sndbuf(8) = 0            ' Adr
        sndbuf(9) = 0            ' Adr = 1   ********* this buttone set address 1 to True ***************
        sndbuf(10) = 0           ' Set to False
        sndbuf(11) = 0           '

        ' send socket data with display if error happens
        send_socket(sndbuf, 1)

    End Sub


    ' function for clicking button3 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim val As Int16
        Dim B(2) As Byte
        Dim err As Boolean = False

        ' convert & check input value in the TextBox
        err = False
        Try
            val = Convert.ToInt16(TextBox8.Text)
        Catch ex As Exception
            err = True
            MessageBox.Show("word value should be in the range of -32768 to +32767")
            Return
        End Try

        ' if value is not correct
        If val > 32767 Or val < -32768 Then
            MessageBox.Show("word value should be in the range of -32768 to +32767")
        Else

            ' convert Int16 to Modbus 2 bytes
            vMap.Int16_to_Mbus_two_Byte(val, B(0), B(1))

            ' value is correct, send to Wincon
            ' below is to set word value to Wincon's integer Network address 103 
            ReDim sndbuf(0 To 11)
            sndbuf(0) = 2   ' ID1
            sndbuf(1) = 3   ' ID2
            sndbuf(2) = 0
            sndbuf(3) = 0
            sndbuf(4) = 0
            sndbuf(5) = 6            ' legth of below bytes

            sndbuf(6) = 1            ' Wincon's Net-ID
            sndbuf(7) = 6            ' Modbus call 6 to set word value
            sndbuf(8) = 0            ' Adr
            sndbuf(9) = 102          ' Adr = 103   
            sndbuf(10) = B(0)        ' hibyte
            sndbuf(11) = B(1)        ' lobyte

            ' send socket data with display if error happens
            send_socket(sndbuf, 1)

        End If

    End Sub


    ' function for clicking button4 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim val As Int16
        Dim B(2) As Byte
        Dim err As Boolean = False

        ' convert & check input value in the TextBox
        err = False
        Try
            val = Convert.ToInt16(TextBox9.Text)
        Catch ex As Exception
            err = True
            MessageBox.Show("word value should be in the range of -32768 to +32767")
            Return
        End Try

        ' if value is not correct
        If val > 32767 Or val < -32768 Then
            MessageBox.Show("word value should be in the range of -32768 to +32767")
        Else

            ' convert Int16 to Modbus 2 bytes
            vMap.Int16_to_Mbus_two_Byte(val, B(0), B(1))

            ' value is correct, send to Wincon
            ' below is to set word value to Wincon's integer Network address 104
            ReDim sndbuf(0 To 11)
            sndbuf(0) = 2   ' ID1
            sndbuf(1) = 4   ' ID2
            sndbuf(2) = 0
            sndbuf(3) = 0
            sndbuf(4) = 0
            sndbuf(5) = 6            ' legth of below bytes

            sndbuf(6) = 1            ' Wincon's Net-ID
            sndbuf(7) = 6            ' Modbus call 6 to set word value
            sndbuf(8) = 0            ' Adr
            sndbuf(9) = 103        ' Adr = 104  
            sndbuf(10) = B(0)        ' hibyte
            sndbuf(11) = B(1)        ' lobyte

            ' send socket data with display if error happens
            send_socket(sndbuf, 1)

        End If

    End Sub


    ' function for clicking button5 
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Dim F As Single
        Dim B(4) As Byte
        Dim err As Boolean = False

        ' convert & check input value in the TextBox
        err = False
        Try
            F = Convert.ToSingle(TextBox10.Text)
        Catch ex As Exception
            err = True
            MessageBox.Show("value should be a single floating point value")
            Return
        End Try

        ' map single floating point value to become Modbus 4 bytes
        If vMap.Single_to_Mbus_four_Byte(F, B(0), B(1), B(2), B(3)) = False Then
            err = True
            MessageBox.Show("value error")
            Return
        End If

        'Debug.Print(" " + B(0).ToString + "  " + B(1).ToString + "  " + B(2).ToString + "  " + B(3).ToString)

        ' value is correct, send to Wincon
        ' below is to set a float value to Wincon's REAL Network address 105 (a float value is 32-bit = 2 words = 4 bytes)
        ReDim sndbuf(0 To 16)
        sndbuf(0) = 2            ' ID1
        sndbuf(1) = 5            ' ID2
        sndbuf(2) = 0
        sndbuf(3) = 0
        sndbuf(4) = 0
        sndbuf(5) = 11           ' legth of below bytes

        sndbuf(6) = 1            ' Wincon's Net-ID
        sndbuf(7) = 16           ' Modbus call 16 to set many words 
        sndbuf(8) = 0            ' starting Adr = 105 
        sndbuf(9) = 104         '  
        sndbuf(10) = 0           ' set 2 words
        sndbuf(11) = 2           '
        sndbuf(12) = 4           ' 2 words = 4 bytes
        sndbuf(13) = B(0)        ' Modbus 4 bytes   , Modbus format
        sndbuf(14) = B(1)        ' 
        sndbuf(15) = B(2)        ' 
        sndbuf(16) = B(3)        ' 

        ' send socket data with display if error happens
        send_socket(sndbuf, 1)

    End Sub


    ' function for clicking button6 
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        Dim V As Int32
        Dim B(4) As Byte
        Dim err As Boolean = False

        ' convert & check input value in the TextBox
        err = False
        Try
            V = Convert.ToInt32(TextBox12.Text)
        Catch ex As Exception
            err = True
            MessageBox.Show("value should be a int32 value")
            Return
        End Try

        ' map Int32 value to become Modbus 4 bytes
        vMap.Int32_to_Mbus_four_Byte(V, B(0), B(1), B(2), B(3))
        
        ' value is correct, send to Wincon
        ' below is to set a long value to Wincon's integer Network address 107 (a long value is 32-bit = 2 words = 4 bytes)
        ReDim sndbuf(0 To 16)
        sndbuf(0) = 2            ' ID1
        sndbuf(1) = 6            ' ID2
        sndbuf(2) = 0
        sndbuf(3) = 0
        sndbuf(4) = 0
        sndbuf(5) = 11           ' legth of below bytes

        sndbuf(6) = 1            ' Wincon's Net-ID
        sndbuf(7) = 16           ' Modbus call 16 to set many words 
        sndbuf(8) = 0            ' starting Adr = 107 
        sndbuf(9) = 106         '  
        sndbuf(10) = 0           ' set 2 words
        sndbuf(11) = 2           '
        sndbuf(12) = 4           ' 2 words = 4 bytes
        sndbuf(13) = B(0)        ' Modbus 4 bytes   , Modbus format
        sndbuf(14) = B(1)        ' 
        sndbuf(15) = B(2)        ' 
        sndbuf(16) = B(3)        ' 

        ' send socket data with display if error happens
        send_socket(sndbuf, 1)

    End Sub


    ' event function triggered when socket data coming
    Private Sub AxWinsock1_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles AxWinsock1.DataArrival

        Dim len As Int32
        Dim boo_val(8) As Boolean

        Try
            AxWinsock1.GetData(rcvbuf)    ' receive coming data
        Catch ex As Exception
            ' process exception
            Return
        End Try

        ' data arrived, disable timeout timer
        Timer2.Enabled = False

        ' get byte number of coming data & display it
        len = rcvbuf.Length
        TextBox2.Text = "Receive " + len.ToString() + " bytes"
        TextBox2.BackColor = Color.LightBlue

        If len >= 10 Then

            ' check if the answer's ID1 & ID2 & Net-ID & Modbus call is correct or not, answer of requesting 8 booleans
            ' ---------------------------------------------------------------------------
            If rcvbuf(0) = 1 And rcvbuf(1) = 1 And rcvbuf(6) = 1 And rcvbuf(7) = 2 Then

                vMap.Byte_to_Mbus_eight_Boolean(rcvbuf(9), boo_val(0), boo_val(1), boo_val(2), boo_val(3), _
                boo_val(4), boo_val(5), boo_val(6), boo_val(7))

                ' change pane1 to 8's color according the value of boo_val()
                If boo_val(0) = True Then
                    Panel1.BackColor = Color.Red
                Else
                    Panel1.BackColor = Color.Blue
                End If
                If boo_val(1) = True Then
                    Panel2.BackColor = Color.Red
                Else
                    Panel2.BackColor = Color.Blue
                End If
                If boo_val(2) = True Then
                    Panel3.BackColor = Color.Red
                Else
                    Panel3.BackColor = Color.Blue
                End If
                If boo_val(3) = True Then
                    Panel4.BackColor = Color.Red
                Else
                    Panel4.BackColor = Color.Blue
                End If
                If boo_val(4) = True Then
                    Panel5.BackColor = Color.Red
                Else
                    Panel5.BackColor = Color.Blue
                End If
                If boo_val(5) = True Then
                    Panel6.BackColor = Color.Red
                Else
                    Panel6.BackColor = Color.Blue
                End If
                If boo_val(6) = True Then
                    Panel7.BackColor = Color.Red
                Else
                    Panel7.BackColor = Color.Blue
                End If
                If boo_val(7) = True Then
                    Panel8.BackColor = Color.Red
                Else
                    Panel8.BackColor = Color.Blue
                End If
                ' ---------------------------------------------------------------------------


                ' check if the answer's ID1 & ID2 & Net-ID & Modbus call is correct or not, answer of requesting 8 words
                ' ---------------------------------------------------------------------------
            ElseIf rcvbuf(0) = 1 And rcvbuf(1) = 2 And rcvbuf(6) = 1 And rcvbuf(7) = 4 And len >= 25 Then

                ' get long_101 value from Modbus 4 byte
                vMap.Mbus_four_Byte_to_Int32(rcvbuf(9), rcvbuf(10), rcvbuf(11), rcvbuf(12), long_101)
                TextBox7.Text = long_101.ToString

                ' get word_103 from Modbus 2 byte 
                vMap.Mbus_two_Byte_to_Int16(rcvbuf(13), rcvbuf(14), word_103)
                TextBox5.Text = word_103.ToString

                ' get word_104 from Modbus 2 byte 
                vMap.Mbus_two_Byte_to_Int16(rcvbuf(15), rcvbuf(16), word_104)
                TextBox6.Text = word_104.ToString

                ' get float_105 point value from Modbus 4 bytes
                If vMap.Mbus_four_Byte_to_Single(rcvbuf(17), rcvbuf(18), rcvbuf(19), rcvbuf(20), float_105) = True Then
                    TextBox11.Text = float_105.ToString    ' display float_105
                Else
                    TextBox11.Text = "ErrValue !!!"
                End If

                ' get long_107 value from Modbus 4 byte
                vMap.Mbus_four_Byte_to_Int32(rcvbuf(21), rcvbuf(22), rcvbuf(23), rcvbuf(24), long_107)
                TextBox13.Text = long_107

                ' ---------------------------------------------------------------------------

            End If


        End If

    End Sub


    ' send socket data
    ' buf( ): byte array to send  
    ' dispaly:  1:display messagebox when error, 0:don't display
    Private Function send_socket(ByVal buf As Byte(), ByVal display As Int32) As Boolean

        Dim ret As Boolean

        ret = True

        ' if socket is connected well
        If AxWinsock1.CtlState = MSWinsockLib.StateConstants.sckConnected Then

            ' send socket data
            Try
                AxWinsock1.SendData(buf)   ' send
            Catch ex As Exception
                ret = False
                ' process exception
            End Try

            ' enable timer2 to check if timeout or not
            ' if controller is not response in 2 seconds, display timeout message
            Timer2.Interval = 2000
            Timer2.Enabled = True

        Else
            ret = False
            If display = 1 Then
                MessageBox.Show("Communication is not healthy, can not send data to Wincon !")
            End If
        End If

        Return ret

    End Function

 
End Class
