Announcement

Collapse
No announcement yet.

VB.net Script for iTach

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    VB.net Script for iTach

    I am attempting to write a .net script to communicate commands to the iTach. I am new to vb.net and get the Warning message below. I have also posted the script. Any help would be appreciated. BTW I took the code from another post and hope to make it work for me with the appropriate modifications.

    VB.Net script exception(0), re-starting: Object reference not set to an instance of an object

    Code:
    ' import required modules
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
        Public Sub TVControl()
            Const Host = "192.168.1.250"
            Const HostPort = 4998
            Const LGTVPWRCode = "sendir,1:1,1,56561,1,1,19,75,10,4,1,79,8,3,1,179" &vbCR
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect(host, hostport)
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            Dim strResponse As String
            strResponse = "No response"
            If networkStream.CanWrite Then
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(LGTVPWRCode)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                strResponse = "Device responded: " & CStr(returndata)
                hs.WriteLog (strResponse)
            Else
                If Not networkStream.CanWrite Then
                    strResponse = "Error: Cannot write data to the device"
                    tcpClient.Close()
                    hs.WriteLog (strResponse)
                End If
            End If
          End Sub

    #2
    change:
    Public Sub TVControl()
    to:
    Sub Main(ByVal parms as Object)

    Also, you might need System.Net added to the scripting references - not 100% sure though...
    HS4Pro Running on a Raspberry Pi4
    79 Z-Wave Nodes, 131 Events, 383 Devices
    Z-Wave, UPB, WiFi
    Plugins: EasyTrigger, weatherXML, OMNI, Z-Wave, Tuya, Device History
    HSTouch Clients: 3 Android, 1 Joggler

    Comment


      #3
      Thanks, that helped with the Warning. I now get the run-time error below. As you see, I am inexperienced with VB.net.

      Error 1 Running script /usr/local/HomeSeer/scripts/secure.vb :Exception has been thrown by the target of an invocation.Cannot find the requested class member.

      Comment


        #4
        Yeah, I think you might need those scripting references for it to work. This is also assuming those DLLs are on the Zee. Try this:

        backup your /usr/local/HomeSeer/Config/settings.ini file
        edit the settings.ini file and add the following entry:
        ScriptingReferences=System.Net;System.Net.dll
        Save and restart
        --Note the case as this is linux...
        HS4Pro Running on a Raspberry Pi4
        79 Z-Wave Nodes, 131 Events, 383 Devices
        Z-Wave, UPB, WiFi
        Plugins: EasyTrigger, weatherXML, OMNI, Z-Wave, Tuya, Device History
        HSTouch Clients: 3 Android, 1 Joggler

        Comment


          #5
          I have the iTach sending out the IR commands and the devices responding as they should. For simplicity, I created four scripts (LGTV, SONY_ON, Sony_OFF, and CISCO) The Sony requires a different on/off command. I have an Event that calls each of the scripts to turn on/turn off my devices. I had to insert a "wait" for 5 seconds between scripts because of the Error that I am getting from the target. I do not know what this error means but the scripts work. If anyone has any ideas it would be greatly appreciated. I have posted one of the scripts below if it helps anyone. I plan to refine and expand the capabilities over the next few weeks and I will post when it works. Now that I have the basics, the rest is just logic. Thanks for the help.

          Code:
          ' import required modules
          Imports System.Net
          Imports System.Net.Sockets
          Imports System.Text
              Sub Main(ByVal parms as Object)
                  Const Host = "192.168.1.250"
                  Const HostPort = 4998
                  Const LGTVPWRCode = " sendir,1:1,1,37993,1,1,342,170,22,21,22,21,22,63,22,21,22,21,22,21,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,63,22,63,22,63,22,21,22,21,22,21,22,63,22,21,22,21,22,21,22,21,22,63,22,63,22,63,22,21,22,63,22,63,22,63,22,63,22,1514,342,85,22,3654,342,3799" &vbCR
                  Const CISCOPWRON = " sendir,1:1,1,57603,1,1,191,190,48,146,47,145,48,48,47,145,48,145,48,146,47,146,47,146,47,48,47,48,47,48,47,48,47,47,48,146,47,48,47,48,47,48,47,48,47,48,47,146,47,146,47,146,48,2189,191,191,47,146,47,146,47,48,47,146,47,145,48,146,47,146,47,146,47,48,47,5700" &vbCR
                  Const SONYPWRON = " sendir,1:1,1,40064,1,1,96,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,25,24,24,24,24,24,48,24,48,24,24,24,24,841,96,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,24,24,25,24,24,24,48,24,48,24,24,24,24,841,96,24,24,24,48,24,48,24,48,24,24,4000" &vbCR
                  Const SONYPWROFF = "sendir,1:1,1,39936,1,1,96,24,48,24,48,24,48,24,48,24,24,24,48,24,24,25,24,24,24,24,24,24,24,24,48,24,48,24,24,24,24,815,96,24,48,24,48,24,48,24,48,24,24,24,48,24,24,24,25,24,24,24,24,24,24,24,48,24,48,24,24,24,24,815,96,24,48,24,48,24,48,24,48,24,24,3900" &vbCR
                  Dim tcpClient As New System.Net.Sockets.TcpClient()
                  tcpClient.Connect(host, hostport)
                  Dim networkStream As NetworkStream = tcpClient.GetStream()
                  Dim strResponse As String
                  strResponse = "No response"
                  If networkStream.CanWrite Then
                      Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(SONYPWROFF)
                      networkStream.Write(sendBytes, 0, sendBytes.Length)
                      Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                      networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                      Dim returndata As String = Encoding.ASCII.GetString(bytes)
                      strResponse = "Device responded: " & CStr(returndata)
                      hs.WriteLog (strResponse)
                  Else
                      If Not networkStream.CanWrite Then
                          strResponse = "Error: Cannot write data to the device"
                          tcpClient.Close()
                          hs.WriteLog (strResponse)
                      End If
                  End If
                End Sub

          Comment


            #6
            OK, I posted too quick. The Error was caused by the two
            "hs.WriteLog (strResponse)" statements that I have inserted for troubleshooting. I removed both of these statements and all is well now. Here is the working code.
            Code:
            ' import required modules
            Imports System.Net
            Imports System.Net.Sockets
            Imports System.Text
                Sub Main(ByVal parms as Object)
                    Const Host = "192.168.1.250"
                    Const HostPort = 4998
                    Const LGTVPWRCode = "sendir,1:1,1,37993,1,1,342,170,22,21,22,21,22,63,22,21,22,21,22,21,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,63,22,63,22,63,22,21,22,21,22,21,22,63,22,21,22,21,22,21,22,21,22,63,22,63,22,63,22,21,22,63,22,63,22,63,22,63,22,1514,342,85,22,3654,342,3799" &vbCR
                    Const CISCOPWRON = "sendir,1:1,1,57603,1,1,191,190,48,146,47,145,48,48,47,145,48,145,48,146,47,146,47,146,47,48,47,48,47,48,47,48,47,47,48,146,47,48,47,48,47,48,47,48,47,48,47,146,47,146,47,146,48,2189,191,191,47,146,47,146,47,48,47,146,47,145,48,146,47,146,47,146,47,48,47,5700" &vbCR
                    Const SONYPWRON = "sendir,1:1,1,40064,1,1,96,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,25,24,24,24,24,24,48,24,48,24,24,24,24,841,96,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,24,24,25,24,24,24,48,24,48,24,24,24,24,841,96,24,24,24,48,24,48,24,48,24,24,4000" &vbCR
                    Const SONYPWROFF = "sendir,1:1,1,39936,1,1,96,24,48,24,48,24,48,24,48,24,24,24,48,24,24,25,24,24,24,24,24,24,24,24,48,24,48,24,24,24,24,815,96,24,48,24,48,24,48,24,48,24,24,24,48,24,24,24,25,24,24,24,24,24,24,24,48,24,48,24,24,24,24,815,96,24,48,24,48,24,48,24,48,24,24,3900" &vbCR
                    Dim tcpClient As New System.Net.Sockets.TcpClient()
                    tcpClient.Connect(host, hostport)
                    Dim networkStream As NetworkStream = tcpClient.GetStream()
                    Dim strResponse As String
                    strResponse = "No response"
                    If networkStream.CanWrite Then
                        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(SONYPWROFF)
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
                        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                        Dim returndata As String = Encoding.ASCII.GetString(bytes)
                        strResponse = "Device responded: " & CStr(returndata)
                    Else
                        If Not networkStream.CanWrite Then
                            strResponse = "Error: Cannot write data to the device"
                            tcpClient.Close()
                        End If
                    End If
                  End Sub

            Comment

            Working...
            X