B4J Question Read data from RFID

PumaCyan

Active Member
Licensed User
Longtime 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.

[CODE lang="b4x" title="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[/CODE]

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

[CODE lang="b4x" title="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[/CODE]

but the result obtained from the query earlier is :

[CODE lang="b4x" title="Result"](NullPointerException) java.lang.NullPointerException[/CODE]

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?
 
[CODE lang="b4x" title="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[/CODE]
(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
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

Upvote 0
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
I've found the answer
and it turned out to be very trivial
just add trim at the end of the variable

[CODE lang="b4x" title="MyCode B4J" highlight="4"]Sub AStream_NewData (Buffer() As Byte)
Dim s As String
s = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
log(s.trim)
End Sub[/CODE]

Thank you to all of you
 
Upvote 0
Back
Top