Android Question Sending data from app to another how?

camolas

Member
Licensed User
Longtime User
Hi,
Im using a python scrit to read incoming sms from a usb gsm modem ( for now i cant put to work in b4a ) in the py i have this to creat a " tunel"
B4X:
import sys
from socket import socket, AF_INET, SOCK_DGRAM

SERVER_IP   = '127.0.0.1'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))

mySocket = socket( AF_INET, SOCK_DGRAM )

while True:
        mySocket.sendto('cool',(SERVER_IP,PORT_NUMBER))
sys.exit()

How can i "catch" the data to a b4a and display it?
Thankz
 
Last edited:

camolas

Member
Licensed User
Longtime User
Hi,

With your py-server i can connect the log say "connected" i change "conn.send(data)" to "conn.send("Hello")" but the log dont show "Hello" any idea?

If i change this part:
B4X:
#while 1:
    #data = conn.recv(1024) # wait for some data
    #if not data: break  # if no data, break
    #conn.send(data)  # resend the same data (is an echo server, or you can conn.send('Hallo world')
conn.send('Hallo world')
conn.close()  #ok, close the connection (not the server)

The problem sims to be on
B4X:
while 1:
     data = conn.recv(1024) # wait for some data
     if not data: break  # if no data, break

Many thx Sir :)
 
Last edited:
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Ok. A good step forward. At this moment we have established a connection between the py server and the b4a client. Now we have exchange some data.
I've never programmed in python, so I need to check out that part of code.
In the meantime, if you want, you can cut your change (get back to my original py server version which resend 'data'). Instead of 'if not data: break' write 'if data="quit": break' (find the correct python syntax) so it will exit the echo loop only when receives a 'quit' string.
Then add a text box and a button so you can write something and send the string when you press the button. You can copy the send routine from the example linked at post 38.

I'll be back tomorrow. But first a question. Why you need the python code? I mean, if the Gsm transmits through USB, why don't you read the USB directly with b4a instead to have the gsm talking to py which talks to b4a...

And one last thing: please don't call me Sir. You can call me Colonel :cool: (hope you understand the joke)
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Hi,

I liked the joke :) , Now "Colonel" ;) im using the python because my usb gsm model (wahai 220 gsm modem with the boot desable to not load the internal software and work as serial com) seems to not work with USB b4a because dont recognize, with UsbSerial 2.3 i get

Manufacturer: not available
Product: not available
Serial: not available
DeviceName :/dev/bus/usb/001/002
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Ok, let's go on with our tries.
Now we can connect, but we cannot exchange data.

In you original py code you wrote:

B4X:
while 1:
    data = conn.recv(1024) # wait for some data
    if not data: break  # if no data, break
    conn.send(data)  # resend the same data (is an echo server, or you can conn.send('Hallo world')
conn.send('Hallo world')

For what I see the while loop checks if there is some data available. But IF NOT DATA: BREAK I suppose will will exit the loop.
So I suggest a first test. Replace the last line conn.send('Hallo World') with print ('exit from waiting loop')
This way you can test if the py program stay in the loop waiting or it exists immediately.

The second test is to write your py routine as follow (hope the syntax is correct)

B4X:
while 1:
    data = conn.recv(1024) # wait for some data
    if data = 'quit': break
    if data: conn.send(data)  # resend the same data (is an echo server, or you can conn.send('Hallo world')
print ("End of loop - End routine")

The meaning of this code is:
- Start a never-endig loop with While 1 (or while true, if you prefer)
- Wait for some data
- if the string received is "quit" then break the loop (exit) - is correct data='quit' or is data="quit" (single or double ' ?)
- If there is some data, send back the same data (just echo)
- When you exit the loop, print something

If this works, you should have a 'Connected' in the b4a log
After the 'connected' just send something to the py server and the py server should echo it back
If you send the string 'quit' the py server will exit the wait loop and print "End of Loop..."

Tell me if it works or not...
 
Upvote 0

camolas

Member
Licensed User
Longtime User
1 test dont print 'exit from waiting loop' while runing the b4a at same time, when i close the b4a client it prints one time

2 test must be "if data == 'quit': break" not working

I will find a way to solve this dont worry more ;), now im going to try to macke the textbox to print the data from server from time to time... if you can hellp with be great :)

Thx
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Of course I'll help.
But probably we have mess too much with the b4a code...
Are you sure that you are transmitting something after the connection.
Post the b4a code. Probably at this very moment we have the py server listening and accepting connections, the b4a code which connects to the py server but then nothing is transmitted...
 
Upvote 0

camolas

Member
Licensed User
Longtime User
The code im using

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim clientsocket As Socket
    Dim SocStream As AsyncStreams
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("Layout1")
    clientsocket.initialize("clientsocket")
    clientsocket.Connect("127.0.0.1",5000,15000)
End Sub
Sub Clientsocket_Connected(Connected As Boolean) As Boolean
   If Connected = True Then
      Log("connected")
      SocStream.Initialize(clientsocket.InputStream,clientsocket.OutputStream,"SocStream")
   Else
       Log("not connected")
   End If
End Sub
Sub SocStream_NewData (Buffer() As Byte)
    'Received new data
    Dim msg As String
    Dim cMsg As String      
    Dim unsignedi As Int
    Dim signedb As Byte
  
    msg = BytesToString(Buffer, 0, Buffer.Length, "ASCII")  
    For i = 0 To msg.Length -1
        signedb = Buffer(i)
        unsignedi = Bit.AND(0xff, signedb)
        cMsg = cMsg & Chr(unsignedi)
    Next
    Log("Received " & cMsg)
  
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Ok. As you can see, the py server is in listening mode accepting connection.
The b4a client (the code you posted in the previous post 47) connects to the server. And the connection is successful (you see the "connected" in the b4a log)

But after this nothing happens. Nor the py server nor the b4a client are transmitting anything. The py server is waiting for incoming data (and if data is received from the client, it will echo it back to the client). But the client don't transmit anything.

So, I will just add a sending routine to the client's code. This way you'll can transmit some data and see if the py server will echo back the data transmitted.

I'll be back with the code in a couple of hours (after dinner, Italian time)
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Ok, here is the b4a simple socket client.
It has a very small display interface where yoy can see a small log and send messages to the py server. There is also a "End App" button which send a "quit" message to the py server and then ends the app.
 

Attachments

  • ClientSocket.zip
    8.2 KB · Views: 179
Upvote 0

camolas

Member
Licensed User
Longtime User
You are Nº1 !! great code :) do you now a way to run (lunch) the server-py script from your ClientSocket?
 
Last edited:
Upvote 0
Top