Android Question Get data from AsyncStreams

Jan Van Gastel

Member
Licensed User
Hi all,

I am making an App which connects to a PLC (with Telnet session). When the connection is made, the app must send commands to the PLC. The PLC will handle these commands and give a feedback.

A part of my software is working. I can connect, and I can see the data which I receive when I connect to the PLC (interpreter).

But when I send the command, the PLC does not receive anything. But the AStraems_newData is triggered, but he gives me the string i've sended as command. Looks like an echo, but the command is not on my PLC Telnet session (he did not reived anything to execute).

B4X:
Sub Process_Globals

    Dim AStreams As AsyncStreams
    Dim Server As ServerSocket
    Dim Socket1 As Socket
 
End Sub

Sub Globals

Dim TCP_Commando As String

End Sub

Sub Activity_Create(FirstTime As Boolean)

    IPadres = ****
    TCPpoortTELNET = "55555"

If FirstTime Then
        Server.Initialize(TCPpoortTELNET, "Server")
        Server.Listen
          'Log("Mijn IP = " & Server.GetMyIP)
        Socket1.Initialize("Socket1")
          Socket1.Connect(IPadres,TCPpoortTELNET,20000)
    End If
 
    TCP_Commando = ".en" & CRLF

end sub

Sub btn_refresh1_click
 
    ' test voor de telnet/socket verbinding, maakt gebruik van Lib: Network en RandomAccesssFile
 
    If AStreams.IsInitialized = False Then Return
    If TCP_Commando.Length > 0 Then
        Dim buffer() As Byte
        buffer = TCP_Commando.GetBytes("UTF8")
        AStreams.Write(buffer)
        'EditText1.SelectAll
        Log("Verzenden: " & TCP_Commando)
    End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
    ' test voor socket
 
    If UserClosed Then
        Log("Sluiten")
        AStreams.Close
        Socket1.Close
    End If

End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Log("Verbonden")
        Socket1 = NewSocket
        AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
        'AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
    Else
        Log(LastException.Message)
    End If
    Server.Listen
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    'ToastMessageShow(msg, False)
    Log("Ontvangen:")
    Log(msg)
End Sub

Sub AStreams_Error
    Log(LastException.Message)
End Sub

Sub Socket1_Connected(Connected As Boolean)
 
   If Connected = True Then
      Log("PLC Verbonden.")
      AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream,"Astreams")
   Else
      Log("PLC niet bereikbaar.")
   End If
 
End Sub


This is the feedback

B4X:
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
PLC Verbonden.
Ontvangen:


Anerma-CF4-Forth: 2.2.2.4

Serial nbr: A82C00208       IPA: 192.168.0.99

sys. time : 05/08/2016 14:41:57

Unit name : JVG

ok # 0

Verzenden: .en
Ontvangen:
.en

This part is the "header" or interpreter of my PLC, this means the android device is connected

B4X:
Anerma-CF4-Forth: 2.2.2.4

Serial nbr: A82C00208       IPA: 192.168.0.99

sys. time : 05/08/2016 14:41:57

Unit name : JVG

ok # 0

As you can see, it is triggered that I send the command ".en".
But the PLC has to execute ".ex" and give feedback, I need this feedback.
 
Last edited:

Jan Van Gastel

Member
Licensed User
Yes, but is there a time out for sending and receiving ?
Because the interpreter message is received, but when I send a command, it does not reply. Also my PLC does not receive anything. So its seems there is a difference between sending and receiving.

In my example, is it correct with the connection with socket and server ? I think I connect on a good way, I receive first data at a good way, but when I send the data, something goes wrong....
 
Last edited:
Upvote 0
Top