B4J Question Read data from RFID

PumaCyan

Member
Licensed User
i just tried arduino uno project with rfid module
for the project at B4R it has been going well, the ID data from the card has also been read.

My B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Private rfid As MFRC522
    Private bc As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    rfid.Initialize(10, 9, "rfid_CardPresent")
End Sub

Sub rfid_CardPresent (UID() As Byte, CardType As Byte)
    Log(bc.HexFromBytes(UID))
End Sub

then there was a problem when I created a project in B4J and SQLite, where I had a sql query to match the data from the card earlier

My B4J Code:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private SQL1 As SQL
    Private SenderFilter As Object
    Private UID As String
    '
    Private sp As Serial
    Private astream As AsyncStreams
    '
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    '
    xui.SetDataFolder("TestB4J")
    '
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    sp.Initialize("")
    sp.Open("COM3")
    Dim BaudRate As Int = 115200
    Dim DataBits As Int = 8
    Dim StopBits As Int = 1
    Dim Parity As Int = 0
    sp.SetParams (BaudRate , DataBits , StopBits , Parity )
   
    Log("Awaiting Selection")
    '
    Config_Object
End Sub

Private Sub B4XPage_Appear
    ConnectToDatabase
End Sub

Sub AStream_NewData (Buffer() As Byte)
    UID = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Sleep(1000)
    Cek_Data
End Sub

Sub ConnectToDatabase
    Dim SafeDirectory0 As String = xui.DefaultFolder
    Dim strFile0 As String = "mydata.db"
    '
    If File.Exists(SafeDirectory0, strFile0) = False Then
        File.Copy(File.DirAssets, strFile0, SafeDirectory0, strFile0)
        SQL1.InitializeSQLite(SafeDirectory0, strFile0, False)
    Else
        SQL1.InitializeSQLite(SafeDirectory0, strFile0, False)
    End If
End Sub

Sub Cek_Data
    Try
        SenderFilter = SQL1.ExecQueryAsync("SQL", $"SELECT * FROM userdata
                                                    WHERE rfid = '${UID}'"$, Null)
       
        Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, RS As ResultSet)
        If Success Then
            Do While RS.NextRow
                Dim cd As User
                cd.iduser = RS.GetString2(0)
                cd.rfid = RS.GetString2(1)
                cd.name = RS.GetString2(2)
            Loop
            '
            log(cd.iduser & " -" & cd.name)
            '
            RS.Close
        End If
    Catch
        Log(LastException)
    End Try
End Sub

but the result obtained from the query earlier is :

Result:
(NullPointerException) java.lang.NullPointerException

but if the query that I wrote earlier, I manually paste the data found
Is there anything missing in the process of sending data from Arduino to B4J?
 

teddybear

Well-Known Member
Licensed User
My B4J Code:
Sub Class_Globals
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    sp.Initialize("")
    sp.Open("COM3")
    Dim BaudRate As Int = 115200
    Dim DataBits As Int = 8
    Dim StopBits As Int = 1
    Dim Parity As Int = 0
    sp.SetParams (BaudRate , DataBits , StopBits , Parity )
 
    Log("Awaiting Selection")
    '
    Config_Object
(NullPointerException) java.lang.NullPointerException

but if the query that I wrote earlier, I manually paste the data found
Is there anything missing in the process of sending data from Arduino to B4J?
It is irrelevant to sqlite.
I think the issue is you do astream.Initialize before sp.initalize
Try
B4X:
    sp.Initialize("")
    sp.Open("COM3")
    Dim BaudRate As Int = 115200
    Dim DataBits As Int = 8
    Dim StopBits As Int = 1
    Dim Parity As Int = 0
    sp.SetParams (BaudRate , DataBits , StopBits , Parity )
    Log("Awaiting Selection")
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    Config_Object
 
Upvote 0

PumaCyan

Member
Licensed User
It is irrelevant to sqlite.
I think the issue is you do astream.Initialize before sp.initalize
Try
B4X:
    sp.Initialize("")
    sp.Open("COM3")
    Dim BaudRate As Int = 115200
    Dim DataBits As Int = 8
    Dim StopBits As Int = 1
    Dim Parity As Int = 0
    sp.SetParams (BaudRate , DataBits , StopBits , Parity )
    Log("Awaiting Selection")
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    Config_Object

I have changed it, but the result is still the same
 

Attachments

  • B4R_RFID.zip
    888 bytes · Views: 145
  • B4J_ReadRFID.zip
    3.3 KB · Views: 146
Upvote 0

teddybear

Well-Known Member
Licensed User
I have changed it, but the result is still the same
You should check what UID you got in sub AStream_NewData.
I have tested it is correct in sub cek_Data when UID = "1C0C134A"
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
Hi, I haven't tested the case yet, because I don't have all the conditions for the test.. but I assume that the problem could be in the data type.

I use it on RFID devices, it works flawlessly .. but I have everything stored in DEC data type.

Why do you use a filter to search for data and not a query with a SELECT statement?

They are just suggestions, I don't know if this solves your problem.
 
Upvote 0

PumaCyan

Member
Licensed User
I've found the answer
and it turned out to be very trivial
just add trim at the end of the variable

MyCode B4J:
Sub AStream_NewData (Buffer() As Byte)
    Dim s As String
    s = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
    log(s.trim)
End Sub

Thank you to all of you
 
Upvote 0
Top