2019年12月22日 星期日

VB2010 Conrol LED ON/OFF & Read MFRV522 RFID UID Code

VB2010 Conrol LED ON/OFF & Read MFRV522  RFID UID Code

下載
http://www.mediafire.com/file/ttnpb0o2okg0elh/RFID_VB_COMPORT_LED.rar/file





==========================Arduino=================================
/*
 * --------------------------------------------------------------------------------------------------------------------
 * Example sketch/program showing how to read new NUID from a PICC to serial.
 * --------------------------------------------------------------------------------------------------------------------

 *
/* Wiring RFID RC522 module
=============================================================================
GND     = GND   3.3V    = 3.3V
The following table shows the typical pin layout used:
Signal        MFRC522     WeMos D1 mini     NodeMcu     Generic
RST/Reset     RST         D3 [1]            D3 [1]      GPIO-0 [1]
SPI SS        SDA [3]     D8 [2]            D8 [2]      GPIO-15 [2]
SPI MOSI      MOSI        D7                D7          GPIO-13
SPI MISO      MISO        D6                D6          GPIO-12
SPI SCK       SCK         D5                D5          GPIO-14
[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
[3] The SDA pin might be labeled SS on some/older MFRC522 boards
=============================================================================
*/
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN  D3     // RST-PIN für RC522 - RFID - SPI - D3
#define SS_PIN   D8     // SDA-PIN für RC522 - RFID - SPI - D8
#define BUILTIN_LED 2   // Arduino standard is GPIO13 but lolin nodeMCU is 2
#define LED       2       // Led in ESP32 at pin 2.lolin nodeMCU is 2

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

// Init array that will store new NUID
byte nuidPICC[4];
String datos;
char charBuf[50];

/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}
/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

void setup() {
  delay(500);
  Serial.begin(9600);
  pinMode(BUILTIN_LED, OUTPUT);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
 
 
  datos = "This code scan the MIFARE Classsic NUID.";
  datos.toCharArray(charBuf, 50);
  delay(100);
  Serial.write(charBuf);
  delay(100);
  Serial.println(""); // to jump lines
  delay(100);
 
  datos = "Using the following key:";
  datos.toCharArray(charBuf, 50);
  delay(100);
  Serial.write(charBuf);
  delay(100);
  Serial.println(""); // to jump lines
  delay(100);
 
  //Serial.println(F("This code scan the MIFARE Classsic NUID."));
  //Serial.print(F("Using the following key:"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  Serial.println(""); // to jump lines
}

void loop() {
  int val;
  while(Serial.available() >0){
     val=Serial.read();
     if(val=='1')
       { digitalWrite(LED,LOW);}
     else if (val=='0')
       {digitalWrite (LED,HIGH); }
   }
  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  //if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    //rfid.uid.uidByte[1] != nuidPICC[1] ||
    //rfid.uid.uidByte[2] != nuidPICC[2] ||
    //rfid.uid.uidByte[3] != nuidPICC[3] ) {
    //Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
 
    Serial.print(F("The NUID tag is:"));
    Serial.print(F("-->In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    //Serial.println();
    Serial.print(F("-->In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  //}
  //else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();

}

=====================VB2010=====================
Imports System.IO
Imports System.IO.Ports


Public Class Form1

    Sub ShowString(ByVal myString As String)
        RichTextBox1.AppendText(myString)
    End Sub

    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myDelegate As New myMethodDelegate(AddressOf ShowString)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Button3.PerformClick() 'click the bottom refresh when the program starts
        LinkLabel1.Links.Add(0, 15, "https://www.make4future.com")
        Dim sports As String() = System.IO.Ports.SerialPort.GetPortNames
        If sports Is Nothing Then
            MsgBox("Can't find Serial Ports in this computer.")
        End If

        For i = 0 To UBound(sports)
            ComboBox1.Items.Add(sports(i))
            ComboBox1.SelectedIndex = 0
        Next

    End Sub


    Private Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click ' clear button
        RichTextBox1.Clear()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked ' link
        System.Diagnostics.Process.Start(e.Link.LinkData.ToString())
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RichTextBox1.TextChanged
        RichTextBox1.SelectionStart = Len(RichTextBox1.Text)
        RichTextBox1.ScrollToCaret()
        RichTextBox1.Select()
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            SerialPort1.Encoding = System.Text.Encoding.Default
            SerialPort1.PortName = ComboBox1.Text
            SerialPort1.Open()
            Label5.Text = "connected"
            Label5.ForeColor = Color.Green
        Catch ex As Exception
            MsgBox("ERROR: " & ex.Message, MsgBoxStyle.Critical, "ERROR !")
        End Try
    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'CONNECT
        Try
            ComboBox1.Items.Clear()
            ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames())
        Catch ex As Exception
            MsgBox("ERROR: " & ex.Message, MsgBoxStyle.Critical, "ERROR !")
        End Try
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'DSICONNECT
        SerialPort1.Close()
        Label5.Text = "disconnected"
        Label5.ForeColor = Color.DarkRed
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  'SEND
        Try
            SerialPort1.Write(TextBox1.Text)
            TextBox1.Clear()
        Catch ex As Exception
            MsgBox("ERROR: " & ex.Message, MsgBoxStyle.Critical, "ERROR !")
        End Try
    End Sub

    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim str As String = SerialPort1.ReadExisting()
        Invoke(myDelegate, str)
    End Sub

End Class

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...