Sub Process_Globals
Private fx As JFX
Private SP As Starchild_SerialCommsClass
Private TryAgainSubroutine As String
End Sub
Sub AppStart (Form1 As Form, Args() As String)
SP.Initialize(Me,"SP","starchild/b4x/free")
TryAgainSubroutine = ""
OpenSerialCommPortToEquipment
'OpenIpNetworkPortToEquipment
End Sub
'Search for an equipment that is connected to
'any ONE of the available Serial Comm ports.
'Validate the equipment is present by sending it
'the text message "Hello".
'On receiving a valid reply
'(in this case loopbaqck of "Hello") the port
'is then deemed to be Connected.
'
'if NO port is available, or the equipment does
'not provide a valid response, then after 2 seconds
'try searching again.
Sub OpenSerialCommPortToEquipment
Log("OpenSerialCommPortToEquipment")
TryAgainSubroutine = "OpenSerialCommPortToEquipment"
'SP.Open("COM1",0,True,9600,8,1,SP.PARITY_NONE)
SP.Open("AUTO",0,True,9600,8,1,SP.PARITY_NONE)
SP.FlowControl(False,True) 'use Xon/Xoff flow control
End Sub
'Open an IP network port to the equipment and
'validate the presence of the equipment.
Sub OpenIpNetworkPortToEquipment
Log("OpenIpNetworkPortToEquipment")
TryAgainSubroutine = "OpenIpNetworkPortToEquipment"
SP.SetTcpNoDelay(True) 'disable Nagle's Algorithm for immediate data transmission
SP.Open("10.0.0.238",800,True,0,0,0,0)
End Sub
'Send a Command to get back an expected reply
Sub SP_DoValidationRequest
Log("SP_DoValidationRequest")
SP.TxData("Hello") 'test for valid connected equipment
End Sub
'the returned reply
Sub SP_AnalyseValidationResult(ReturnedData As String) As Boolean
Log("SP_AnalyseValidationResult")
Log(" ReturnedData: " & ReturnedData)
Return (ReturnedData = "Hello") 'a valid connection is established
End Sub
Sub SP_Connected(Success As Boolean)
If Success = True Then
Log("SP_Connected(" & SP.CURRENT_PORT & "): " & Success)
Else
Log("SP_Connected(): Equipment Not Found")
Log(" Last Error: " & SP.LastError)
Sleep(2000)
CallSub(Me,TryAgainSubroutine)
End If
End Sub
Sub SP_HasClosed(ErrorIfAny As String)
Log("Closed: " & ErrorIfAny)
End Sub
Sub SP_RxData(Data As String)
Log("RxData: " & Data)
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub