B4J Question Socket in B4J OK but B4A is NG

jinyistudio

Well-Known Member
Licensed User
Longtime User
Following code is b4j. I can read/write data with remote PLC.
Same code i running in B4A. I could connect PLC but i cann't read/write with remote PLC(no any respones).
Why ... ?

P.S This socket library, I try to running in a service !


B4X:
Private client As Socket
Private AStreams As AsyncStreams
Private ReconnectTimer As Timer

Sub Open
    If client.Connected = False Then
        client.Initialize("client")
        Try
            client.Connect(vIP, vPort,30000)
        Catch
            Log("OpenException:"&LastException)
        End Try
    End If
End Sub

private Sub client_Connected (Successful As Boolean)
    ReconnectTimer.Enabled = False
    If AStreams.IsInitialized Then AStreams.Close
    If Successful Then
        mcLog("ClientConnected")
        Try
            AStreams.Initialize(client.InputStream, client.OutputStream, "AStreams")        
            timer1.Enabled=True        
        Catch
            mcLog("ClientConnectedException:"&LastException.Message)
        End Try    
    Else
        mcLog("ClientConnected:"&Successful)
    End If
    DoOnConnected(Successful)
End Sub

// Receive Data From remote PLC

private Sub AStreams_NewData (Buffer() As Byte)
    Dim n1 As Int=Buffer.Length
    If n1 > 0 Then
        For n1=0 To Buffer.Length-1
            Rxbuf.Data(Rxbuf.Count) = Buffer(n1)
            Rxbuf.Count = Rxbuf.Count+1
        Next    
    End If
End Sub

// Write data to remote PLC

private Sub Write2(buffer() As Byte,start As Int,count As Int) As Boolean
    If AStreams.IsInitialized Then
        Try
            Return AStreams.Write2(buffer,start,count)
        Catch
            Log("Write2Exception:"&LastException)
            Return False
        End Try    
    Else
        b4jlib.Throwex("AStream not Initialzed")
        Return False
    End If
End Sub
 
Last edited:

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi, It is Ok when I try it in a Activity.
How could i let this library(PLC Component) running in backgorud(Non-stop) ?

B4X:
    #Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private timer1 As Timer
    Private n1 As Int
    Dim plc As Fx5uNetwork
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("page1")
    plc.Initialize(Me,"plc","192.168.1.123",4096)
    plc.Open   
    plc.Put("D10",1).Read.Cycling=500
    plc.Put("D11",1).Write.Cycling=500
    timer1.Initialize("timer1",500)
    timer1.Enabled=True   
End Sub

Sub plc_connected(Successful As Boolean)
    Log("plc connect:"&Successful)
    'timer1.Enabled=True
End Sub

Sub plc_Received(data As Fx5uDevice)
   
End Sub

Sub timer1_tick
    plc.Write("D12",1,Array As Int(n1))
    n1=n1+1
    If n1>9999 Then n1=1
    plc.Var("D11").Ints(0,n1)   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
Top