Imports Bt
Imports System.Runtime.InteropServices
Imports System.Net.Sockets

Public Class SendDataToPLC

    '--------------------------------------------------------------
    ' DLLImport
    '--------------------------------------------------------------
    'ソケット通信用
    <DllImport("wininet.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function InternetOpen(ByVal lpszAgent As [String], ByVal dwAccessType As Int32, ByVal lpszProxyName As [String], ByVal lpszProxyBypass As [String], ByVal dwFlags As Int32) As IntPtr
    End Function

    <DllImport("wininet.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function InternetConnect(ByVal hInternet As IntPtr, ByVal lpszServerName As [String], ByVal nServerPort As Int16, ByVal lpszUsername As [String], ByVal lpszPassword As [String], ByVal dwService As Int32, _
     ByVal dwFlags As Int32, ByVal dwContext As IntPtr) As IntPtr
    End Function

    <DllImport("wininet.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function InternetCloseHandle(ByVal hInternet As IntPtr) As <MarshalAs(UnmanagedType.Bool)> [Boolean]
    End Function

    <DllImport("wininet.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function FtpPutFile(ByVal hConnect As IntPtr, ByVal lpszLocalFile As [String], ByVal lpszNewRemoteFile As [String], ByVal dwFlags As Int64, ByVal dwContext As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Public Shared Function WaitForSingleObject(ByVal Handle As IntPtr, ByVal Wait As Int32) As Int32
    End Function

    <DllImport("coredll.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Unicode)> _
    Public Shared Function CreateEvent(ByVal lpEventAttributes As IntPtr, <[In](), MarshalAs(UnmanagedType.Bool)> ByVal bManualReset As [Boolean], <[In](), MarshalAs(UnmanagedType.Bool)> ByVal bIntialState As Boolean, <[In](), MarshalAs(UnmanagedType.BStr)> ByVal lpName As [String]) As IntPtr
    End Function

    '--------------------------------------------------------------
    ' 定数定義
    '--------------------------------------------------------------
    ' wininetで使用する定数
    Public Const INTERNET_DEFAULT_FTP_PORT As Int32 = 21
    Public Const INTERNET_OPEN_TYPE_PRECONFIG As Int32 = 0
    Public Const INTERNET_OPEN_TYPE_DIRECT As Int32 = 1
    Public Const INTERNET_OPEN_TYPE_PROXY As Int32 = 3
    Public Const INTERNET_INVALID_PORT_NUMBER As Int32 = 0
    Public Const INTERNET_SERVICE_FTP As Int32 = 1
    Public Const INTERNET_SERVICE_GOPHER As Int32 = 2
    Public Const INTERNET_SERVICE_HTTP As Int32 = 3
    Public Const FTP_TRANSFER_TYPE_BINARY As Int64 = &H2
    Public Const FTP_TRANSFER_TYPE_ASCII As Int64 = &H1
    Public Const INTERNET_FLAG_NO_CACHE_WRITE As Int64 = &H4000000
    Public Const INTERNET_FLAG_RELOAD As Int64 = &H80000000UI
    Public Const INTERNET_FLAG_KEEP_CONNECTION As Int64 = &H400000
    Public Const INTERNET_FLAG_MULTIPART As Int64 = &H200000
    Public Const INTERNET_FLAG_PASSIVE As Int64 = &H8000000
    Public Const FILE_ATTRIBUTE_READONLY As Int64 = &H1
    Public Const FILE_ATTRIBUTE_HIDDEN As Int64 = &H2
    Public Const FILE_ATTRIBUTE_SYSTEM As Int64 = &H4
    Public Const FILE_ATTRIBUTE_DIRECTORY As Int64 = &H10
    Public Const FILE_ATTRIBUTE_ARCHIVE As Int64 = &H20
    Public Const FILE_ATTRIBUTE_NORMAL As Int64 = &H80
    Public Const FILE_ATTRIBUTE_TEMPORARY As Int64 = &H100
    Public Const FILE_ATTRIBUTE_COMPRESSED As Int64 = &H800
    Public Const FILE_ATTRIBUTE_OFFLINE As Int64 = &H1000

    Public Const WAIT_OBJECT_0 As Int32 = &H0 ' coredll.dllで使用する定数

    Private ip As String() = {"192", "168", "100", "1"} 'IPアドレス
    Public strCodedata As [String] = "" 'スキャンデータ
    Public data_count As Integer = 0 'データ数
    Public connectflag As Integer = 1 '0:接続 1:切断

    Private data_array As String()
    Private senddata As String


    Shared Sub New()
        'your code goes here
    End Sub

    Public Function Connect(ByVal serverIP As String, ByVal _Port As String, ByVal _DM As String, ByVal _data As String) As Boolean
        Dim valid As Boolean = False
        Dim strResult As String = ""

        Try

            'Dim writedat() As String = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
            '                            "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
            '                            "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
            '                            "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
            '                            "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
            '                            "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}

            Dim remotePort As Integer = CLng(_Port)

            Dim remoteHost As String
            remoteHost = serverIP

            Dim objSck As New System.Net.Sockets.TcpClient(remoteHost, remotePort)

            'Dim objSck As New System.Net.Sockets.UdpClient(remotePort)

            'Dim i As Integer
            'For i = 0 To _data.Length - 1
            '    writedat(i) = Hex(Asc(_data.Substring(i, 1)))
            'Next


            'Dim sendByte As Byte() = System.Text.Encoding.ASCII.GetBytes("WRS DM" + _DM + ".H 0050 " _
            '                               & writedat(0) & writedat(1) & " " & writedat(2) & writedat(3) & " " & writedat(4) & writedat(5) & " " & writedat(6) & writedat(7) & " " & writedat(8) & writedat(9) & " " _
            '                               & writedat(10) & writedat(11) & " " & writedat(12) & writedat(13) & " " & writedat(14) & writedat(15) & " " & writedat(16) & writedat(17) & " " & writedat(18) & writedat(19) & " " _
            '                               & writedat(20) & writedat(21) & " " & writedat(22) & writedat(23) & " " & writedat(24) & writedat(25) & " " & writedat(26) & writedat(27) & " " & writedat(28) & writedat(29) & " " _
            '                               & writedat(30) & writedat(31) & " " & writedat(32) & writedat(33) & " " & writedat(34) & writedat(35) & " " & writedat(36) & writedat(37) & " " & writedat(38) & writedat(39) & " " _
            '                               & writedat(40) & writedat(41) & " " & writedat(42) & writedat(43) & " " & writedat(44) & writedat(45) & " " & writedat(46) & writedat(47) & " " & writedat(48) & writedat(49) & " " _
            '                               & writedat(50) & writedat(51) & " " & writedat(52) & writedat(53) & " " & writedat(54) & writedat(55) & " " & writedat(56) & writedat(57) & " " & writedat(58) & writedat(59) & " " _
            '                               & writedat(60) & writedat(61) & " " & writedat(62) & writedat(63) & " " & writedat(64) & writedat(65) & " " & writedat(66) & writedat(67) & " " & writedat(68) & writedat(69) & " " _
            '                               & writedat(70) & writedat(71) & " " & writedat(72) & writedat(73) & " " & writedat(74) & writedat(75) & " " & writedat(76) & writedat(77) & " " & writedat(78) & writedat(79) & " " _
            '                               & writedat(80) & writedat(81) & " " & writedat(82) & writedat(83) & " " & writedat(84) & writedat(85) & " " & writedat(86) & writedat(87) & " " & writedat(98) & writedat(89) & " " _
            '                               & writedat(90) & writedat(91) & " " & writedat(92) & writedat(93) & " " & writedat(94) & writedat(95) & " " & writedat(96) & writedat(97) & " " & writedat(98) & writedat(99) & " " _
            '                               & writedat(100) & writedat(101) & " " & writedat(102) & writedat(103) & " " & writedat(104) & writedat(105) & " " & writedat(106) & writedat(107) & " " & writedat(108) & writedat(109) & " " _
            '                               & writedat(110) & writedat(111) & " " & writedat(112) & writedat(113) & " " & writedat(114) & writedat(115) & " " & writedat(116) & writedat(117) & " " & writedat(118) & writedat(119) & Chr(&HD))


            If _data = "OK" Then
                strResult = "4F4B"
            ElseIf _data = "NG" Then
                strResult = "4E47"
            Else
                strResult = ""
            End If

            Dim sendByte As Byte() = System.Text.Encoding.ASCII.GetBytes("WR EM" + _DM + ".H " & strResult & vbCr)


            Dim stream As NetworkStream = objSck.GetStream()
            stream.Write(sendByte, 0, sendByte.Length)

            'objSck.Connect(remoteHost, remotePort)
            'objSck.Send(sendByte, sendByte.GetLength(0))

            'Dim cnt As Integer = 0

            'For cnt = 0 To 100
            '    System.Threading.Thread.Sleep(100)

            '    If objSck.Client.Available > 0 Then
            '        Dim remoteEP As System.Net.IPEndPoint = Nothing
            '        Dim dat2 As Byte() = New [Byte](255) {}
            '        stream.Read(dat2, 0, dat2.Length)
            '        'Dim dat2 As Byte() = objSck.Receive(remoteEP)

            '        Dim result As String = System.Text.Encoding.ASCII.GetString(dat2, 0, dat2.Length)
            '        'MsgBox("result = [" & result & "]")

            '        Exit For
            '    End If

            '    'If cnt = 15 Then
            '    '    MsgBox("Timeout")
            '    '    Exit For
            '    'End If
            'Next


            stream.Close()
            objSck.Close()
            Return True


        Catch ex As Exception
            Return valid
        End Try


    End Function


    Public Function TestMessage(ByVal serverIP As String, ByVal _Port As String, ByVal _DM As String, ByVal _data As String) As String
        Dim valid As String = ""

        Try

            Dim writedat() As String = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", _
                                        "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}

            Dim remotePort As Integer = CLng(_Port)

            Dim remoteHost As String
            remoteHost = serverIP

            'Dim objSck As New System.Net.Sockets.TcpClient(remoteHost, remotePort)

            'Dim objSck As New System.Net.Sockets.UdpClient(remotePort)

            Dim i As Integer
            For i = 0 To _data.Length - 1
                writedat(i) = Hex(Asc(_data.Substring(i, 1)))
            Next

            valid = "WRS DM" + _DM + ".H 0050 " _
                                           & writedat(0) & writedat(1) & " " & writedat(2) & writedat(3) & " " & writedat(4) & writedat(5) & " " & writedat(6) & writedat(7) & " " & writedat(8) & writedat(9) & " " _
                                           & writedat(10) & writedat(11) & " " & writedat(12) & writedat(13) & " " & writedat(14) & writedat(15) & " " & writedat(16) & writedat(17) & " " & writedat(18) & writedat(19) & " " _
                                           & writedat(20) & writedat(21) & " " & writedat(22) & writedat(23) & " " & writedat(24) & writedat(25) & " " & writedat(26) & writedat(27) & " " & writedat(28) & writedat(29) & " " _
                                           & writedat(30) & writedat(31) & " " & writedat(32) & writedat(33) & " " & writedat(34) & writedat(35) & " " & writedat(36) & writedat(37) & " " & writedat(38) & writedat(39) & " " _
                                           & writedat(40) & writedat(41) & " " & writedat(42) & writedat(43) & " " & writedat(44) & writedat(45) & " " & writedat(46) & writedat(47) & " " & writedat(48) & writedat(49) & " " _
                                           & writedat(50) & writedat(51) & " " & writedat(52) & writedat(53) & " " & writedat(54) & writedat(55) & " " & writedat(56) & writedat(57) & " " & writedat(58) & writedat(59) & " " _
                                           & writedat(60) & writedat(61) & " " & writedat(62) & writedat(63) & " " & writedat(64) & writedat(65) & " " & writedat(66) & writedat(67) & " " & writedat(68) & writedat(69) & " " _
                                           & writedat(70) & writedat(71) & " " & writedat(72) & writedat(73) & " " & writedat(74) & writedat(75) & " " & writedat(76) & writedat(77) & " " & writedat(78) & writedat(79) & " " _
                                           & writedat(80) & writedat(81) & " " & writedat(82) & writedat(83) & " " & writedat(84) & writedat(85) & " " & writedat(86) & writedat(87) & " " & writedat(98) & writedat(89) & " " _
                                           & writedat(90) & writedat(91) & " " & writedat(92) & writedat(93) & " " & writedat(94) & writedat(95) & " " & writedat(96) & writedat(97) & " " & writedat(98) & writedat(99) & " " _
                                           & writedat(100) & writedat(101) & " " & writedat(102) & writedat(103) & " " & writedat(104) & writedat(105) & " " & writedat(106) & writedat(107) & " " & writedat(108) & writedat(109) & " " _
                                           & writedat(110) & writedat(111) & " " & writedat(112) & writedat(113) & " " & writedat(114) & writedat(115) & " " & writedat(116) & writedat(117) & " " & writedat(118) & writedat(119) & Chr(&HD)


            'Dim sendByte As Byte() = System.Text.Encoding.ASCII.GetBytes("WRS DM" + _DM + ".H 0050 " _
            '                               & writedat(0) & writedat(1) & " " & writedat(2) & writedat(3) & " " & writedat(4) & writedat(5) & " " & writedat(6) & writedat(7) & " " & writedat(8) & writedat(9) & " " _
            '                               & writedat(10) & writedat(11) & " " & writedat(12) & writedat(13) & " " & writedat(14) & writedat(15) & " " & writedat(16) & writedat(17) & " " & writedat(18) & writedat(19) & " " _
            '                               & writedat(20) & writedat(21) & " " & writedat(22) & writedat(23) & " " & writedat(24) & writedat(25) & " " & writedat(26) & writedat(27) & " " & writedat(28) & writedat(29) & " " _
            '                               & writedat(30) & writedat(31) & " " & writedat(32) & writedat(33) & " " & writedat(34) & writedat(35) & " " & writedat(36) & writedat(37) & " " & writedat(38) & writedat(39) & " " _
            '                               & writedat(40) & writedat(41) & " " & writedat(42) & writedat(43) & " " & writedat(44) & writedat(45) & " " & writedat(46) & writedat(47) & " " & writedat(48) & writedat(49) & " " _
            '                               & writedat(50) & writedat(51) & " " & writedat(52) & writedat(53) & " " & writedat(54) & writedat(55) & " " & writedat(56) & writedat(57) & " " & writedat(58) & writedat(59) & " " _
            '                               & writedat(60) & writedat(61) & " " & writedat(62) & writedat(63) & " " & writedat(64) & writedat(65) & " " & writedat(66) & writedat(67) & " " & writedat(68) & writedat(69) & " " _
            '                               & writedat(70) & writedat(71) & " " & writedat(72) & writedat(73) & " " & writedat(74) & writedat(75) & " " & writedat(76) & writedat(77) & " " & writedat(78) & writedat(79) & " " _
            '                               & writedat(80) & writedat(81) & " " & writedat(82) & writedat(83) & " " & writedat(84) & writedat(85) & " " & writedat(86) & writedat(87) & " " & writedat(98) & writedat(89) & " " _
            '                               & writedat(90) & writedat(91) & " " & writedat(92) & writedat(93) & " " & writedat(94) & writedat(95) & " " & writedat(96) & writedat(97) & " " & writedat(98) & writedat(99) & " " _
            '                               & writedat(100) & writedat(101) & " " & writedat(102) & writedat(103) & " " & writedat(104) & writedat(105) & " " & writedat(106) & writedat(107) & " " & writedat(108) & writedat(109) & " " _
            '                               & writedat(110) & writedat(111) & " " & writedat(112) & writedat(113) & " " & writedat(114) & writedat(115) & " " & writedat(116) & writedat(117) & " " & writedat(118) & writedat(119) & Chr(&HD))

            'Dim stream As NetworkStream = objSck.GetStream()
            'stream.Write(sendByte, 0, sendByte.Length)

            'objSck.Connect(remoteHost, remotePort)
            'objSck.Send(sendByte, sendByte.GetLength(0))

            'Dim cnt As Integer = 0

            'For cnt = 0 To 100
            '    System.Threading.Thread.Sleep(100)

            '    If objSck.Client.Available > 0 Then
            '        Dim remoteEP As System.Net.IPEndPoint = Nothing
            '        Dim dat2 As Byte() = New [Byte](255) {}
            '        stream.Read(dat2, 0, dat2.Length)
            '        'Dim dat2 As Byte() = objSck.Receive(remoteEP)

            '        Dim result As String = System.Text.Encoding.ASCII.GetString(dat2, 0, dat2.Length)
            '        'MsgBox("result = [" & result & "]")

            '        Exit For
            '    End If

            '    'If cnt = 15 Then
            '    '    MsgBox("Timeout")
            '    '    Exit For
            '    'End If
            'Next


            'stream.Close()
            'objSck.Close()
            Return valid


        Catch ex As Exception
            Return valid
        End Try


    End Function

   

End Class
