B4J Question Connexion to VideoProjector in 3 continuous steps

CR95

Active Member
Licensed User
I would like to connect a VP from a NON UI B4J program.
Hardware/network initialization flow is as follows :
- socket Initialize
- socket connect
- wait for socket connected
- send specific pattern to VP
- wait for VP response.

I group all the code in a subroutine as the VP frequently disconnects and my subroutine is used to reconnect from different places in my main program
B4X:
Private Sub ConnectToVP
    If FluxVP.IsInitialized  Then
        FluxVP.Close
    End If
    If Socket.IsInitialized  Then
        Socket.Close
    End If
    Log("Initialise le socket pour communiquer avec l'EPSON")
    Socket.Initialize("SocketVP")
    Socket.Connect(IPEpson,PortEpson,0)            ' No TimeOut
    Wait For SocketVP_Connected(ConnexionOK As Boolean)

'    Revient ici au retour de l'initialisation de la connexion avec l'EPSON
    If ConnexionOK = True Then   
        Log("Connect initial EPSON Sucessful")
        FluxVP.Initialize(Socket.InputStream, Socket.OutputStream, "FluxVP")
    Else
        Log("Connection EPSON failed")
        Return     
    End If
'    Initialise le VP EPSON avec une pattern de démarrage
'    Pattern(string) = ESC/VP.net<DLE><ETX><NUL><NUL><NUL><NUL>
    Dim StartPattern = "4553432F56502E6E6574100300000000" As String
    Log("Envoie la Pattern de démarrage au VP")
    Dim Buffer() As Byte   
    Buffer = BConv.HexToBytes(StartPattern)
    FluxVP.Write(Buffer)
'
    Wait For FluxVP_NewData (Buffer() As Byte)
    Dim RetourVP As String
    Dim MessRetour As String = "Retour du VP après envoi pattern: "
    RetourVP = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    If RetourVP.StartsWith("ESC/VP.net") Then
        VPEpsonOK = 1    ' Connecté
        Log(MessRetour & "OK")
    Else
        VPEpsonOK = 0    ' NON Connecté
        Log(MessRetour & "Retour VP pas conforme")
    End If
    Return     
End Sub

First question : I call the subroutine by ConnectToVP and it works BUT control returns to main program just after
B4X:
Wait For SocketVP_Connected(ConnexionOK As Boolean)
I did not arrive to write a "Wait For .... complete.... "
I get a syntax error
How to keep control in my subroutine because my main program cannot continue execution before the 3 steps of the whole connexion to the VP ?

Second question :
If I modify the subroutine in a "resumable sub" by
- changing its name : Private Sub ConnectToVP As ResumableSub
- adding True or False after each Return
- changing call in main main program by "Wait For (ConnectToVP) Complete (Result As Boolean)"
I got the error : StartMessageLoop is missing.
I made several trials but I did not find where and how I should add this instruction ?

Third question :
How could it be possible to add a timeout test on the whole subroutine if VP never responds ?

Thanks for your help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) [B4X] Resumable subs that return values (ResumableSub)
2)
B4X:
Sub AppStart(...)
 Start
 StartMessageLoop
End Sub

Sub Start
 'Do whatever you like
End Sub

3) There all kinds of ways to implement a timeout. Why are you setting the timeout parameter to 0?
One possible and simple implementation:
B4X:
Sub Start
 Do While True
 MakeConnection
 Sleep(10000)
 Do While  FlagThatTellsUsThatThereIsAConnection = True
   Sleep(10000)
 Loop
'close everything and prepare for new connection
Loop
 
Upvote 0

CR95

Active Member
Licensed User
Thanks Erel
Question 1 : I follow your recommendation BUT I get a compilation syntax error when I use
B4X:
    Wait For SocketVP_Connected (ConnexionOK As Boolean) completed (Return As Boolean)

Question 2 : My subroutine has several exits. Do I have to add "StopMessageLoop" after every "Return" ?

Have a good day
 
Upvote 0
Top