Italian Comunicare con uno strumento elettronico

FreeWolF

Active Member
Licensed User
Longtime User
Ciao a tutti!

Devo realizzare un piccolo programma che mi permetta di far comunicare il cellulare con una scheda elettronica tramite WIFI.

Per collegarmi a questa scheda ho utilizzato la libreria JHwifi e riesco a collegarmi senza problemi.

Poi ho inizializzato un socket per poter comunicare con la scheda.

Il problema è: non riesco a mandare dati (e soprattutto a riceverli).

Devo spedire questi dati: "0x01 0x08 0x00 0x00 0x00 0x00 0xE0 0x0B" sono ESADECIMALI

E la scheda deve rispondere con gli stessi dati che ho mandato.

Come posso realizzare tutto ciò?

Grazie mille !!!
 

FreeWolF

Active Member
Licensed User
Longtime User
E' un prototipo di una scheda che stanno sviluppando nella mia ditta. In pratica deve comunicare tramite wifi con un cellulare android. Io dal cellulare mando una serie di dati in esadecimale e la scheda deve "rispondere" dandomi gli stessi caratteri che ho spedito
 

Spinter

Active Member
Licensed User
Longtime User
allora secondo me potresti usare una connessione tcp ma dipende cosa puoi fare con la scheda,percaso è arduino?
 

FreeWolF

Active Member
Licensed User
Longtime User
Eccomi!

Ho provato a realizzare il programma, ho dovuto usare anche la funzione asyncstream però, visto che con solo il socket non riesco a mandare i dati allo strumento.

Ecco il codice che ho scritto:

B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

Dim Server As ServerSocket
Dim Socket1 As Socket
Dim AStreams 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.

Private Button1 As Button
Private EditText1 As EditText
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("Main")

If FirstTime Then
Server.Initialize(5555, "Server")
Server.Listen
End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click

Socket1.Initialize("Socket1")
Socket1.Connect("192.168.1.2", 5555, 5000)

End Sub

Sub Socket1_Connected(Connected As Boolean)As Boolean

If Connected = True Then
ToastMessageShow("Connesso",True)
Else
ToastMessageShow("Errore",True)
End If

End Sub


Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)

AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")

If Successful Then
Socket1 = NewSocket

AStreams.write(Array As Byte(0x16,  0x08,  0x00,  0x00,  0x00,  0x00, 0xE3, 0x2C))
ToastMessageShow("Dati inviati",True)
End If


Server.Listen

End Sub

Sub AStreams_NewData (Buffer() As Byte)

Dim a As Byte
a = AStreams.StreamReceived

EditText1.Text = a


End Sub


La stringa di byte "0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x2C", direi che venga mandata correttamente allo strumento, ma non ricevo nessuna risposta da esso. Non so se ho sbagliato qualcosa io nel programma oppure il problema è altrove (visto che è ancora un prototipo, ma penso che sia il mio codice che ha qualche problema).

Quando la connessione tramite socket avviene, mi viene correttamente visualizzato "Connesso". Compare anche la scritta "Dati inviati".

Ora, mi stavo chiedendo: non è che per caso devo usare una libreria per il wifi-direct anzichè collegare tramite wifi il telefono allo strumento "normalmente"? Potrebbe essere questo il problema? Un pò lo escludo però, visto che almeno il socket sembra collegarsi senza problemi....

P.s. L' indirizzo e la porta della scheda sono corretti

Dimenticavo una cosa importantissima: lo strumento come risposta deve restituirmi la stessa stringa che gli ho inviato cioè "0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x2C", non so se con o senza le virgole.
 
Last edited:

Spinter

Active Member
Licensed User
Longtime User
io lo spedisco via stringa!
questo è l'esempio.
pero' bisognerebbe sapere come vuole la risposta!
Questo è un server nella schedina deve esserci un client.

B4X:
Sub send_data(data As String)
If AStreams.IsInitialized = True Then
    Dim buffer() As Byte    
        data=data
        buffer = data.GetBytes("UTF8")  
        AStreams.Write(buffer)        
      End If
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…