B4R Question Return byte array from Inline C

tzfpg

Active Member
Licensed User
Longtime User
Hi,

I like to get byte array value from inline c.

How can i do that. I have tried many times but no luck.

Below is my Error Log:
B4X:
B4R Version: 2.51
Parsing code.    (0.00s)
Compiling code.    (0.04s)
Building project    (0.03s)
Compiling & deploying Ino project (WeMos D1 R1 - COM4)    Error
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
[JmDNS(user.local.).State.Timer] WARN javax.jmdns.impl.DNSStatefulObject$DefaultImplementation - Trying to advance state whhen not the owner. owner: null perpetrator: Prober(user.local.) state: probing 1
C:\Users\Admin\Desktop\B4R_I2~1\Objects\bin\sketch\b4r_main.cpp: In function 'void readcard(B4R::Object*)':
b4r_main.cpp:100:32: error: 'class B4R::Array' has no member named 'mfrc522'
   (Byte*)b4r_main::_cardarray->mfrc522.uid.uidByte;
                                ^
exit status 1

Below is my code:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 5000
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private t1 As Timer
    Dim cardArray(4) As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    t1.Initialize("t1_Tick",100)
    Log("AppStart")
    RunNative("init", Null)
    RunNative("ShowReaderDetails", Null)
    reader_value(ReaderStatus)
    t1.Enabled = True
End Sub

Sub reader_value(value As Int)
    If value = 1 Then
        Log("MFRC522 Software Version: 1.0")
    Else If value = 2 Then
        Log("MFRC522 Software Version: 2.0")
    Else If value = 3 Then
        Log("MFRC522 Software Version: Unknown")
    Else If value = 4 Then
        Log("WARNING: Communication failure, is the MFRC522 properly connected?")
    End If
End Sub

Sub t1_Tick
    RunNative("readcardid",Null)
'    Log("cardArray : " , bc.HexFromBytes(cardArray))
End Sub

#if C
#include <Wire.h>
#include "MFRC522_I2C.h"
MFRC522 mfrc522(0x28);
MFRC522::MIFARE_Key key;

void ShowReaderDetails(B4R::Object* o) {
  // Get the MFRC522 software version
  byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  //Serial.print(F("MFRC522 Software Version: 0x"));
  //Serial.print(v, HEX);
  if (v == 0x91)
    //Serial.print(F(" = v1.0"));
    b4r_main::_readerstatus = 1;
  else if (v == 0x92)
    //Serial.print(F(" = v2.0"));
    b4r_main::_readerstatus = 2;
  else
    //Serial.print(F(" (unknown)"));
    b4r_main::_readerstatus = 3;
  Serial.println("");
  // When 0x00 or 0xFF is returned, communication probably failed
  if ((v == 0x00) || (v == 0xFF)) {
    //Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
    b4r_main::_readerstatus = 4;
  }
}

void init(B4R::Object* o) {
    Wire.begin(); 
    mfrc522.PCD_Init();
    
    //MiFare Key
    key.keyByte[0] = 0xFF;
    key.keyByte[1] = 0xFF;
    key.keyByte[2] = 0xFF;
    key.keyByte[3] = 0xFF;
      key.keyByte[4] = 0xFF;
    key.keyByte[5] = 0xFF;
}

void readcardid(B4R::Object* o) {
    if ( ! mfrc522.PICC_IsNewCardPresent() ||  ! mfrc522.PICC_ReadCardSerial())
      {
        return;
     }
    
     Serial.print(F("Card UID:"));
    for (byte i = 0; i < mfrc522.uid.size; i++) {
           Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
    }
  Serial.println();
  (Byte*)b4r_main::_cardarray->mfrc522.uid.uidByte;
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
#End If
 

tzfpg

Active Member
Licensed User
Longtime User
Well, the easiest way is to switch to SPI communication and use the existing B4R-LIB's.
Check the Pin Layout for your WeMos D1.
https://github.com/miguelbalboa/rfid#pin-layout
If you have modified your MFRC522-Board for I2C communication, you have to reverse the modification.

i need to use i2c mfrc522 for my project, i successful get the card id using inline c code, i just dont know how to pass the value to b4r main.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
I define an array:
B4X:
Sub Process_Globals
    Public stringa() As Byte
 End Sub
then use this code to return a string from C
B4X:
#if C
#include <avr/pgmspace.h>
char strx[50];
  B4R::Object* findstring(B4R::Object* o) {
   // Serial.print("Findstring1111");
   b4r_statica::_stringa->data = strx;
   b4r_statica::_stringa->length = strlen(strx);
   return 0;   
}
#end if
}
whatever is in memory at strx will be available in string "stringa" at B4R level
( i assume that module name is "statica.bas").
The strx is a null terminated string.
 
Upvote 0
Top