Send byte from arduino to java

Hello im working on a project for myself. i have serveral RFID cards. I want to send a RFID code(cardID) to my arduino and then scan a random rfid card. if the sent RFID code and the one scaned are matching i want to send back a string or just one byte back to my java programm.

so far sending to my arduino and checking the RFID card is working fine however im stuck at sending something back to my java programm. i was thinking about using serielConnection but tbh its very difficult to understand. can someone help me out ? thanks in advance

my java code

    package test;

import java.io.IOException;

import com.fazecast.jSerialComm.SerialPort;

public class Startup {

    public static void main(String[] args) throws IOException, InterruptedException {
        SerialPort sp = SerialPort.getCommPort("COM4"); // device name TODO: must be changed
        sp.setComPortParameters(115200, 8, 33, 34); // 9600,8,1,0default connection settings for Arduino
        sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0); // block until bytes can be written
        
        if (sp.openPort()) {
          System.out.println("Port is open :)");
        } else {
          System.out.println("Failed to open port :(");
          return;
        }       
        
        /*for (Integer i = 0; i < 5; ++i) {         
          sp.getOutputStream().write(i.byteValue());
          sp.getOutputStream().flush();
          System.out.println("Sent number: " + i);
          Thread.sleep(4000); //default 1000
        }       
        */
        //rfid tag  0x82,130
        //rfid card 118
        
        Integer i = 0;//0x82,130
        sp.getOutputStream().write(i.byteValue());
          sp.getOutputStream().flush();
          System.out.println("Sent number: " + i);
          Thread.sleep(8000);
        
        
        
        if (sp.closePort()) {
          System.out.println("Port is closed :)");
        } else {
          System.out.println("Failed to close port :(");
          return;
        }
        
        //from arduino
        
     
      }

}

arduino code:

    #include <SPI.h>
#include <MFRC522.h>

#define RST_PIN   26  
#define SS_PIN    5  
#define MISO_PIN  19 
#define MOSI_PIN  23 
#define SCK_PIN   18 


byte incomingByte = Serial.read();

MFRC522 mfrc522(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

void setup() {
    Serial.begin(115200);       // Initialize serial communications with the PC
    pinMode(16,OUTPUT);
    pinMode(21,OUTPUT);
    while (!Serial);        // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN);
    mfrc522.PCD_Init();     // Init MFRC522
    delay(40);               // Optional delay. Some board do need more time after init to be ready, see Readme
    mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details

    //dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
}

void loop() {
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
    }
    if(Serial.available() > 0){
  byte incomingByte = Serial.read();
  
    if (mfrc522.uid.uidByte[0] == incomingByte )/*&&*/ //それぞれ読んだカードの値と比較したい16進数の値を比較
             ) 
    {
        digitalWrite(16,HIGH);
        delay(5000);
        digitalWrite(16,LOW);
        delay(50); 

        
    }
    else{

      digitalWrite(21,HIGH);
      delay(2000);
      digitalWrite(21,LOW);
    }
    }

}

Thanks

seems like a question for a JAVA forum.... there are many online tutorials

here is a first hit

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.