Android Question Felusbserial missing bytes

saunwin

Active Member
Licensed User
Longtime User
Good evening.

I have a micro sending a byte every 10ms (max) at 9600 baud thru an ftdi device.
Four different bytes are sent 0x0X, 0x1X, 0x2X, 0x3X and 0x4X
upper nibble (0,1,2 = time counting down in secs, 10th of sec and 100's of sec)
Lower nibble is 0-9
0x3X and 0x4X are the scores (tens and units), again X is 0-9.

The code below works, but misses bytes - Realterm for PC shows me the bytes are there B4A logs show me the bytes are missing.

Am I asking too much ?
Do I need to make an array ?
can I speed things up anyway ?
Thanks in advance.


B4X:
Private Sub serial_DataAvailable (Buffer() As Byte)

    Dim y As Int
        y=ToUnsigned(Buffer(0))
    Log("y= " & y )
    
   Select True
        
        Case y<10
            TimeH.Text=y
            
        Case y<32
            y=y-16
            TimeT.Text=y
            
        Case y<48
            y=y-32
            TimeU.Text=y
        
        Case y<64
            y=y-48
            CountH.Text=y
            
        Case y>63
            y=y-64
            CountL.Text=y
        
    End Select
    
End Sub


Sub ToUnsigned(b As Byte) As Int
    Return Bit.And(0xFF, b)
End Sub
 

JordiCP

Expert
Licensed User
Longtime User
Perhaps there are additional timing issues, but the mistake here is that you are not processing all the incoming buffer (you are assuming that buffer length will be one and this is not necessarily true)

Try this
B4X:
Private Sub serial_DataAvailable (Buffer() As Byte)

  For k=0 to Buffer.len-1   
    
    Dim y As Int = ToUnsigned(Buffer(k))
    Log("y= " & y )
    
    Select True
        
        Case y<10
            TimeH.Text=y
            
        Case y<32
            y=y-16
            TimeT.Text=y
            
        Case y<48
            y=y-32
            TimeU.Text=y
        
        Case y<64
            y=y-48
            CountH.Text=y
            
        Case y>63
            y=y-64
            CountL.Text=y
        
    End Select
  Next  
End Sub


Sub ToUnsigned(b As Byte) As Int
    Return Bit.And(0xFF, b)
End Sub
 
Upvote 0
Top