B4R Question Asyncstream, timer and SD.stream conflict

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I have three components, Asyncstream, timer and SD.stream, asyncstream reads data from RFID module, timer prevents repeated readings within 5 seconds then I open file that is equivalent to RFID card to read data.

The problem is, before writing SD.stream part, everything was going very good, then it seems that the timer is useless (repeated reading occurs) but I get success reading from SD card. The question is, how to make these three components work together without conflict.

Thanks

B4X:
    RunNative("SerialNative1", Null)
    RunNative("SerialNative2", Null)
    RFIDstream.Initialize(SerialNative2,"ReadRFID","RFIDerr")
    NoReadingTimer.Initialize ("NoReadingTimer_Tick",1000)
    NoReadingTimer.Enabled =True

B4X:
private Sub ReadRFID(Buffer() As Byte)
    Dim tmpBuffer () As Byte
    Dim Str_HexNumber As String
    'Dim Int_Number As ULong
    'Log(BC.HexFromBytes (Buffer))
   
    If BC.ArrayCompare(Buffer,BufferLast) = 0 Then Return'compare last read card to recent one
    BC.ArrayCopy(Buffer,BufferLast)                    'copy last read to recent one, for next comparison
    NoReadingTimerTicks = 0
    NoReadingTimer.Enabled = True
   
    Log("hex buffer:",BC.HexFromBytes (Buffer))
   
   
    tmpBuffer= BC.SubString2(Buffer,3,11) 'get card bytes only, get ride of checksum and start byte
    Str_HexNumber = BC.StringFromBytes(tmpBuffer)
    'Int_Number = Bit.ParseInt(Str_HexNumber, 16) 'convert hex to decimal string
   
    Dim cb (70) As Byte
    Dim filename As String
    filename =    JoinStrings (Array As String("vehicles/",Str_HexNumber, ".vrd"))
    Log(filename)
    If Sd.OpenRead(filename) Then
        Sd.Stream.ReadBytes(cb, 0 ,70)
        Sd.close
        Log("buffer:", cb)
    End If
End Sub

B4X:
private Sub NoReadingTimer_Tick()
    NoReadingTimerTicks = NoReadingTimerTicks + 1 'avoid reading same card withing 5 seconds
    If NoReadingTimerTicks >= 5 Then
        Dim m As Byte
            For m = 0 To 13
                BufferLast(m) = 0  'reset comparison array
            Next
        NoReadingTimer.Enabled =False
    End If
End Sub


if I remove this part every thing goes fine

B4X:
Dim cb (70) As Byte
    Dim filename As String
    filename =    JoinStrings (Array As String("vehicles/",Str_HexNumber, ".vrd"))
    Log(filename)
    If Sd.OpenRead(filename) Then
        Sd.Stream.ReadBytes(cb, 0 ,70)
        Sd.close
        Log("buffer:", cb)
    End If


B4X:
#IF C
void SerialNative1(B4R::Object* unused) {
::Serial1.begin(115200); //<--You can change the baud rate
b4r_main::_serialnative1->wrappedStream = &::Serial1;
}
void SerialNative2(B4R::Object* unused) {
::Serial2.begin(9600); //<--You can change the baud rate
b4r_main::_serialnative2->wrappedStream = &::Serial2;
  }
#END IF
 

Mostez

Well-Known Member
Licensed User
Longtime User
when I get stream I run the timer to prevent same RFID card reading withing 5 seconds(compare current reading with previous one) , however, I will try your idea to compare time and let you know

Thanks
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
I modified the code to this:

B4X:
If TimeTicks >= (TimeTicksLast + 5) Then
        For m = 0 To 13
                BufferLast(m) = 0  'reset comparison array
        Next
    End If
   
    If BC.ArrayCompare(Buffer,BufferLast) = 0  Then  Return'compare last read card to recent one
    BC.ArrayCopy(Buffer,BufferLast)                    'copy recent one to last read, for next comparison
    TimeTicksLast = TimeTicks                        'copy recent time-ticks for comparison

nothing changed, i still have the same problem, though the new method is more readable, shorter and can be used to perform delays.
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
It's almost 3:00 AM now, i could not sleep until i finish this, the problem is, the RFID is quickly sends card information without any kind of delay, that makes Arduino buffers another stream before finishing the one on hand and before going to delay procedure, some times i get Arduino restart on its own I even lose RTC readings.
I made a small repeater (PIC16F648A with small software) to repeat the incoming card code, but after small adjustable delay, it can also check card duplication.

Thanks so much Erel
 
Upvote 0
Top