Italian Gestire un evento di un servizio [SOLVED]

Angelo Messina

Active Member
Licensed User
Longtime User
Buona sera, avete qualche consiglio per fare in modo che quando un valore di un servizio (lettura da una porta Bluetooth) è stato acquisito correttamente?

in modo tale che un altro modulo possa andare a leggerlo ed usarlo.

Grazie
 

uniplan

Active Member
Licensed User
Longtime User
Non puoi usare il classico callsub() per richiamare dal servizio la funzione di tuo interesse?
 

Angelo Messina

Active Member
Licensed User
Longtime User
no perché la callsub() o callsub2() prenderebbe il valore anche quando non è stato acquisito quindi lo stesso della precedente lettura.
 

Star-Dust

Expert
Licensed User
Longtime User
Spiega meglio che vuoi ottenere
 

picenainformatica

Active Member
Licensed User
Longtime User
Memorizza in una variabile del servizio l'ultimo valore. Il timer lo controlla e quando cambia genera la chiamata.
 

Star-Dust

Expert
Licensed User
Longtime User
Perché non puoi mettere un CallSub all'interno del timer dopo che ha verificato che ci sia stata la condizione per avviare la chiamata?
 

MarcoRome

Expert
Licensed User
Longtime User
Buona sera, avete qualche consiglio per fare in modo che quando un valore di un servizio (lettura da una porta Bluetooth) è stato acquisito correttamente?

in modo tale che un altro modulo possa andare a leggerlo ed usarlo.

Grazie
Nel servizio:
B4X:
Sub Service_Start (StartingIntent As Intent)
    ......
    test = ACosD(112.345)
    CallSub2(Main, "preleva_valore", test )
End Sub

Nel codice Main ( o in qualsiasi altra activity ):
B4X:
.....
Sub preleva_valore(test As Double)
    Log(${test})
End Sub
.....
 

Angelo Messina

Active Member
Licensed User
Longtime User
Spiego come deve funzionare:
Allora dal Log del Servizio leggo le letture e le esegue bene, metto il valore letto in una variabile in un modulo code il valore letto.
Sub Process_Globals
Dim ValoreLetto As String
End Sub

devo effettuare queste misure una a 2 secondi una a 5 una a 10 una a 15
ma leggo sempre lo stesso valore che nel frattempo dovrebbe essere modificato dal servizio e salvata nella variabile del modulo code.

Ho provato quello che suggerisce MarcoRome stessa cosa
 

MarcoRome

Expert
Licensed User
Longtime User
Spiego come deve funzionare:
Allora dal Log del Servizio leggo le letture e le esegue bene, metto il valore letto in una variabile in un modulo code il valore letto.

devo effettuare queste misure una a 2 secondi una a 5 una a 10 una a 15
ma leggo sempre lo stesso valore che nel frattempo dovrebbe essere modificato dal servizio e salvata nella variabile del modulo code.

Ho provato quello che suggerisce MarcoRome stessa cosa
VEDI QUI
 

Angelo Messina

Active Member
Licensed User
Longtime User
Mi fai un esempio per timer che controlla e quando cambia genera la chiamata?
( una soluzione da provare )
 

Angelo Messina

Active Member
Licensed User
Longtime User
mi avevi già suggerito CallSubUtils
ma mi rallenta tanto che leggo dati vecchi circa 5 secondi (non usabili)
adesso misuro conduttanza in microsecondi
 

Angelo Messina

Active Member
Licensed User
Longtime User
Conduttanza 0.08984
Conduttanza 0.08586
Conduttanza 0.08453
Conduttanza 0.08387
Conduttanza 1.09300
Conduttanza 1.09300
Conduttanza 1.06851
Conduttanza 0.93315
Conduttanza 0.87927
Conduttanza 0.81129
Conduttanza 0.17124
Conduttanza 0.42767
Conduttanza 1.18112
Conduttanza 1.11384
Conduttanza 1.13546
Conduttanza 1.13696
Conduttanza 1.15115
Conduttanza 1.14144
Conduttanza 1.14667
Conduttanza 1.14816
Conduttanza 1.14742
Conduttanza 1.14368
Conduttanza 1.14069
Conduttanza 1.14218
Conduttanza 1.13770
Conduttanza 1.13024
Conduttanza 1.12725
Conduttanza 1.12427
Conduttanza 1.12427
Conduttanza 1.12949
Conduttanza 1.13397
Conduttanza 1.12949
Conduttanza 1.12875
Conduttanza 1.12875
Conduttanza 1.12427
Conduttanza 1.11980
Conduttanza 1.12278
Conduttanza 1.12278
Conduttanza 1.11905
Conduttanza 1.11831
Conduttanza 1.11905
Conduttanza 1.11384
Conduttanza 1.11011
Conduttanza 1.11235
Conduttanza 1.11384
Conduttanza 1.11682
Conduttanza 1.12949
Conduttanza 1.13994
Conduttanza 1.13994
Conduttanza 1.14144
Conduttanza 1.14517
Conduttanza 1.14517
Conduttanza 1.14368
Conduttanza 1.14517
Conduttanza 1.14218
Conduttanza 1.13621
Conduttanza 1.12949
Conduttanza 1.13397
Conduttanza 1.13845
Conduttanza 1.14368
Conduttanza 1.15340
Conduttanza 1.16238
Conduttanza 1.17512
Conduttanza 1.17962
Conduttanza 1.18412
Conduttanza 1.18712
Conduttanza 1.18712
Conduttanza 1.19013
Conduttanza 1.19914
Conduttanza 1.21118
Conduttanza 1.22097
Conduttanza 1.22248
Conduttanza 1.22323
Conduttanza 1.22776
Conduttanza 1.22776

