Android Question Astreams_NewData being blocked by Timer1

ChocoScope

Member
Licensed User
Longtime User
I am using a timer to trigger my program to send a byte out on the usb port. This byte causes a datastream to be sent back from a microprocessor which is then displayed on the screen. It works fine when I do it manually via a Button_Click but if I replace the button click with a Timer1_Tick, It sends out the single byte (once a second) but Astreams_NewData does not fire up until I disable the timer externally using another Button_Click. The data is definitely being sent in both directions. It seems that the Timer1_Tick subroutine is blocking the Astreams_NewData. Please can you help?

Here is an extract from my code

B4X:
Sub Process_Globals

    Dim Timer1       As Timer
    Dim astreams     As AsyncStreams
    Dim Scan         As Boolean    :    Scan = False

End Sub


Sub Globals

    Dim Ux0         As Float
    Dim Ux1         As Float
    Dim Uy0         As Float
    Dim Uy1         As Float
    Dim Ud          As Float

End Sub


Sub Activity_Create(FirstTime As Boolean)

    If FirstTime  Then       
          Timer1.Initialize("Timer1",1000)
          StartUSB
    astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")        
    End If

Timer1_Tick

End Sub


Sub Timer1_Tick

    Trigger_Stream

End Sub



Sub Trigger_Stream        'send a byte to usb to trigger datastream

    Ux0=GridX0   
    Ud =GridW/100   
    Ux1=GridX0+Ud   
    Message="U"
    astreams.Write(Message.GetBytes("UTF8"))

End Sub


Sub Button5_Click        'enable and disable timer1

    If Scan Then 
       Scan=False
       Timer1.Enable=False
    Else
       Scan=True
       Timer1.Enable=True  
    End If
 
End Sub



Sub Button6_Click        'send a byte to usb to trigger datastream

    Trigger_Stream

End Sub



Sub Astreams_NewData (Buffer() As Byte)
    .....................................................................
    .....................................................................
    For y = 0 To Buffer.Length -1
      Uy1 = Buffer(y)
      cvsGraph.DrawLine(Ux0, Uy0, Ux1, Uy1, Colors.Green, 2)
      Ux0 = Ux1
      Ux1 = Ux1 + Ud
      Uy0 = Uy1
    End If
    .....................................................................
    .....................................................................
End Sub
 

KitCarlson

Active Member
Licensed User
Longtime User
I have used timers to trigger communication requests without problems. In looking at your code I see a couple things.

When I initialize the timer, I also enable in the following line. In your activity_create the software call to Timer1_Tick, may do nothing, because at that point the timer is not enabled.

The button5_click for timer enable/disable may be out of sync, I do not see a visual feedback. I use a checkbox for visual control, it takes place of your scan flag. I have also found adding a small panel to simulate a LED, and toggling visibility, provides a heart beat so the enabled timer can be monitored.
 
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
Message for both Erel and Kit. Thanks for your input both.

Erel ... I have proven that the controller is responding correctly as the manual push button system will respond as quickly as I can press it.

Kit ... In your communication requests, do you use the asyncstreams exception to collect the return data or actually wait for it in your timer routine?
Do you disable the timer after requesting communications? I do not want the timer to kick in as soon as the program opens. I have a button to enable it when required. I put Timer1_Tick into the activity_create because I saw it there in a sample program, but it makes no difference. I left it there for comment. Please explain "out of sync". The button5_click routine is the only place I enable/disable the timer. I like your idea of a toggling "led" to show when the timer is active. I will try that. Thank's

Erel ... Can you tell me if the Timer1_Tick routine is exited to main at End Sub please?
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
By out of sync on Button5: If there is no way to see if Timer1 is enabled when Button5 is pressed, because it toggles when pressed, how do you know the state? A bounce of the button may place it in the opposite state.

I started with the Erel's BT chat example. It in the ChatActivity I made changes to the communications. My NewData responds to packets of data from the embedded system. The packets are framed with special characters, so I know the beginning, end and the packet type. The packet does not always get there in one NewData, so new data adds incoming information to the buffer, and checks for an end, if not it exits. If an end is found, it calls the calls the process data sub, passing the packet type and data. It then removes the packet from the buffer, keeping what may be the start on a new packet. Erel wrote a class for async streams text that is very similar, check that out. My code is perhaps too application specific to share.

More about my system:
While the timer sends periodic requests for plotting and logging, there are also buttons that send direct control requests to the embedded system. The button commands index data tables, modify real-time parameters on the embedded side, and request update information for the android GUI RTI. The packet header information, and command processor on the embedded side, keeps orders correct, the system works flawless. The system logs, plots charts, updates real-time values, and changes data on the embedded side without noticeable lag in the GUI. The period timer is enabled or disabled by checkbox, and a button is used to toggle timer rate values. When the timer interval is changed, the timer is momentarily disabled.

My application is the user interface for an automotive EFI/Ignition controller I developed. The BT at 115,200 Baud on Android and B4A application, is much superior to VB6 PC application at 38,400 baud. I have zero complaints with communication integrity.

I often setup rx, tx communications monitoring when I develop. It provides added debug capability and speeds development.
 
Last edited:
Upvote 0
Top