Announcement

Collapse
No announcement yet.

Telnet script waiting until end of script to fire

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

  • Telnet script waiting until end of script to fire

    The following script is intended as a power toggle for a projecter using IR over a Telnet interface. There is a virtual device (87) for the power state of the projector. The projector requires a double submission of the power button to turn off (one to start, 1 to confirm). The confirmation shows about 3 seconds after the button is pressed when using a separate remote.

    The sequence to power ON the device is fine. The issue I am having is that the timing is not honored on the power OFF. The intent is that (if 87 is not "ON") the script fires the power signal, pauses for 8 seconds or so, then fires the power signal again.

    What is actually happening is that the script is waiting until all the delay time has lapsed to fire the first sequence. The second is then fired before the confirmation is ready. I have tried this using Thread.sleep as well as hs.WaitSec. I have tried this by putting a pause before the actual SEND line within the telnet session as well as the way it is below (calling a subfunction).

    I can't think of anything further.


    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Imports System.IO
    Imports System.Threading
    
    Private DEBUG_LEVEL As Integer = 2
    
    Sub Main(parms As Object)
                    If hs.isOn(87) Then
                        Telnet(parms)
                Thread.Sleep(15000)
                        Telnet(parms)
                        hs.SetDeviceValueByRef(87, 0, true)
                    Else
                        Telnet(parms)
                        hs.SetDeviceValueByRef(87, 100, true)
                    End If
    End sub
     
    Sub Telnet(ByVal IPTOEXEC As String)
        Dim Client As New TcpClient()
        Dim NetStream As NetworkStream
        Dim Reply As String
    
            Dim IR_KEY As String = "0000 006D 0000 0022 0153 00A9 0016 0015 0016 0040 0016 0040 0016 0015 0016 0015 0016 0015 0016 0015 0016 0040 0016 0040 0015 0040 0016 0015 0016 0040 0016 0015 0016 0040 0016 0040 0016 0015 0016 0040 0016 0040 0016 0040 0016 0040 0016 0040 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0040 0016 0040 0016 0040 0016 0FF1\r"
        
        Try
            If DEBUG_LEVEL > 1 Then hs.writeLog("HDMIControl","Attempting Connection")
            Client.Connect(IPAddress.Parse(IPTOEXEC), 4998)
            If DEBUG_LEVEL > 0 Then hs.writeLog("HDMIControl","Connection Established")
            NetStream = Client.GetStream()
            'Thread.Sleep(100)
        Catch ex As Exception 
            hs.writeLog("HDMIControl","Connection Error: " &  ex.toString)
            return
        End Try
            
            Reply = SendRead(NetStream, IR_KEY)
            Thread.Sleep(100)
                    NetStream.Close()
    
            ' Disconnect
            Client.Close()
    End Sub
    
    Private Function SendRead(ByVal Stream As NetworkStream, ByVal Data As String) As Boolean
        Try
        If DEBUG_LEVEL > 1 Then hs.writeLog("HDMIControl"," Write: " &  Data)
          Dim bytes() As Byte = Encoding.ASCII.GetBytes(Data & vbCrLf)
          Stream.Write(bytes, 0, bytes.Length)
        Catch ex As System.Net.Sockets.SocketException
            If DEBUG_LEVEL > 1 Then hs.writeLog("HDMIControl"," Write: FAIL")
        End Try
        Return True
    End Function
Working...
X