esempio di letture da log del servizio

tutte giuste
 

Angelo Messina

Active Member
Licensed User
Longtime User
metto ogni singola lettura in una variabile di un modulo code
ma quando vado a leggere a tempi diversi. sempre stesso valore
 

Angelo Messina

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   
    Dim BT As BluetoothAdmin
    Dim Serial1 As Serial
    Dim timerBT As Timer
    Dim timerLettura As Timer
    Dim AStreams As AsyncStreams
    Dim tr As TextReader
    Dim MyDeviceName As String
    Dim MacAddress As String
    Private sb As StringBuilder
    Public charset As String = "UTF8"
       
End Sub

Sub Service_Create
   
    If File.Exists(File.DirRootExternal, "Bluetooth.txt") Then
        tr.Initialize2(File.OpenInput(File.DirRootExternal, "Bluetooth.txt"), "Windows-1252")
        MacAddress = tr.ReadLine
        MyDeviceName = tr.ReadLine
        tr.Close
    End If

    ToastMessageShow("Connessione " & MyDeviceName & "  MacAddress "& MacAddress, True)
    ToastMessageShow("Trying to connect to " & MyDeviceName, True)
    timerBT.Initialize("timerBT",5000)
    timerBT.Enabled = False
   
    Try
        sb.Initialize
        BT.Initialize("BT")
        Serial1.Initialize("Serial1")
    Catch
        ToastMessageShow("No BlueTooth Device visible...", False)
    End Try
End Sub

Sub Service_Start (StartingIntent As Intent)
    'Start Bluetooth
    Try
        If BT.IsEnabled = False Then
            BT.Enable
        Else
            BTConnectToDevice
        End If
    Catch
        ToastMessageShow("No BlueTooth Device Connect ...", False)
    End Try
End Sub

Sub BT_StateChanged(NewState As Int,OldState As Int)
    If NewState = BT.STATE_ON Then
        BTConnectToDevice
        Log("BT Connect")
    Else
        Serial1.Disconnect
        timerBT.Enabled = False
        Log("BT Disconnect")
    End If
End Sub

Sub BTConnectToDevice
    Try
        'Serial1.Connect(MacAddress)
        Serial1.Connect3(MacAddress,1)
    Catch
        ToastMessageShow("Device not available",False)
    End Try
End Sub

Sub Serial1_Connected (Success As Boolean)

    If Success = True Then
        ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
        AStreams.Initialize(Serial1.InputStream,Serial1.OutputStream,"AStream")
        timerBT.Enabled = True
    Else
        ToastMessageShow("Connection to " & Serial1.Address & " broken!", False)
        timerBT.Enabled = False
        'Service_Destroy
    End If

End Sub

Sub AStream_NewData (Buffer() As Byte)

    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1

        Dim c As Char = s.CharAt(i)
        If i = 0 And c = Chr(10) Then '\n...And
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            'CallSub2(Training,"ValoreGSR",s.SubString2(start, i))
            'GSR=s.SubString2(start, i)
            start = i + 1
        Else If c = Chr(13) Then '\r
'            CallSub2(Training,"GSROk",s.SubString2(start, i))
            If start =0 And i=7 Then
                IptMain.GSR=s.SubString2(start, i)
                 Log("Conduttanza " & IptMain.GSR)
            End If
            If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next

    If start > 0 Then sb.Remove(0, start)
   
End Sub


Sub timerBT_Tick
    'Communicte with the device here
    'if the device answers, fine
    'if there is no response, communication might be lost
    'Stop the timer and you should call BTConnectToDevice again
End Sub


Sub Service_Destroy
    StopService(Me)
End Sub
 

Angelo Messina

Active Member
Licensed User
Longtime User
IptMain
'Code module
'Subs in this code module will be accessible from all modules.

Sub Process_Globals

Public GSR As String

End Sub








questo è come leggo i risultati

Sub Globals
Dim Lettura5 As String
Dim Lettura7 As String
Dim Lettura10 As String
Dim Lettura12 As String
Dim Lettura15 As String

End Sub

Lettura5=IptMain.GSR
Log("Lettura 5 " & Lettura5)

Attendi(5)

....
...

Attendi(15)
Lettura15=IptMain.GSR
Log("Lettura 15 " & Lettura15)





Sub Attendi(Secondi As Int)
Dim Ti As Long
Ti = DateTime.Now + (Secondi * 1000)
Do While DateTime.Now < Ti
DoEvents
Loop
End Sub
 
Top