pn532

package module
v0.0.0-...-f07b441 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

pn532

通过串口与PN532通信的包 包含一些基础函数

因为基于 nfclib 的库并不方便使用,而市面上能够买到的 PN532 成品多是以串口的方式进行通信,所以我自己整了一个基于串口的库用来和 532 通信。

该库封装了一些基础的函数与命令,比如读写命令,状态命令,等等。具体使用方式可以参考 com_test.go,测试用例已基本覆盖常用功能。

安装

go get github.com/asjdf/pn532

快速开始

package main

import (
	"github.com/asjdf/pn532"
	"log"
)

func main() {
	pn532.Mode = pn532.Release

	log.Print("初始化设备")
	device, err := pn532.QuickInit("COM4")
	if err != nil {
		log.Fatalf("初始化设备失败: %v", err)
	}
	log.Print("初始化成功")

	_, err = device.FirmwareVersion()
	if err != nil {
		log.Fatal(err)
	}
	
	log.Print("准备读取单张卡")
	uid, err := device.ReadPassiveTarget(pn532.ISO14443A)
	if err != nil {
		log.Fatalf("读取单张卡失败: %v", err)
	}
	log.Printf("读取单张卡成功 卡号: % X", uid)
}

Documentation

Index

Constants

View Source
const (
	ISO14443A = 0x00 // 卡片类型
)

Variables

View Source
var DefaultLogger = &defaultLogger{}

Functions

This section is empty.

Types

type Config

type Config struct {
	Port string // 串口号 例如 COM1 或者 /dev/ttyUSB0
	*serial.Mode
	Logger Logger
}

type FrameType

type FrameType int
const (
	UnknownFrame FrameType = iota
	NormalFrame
	ExtFrame
	ACKFrame
	NACKFrame
	ErrorFrame
)

type InfoFrame

type InfoFrame struct {
	PreAmble  byte
	StartCode [2]byte
	Len       byte
	Lcs       byte
	Tfi       byte
	Data      []byte
	Dcs       byte
	PostAmble byte
}

func Decode

func Decode(raw []byte) (*InfoFrame, error)

Decode decode normal frame

func NewNormalFrame

func NewNormalFrame(data []byte) *InfoFrame

func (*InfoFrame) Gen

func (f *InfoFrame) Gen() []byte

type Logger

type Logger interface {
	Infof(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	Debugf(format string, v ...interface{})
}

type Pn532

type Pn532 struct {
	Resp chan *RespFrame
	// contains filtered or unexported fields
}

func Init

func Init(port string) (*Pn532, error)

func InitWithConf

func InitWithConf(conf *Config) (*Pn532, error)

func QuickInit

func QuickInit(port string) (*Pn532, error)

func (*Pn532) Close

func (p *Pn532) Close() error

func (*Pn532) FirmwareVersion

func (p *Pn532) FirmwareVersion() ([]byte, error)

FirmwareVersion 获取固件版本

func (*Pn532) InAutoPoll

func (p *Pn532) InAutoPoll(PollNr, Period byte, Type ...byte) ([]byte, error)

InAutoPoll 读卡 并返回读到的uid PollNr specifies the number of polling (one polling is a polling for each Type j). period (0x01-0x0F) indicates the polling period in units of 150 ms. Type 1 indicates the mandatory target type to be polled at the 1st time.

func (*Pn532) MifareClassicAuthenticateBlock

func (p *Pn532) MifareClassicAuthenticateBlock(uid []byte, blockNum byte, keyType byte, key []byte) (bool, error)

MifareClassicAuthenticateBlock 验证区块密码 keyType 为设置验证A密码或B密码 blockNum为块号

func (*Pn532) MifareClassicReadBlock

func (p *Pn532) MifareClassicReadBlock(blockNum byte) ([]byte, error)

func (*Pn532) MifareClassicWriteBlock

func (p *Pn532) MifareClassicWriteBlock(blockNum byte, data []byte) (bool, error)

func (*Pn532) ReadPassiveTarget

func (p *Pn532) ReadPassiveTarget(cardBaud byte) ([]byte, error)

ReadPassiveTarget 读卡 并返回读到的uid

func (*Pn532) SAMConfiguration

func (p *Pn532) SAMConfiguration(mode byte, timeout byte) (bool, error)

SAMConfiguration 通常传入command.NormalMode,0x17

func (*Pn532) SendCommand

func (p *Pn532) SendCommand(data []byte) (bool, error)

SendCommand 发送命令至pn532 如响应正确 返回true 否则返回false

func (*Pn532) SetParameters

func (p *Pn532) SetParameters(NADUsed, DIDUsed, AutoATR_RES, AutoRATS, ISO14443_4_PICC, RemovePrePostAmble bool) (bool, error)

SetParameters 此命令用于设置 PN532 的内部参数,然后配置其针对不同情况的行为。 NADUsed: Use of the NAD information in case of initiator configuration (DEP and ISO/IEC14443-4 PCD). DIDUsed: Use of the DID information in case of initiator configuration (or CID in case of ISO/IEC14443-4 PCD configuration). AutoATR_RES: Automatic generation of the ATR_RES in case of target configuration. AutoRATS: Automatic generation of the RATS in case of ISO/IEC14443-4 PCD mode. ISO14443-4_PICC: The emulation of a ISO/IEC14443-4 PICC is enabled. RemovePrePostAmble: The PN532 does not send Preamble and Postamble.

func (*Pn532) WaitInfoFrame

func (p *Pn532) WaitInfoFrame() (*InfoFrame, error)

WaitInfoFrame 等待响应帧

func (*Pn532) Write

func (p *Pn532) Write(data []byte) (int, error)

func (*Pn532) WriteFrame

func (p *Pn532) WriteFrame(data []byte) error

type RespFrame

type RespFrame struct {
	Type FrameType
	Raw  []byte
}

type SilentLogger

type SilentLogger struct{}

func (*SilentLogger) Debugf

func (l *SilentLogger) Debugf(_ string, _ ...interface{})

func (*SilentLogger) Errorf

func (l *SilentLogger) Errorf(_ string, _ ...interface{})

func (*SilentLogger) Infof

func (l *SilentLogger) Infof(_ string, _ ...interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL