Get Suica balance in Python (using libpafe)

Get Suica balance in Python (using libpafe)

Operating environment

Library

Environmental setting

  1. Install libusb

    • $ sudo apt-get install libusb-dev
  2. Clone libpafe from GitHub

    • $ git clone https://github.com/rfujita/libpafe.git
  3. Compile & install

    $ cd libpafe
    $ ./configure
    $ make
    $ sudo make install
    
  4. Operation check

    $ cd libpafe
    $ ./tests/pasori_test 
    PaSoRi (RC-S320)
     firmware version 1.40
    Echo test... success
    EPROM test... success
    RAM test... success
    CPU test... success
    Polling test... success
    
  1. udev settings

Get IC card IDm

Read FeliCa IDm with Raspberry Pi + libpafe + Python + ctypes (Almost referring to what was posted here)

read_idm.py


# -*- coding: utf-8 -*-

from __future__ import print_function
from ctypes import *

# libpafe.Defined on line 77 of h
FELICA_POLLING_ANY = 0xffff

if __name__ == '__main__':

	libpafe = cdll.LoadLibrary("/usr/local/lib/libpafe.so")

	libpafe.pasori_open.restype = c_void_p
	pasori = libpafe.pasori_open()

	libpafe.pasori_init(pasori)

	libpafe.felica_polling.restype = c_void_p
	felica = libpafe.felica_polling(pasori, FELICA_POLLING_ANY, 0, 0)

	idm = c_ulonglong()
	libpafe.felica_get_idm.restype = c_void_p
	libpafe.felica_get_idm(felica, byref(idm))

	#IDm is in hexadecimal notation
	print("IDm:", "%016X" % idm.value)

	#From the README, felica_polling()Free after use()use
	#In addition, free seems to be automatically included in the library
	libpafe.free(felica)

	libpafe.pasori_close(pasori)

Get your Suica balance

read_balance.py


# -*- coding: utf-8 -*-

from __future__ import print_function
from ctypes import *

# libpafe.Defined on line 77 of h
FELICA_POLLING_ANY = 0xffff

#Definition of a class to replace the structure
class felica_block_info(Structure):
	_fields_ = [
		("service", c_uint16),
		("mode", c_uint8),
		("block", c_uint16)
	]

if __name__ == '__main__':

	libpafe = cdll.LoadLibrary("/usr/local/lib/libpafe.so")

	libpafe.pasori_open.restype = c_void_p
	pasori = libpafe.pasori_open()

	libpafe.pasori_init(pasori)

	libpafe.felica_polling.restype = c_void_p
	felica = libpafe.felica_polling(pasori, FELICA_POLLING_ANY, 0, 0)

	#Definition of C int type array(Length 16)
	int_array16 = c_uint8 * 16

	#Response data
	data = int_array16()
	#List of service codes
	info = felica_block_info(c_uint16(0x090f), c_uint8(0), c_uint16(0))
	for i in range(0, 32):
		c_i = c_int(i)
		libpafe.felica_read(felica, byref(c_i), byref(info), byref(data))
		if (data[1] > 0) or (data[2] > 0):
			print("Balance:", data[11] * 256 + data[10], "Circle")
			break

	libpafe.free(felica)

	libpafe.pasori_close(pasori)

reference

Recommended Posts

Get Suica balance in Python (using libpafe)
Get Youtube data in Python using Youtube Data API
Get date in Python
Get image URL using Flickr API in Python
Get YouTube Comments in Python
Python --bitflyer Get asset balance
Get last month in python
Get LEAD data using Marketo's REST API in Python
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Translate using googletrans in Python
Using Python mode in Processing
Get Japanese synonyms in Python
Get Python scripts to run quickly in Cloud Run using responder
Get Leap Motion data in Python.
GUI programming in Python using Appjar
Get data from Quandl in Python
Get the desktop path in Python
Try using LevelDB in Python (plyvel)
Get the script path in Python
Using global variables in python functions
Get, post communication memo in Python
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Get the desktop path in Python
Get the host name in Python
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
Get started with Python in Blender
When using regular expressions in Python
GUI creation in python using tkinter 2
I get a can't set attribute when using @property in python
Get additional data in LDAP with python
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Get exchange rates from open exchange rates in Python
GUI creation in python using tkinter part 1
(Bad) practice of using this in Python
Get tweets containing keywords using Python Tweepy
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
[FX] Hit oanda-API in Python using Docker
Get mail using Gmail API in Java
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
Get Google Fit API data in Python
I tried using Bayesian Optimization in Python
Log in to Slack using requests in Python
How to get a stacktrace in python
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Get battery level from SwitchBot in Python
Develop slack bot in python using chat.postMessage
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz