Create a gist now

Instantly share code, notes, and snippets.

anonymous /
Created Mar 30, 2011

What would you like to do?
Flashing tool for the SM130 NFC reader
import logging
import serial
import struct
import sys
import time
def sm130_checksum(packet):
return sum(ord(x) for x in packet) % 256
def build_packet(command, payload):
packet = struct.pack('BBB', 0x00, len(payload) + 1, command)
packet += payload
packet = '\xff' + packet + struct.pack('B', sm130_checksum(packet))
return packet
def send_command(s, command, payload):
packet = build_packet(command, payload)
header, reserved, len, response_to = struct.unpack('BBBB',
assert header == 0xFF
assert reserved == 0x00
response = - 1)
response_checksum =
computed_checksum = build_packet(response_to, response)[-1]
assert computed_checksum == response_checksum
assert command == response_to
return response
def read_image_data(f):
# Discard the header
return [x.strip().decode('hex') for x in f.readlines()]
def flash(s, image):"Enabling update mode.")
assert == '\xff\x00\x02\x95\x00\xff'"Beginning update.")
start_chunk = '\xff\x38\xaa\x55\x33\x68\x98\x0b'
result =
assert result == '\x20', result.encode('hex')
for i, chunk in enumerate(image):
result =
assert result == '\x20', result.encode('hex')"Wrote chunk %d/%d.", i, len(image))"Finalizing update.")
s.write('\xff\x3b\xaa\x55\x33\x68\x98\x0b'.ljust(0x4e, '\0'))
result =
assert result == '\x21', result.encode('hex')
def main(args):
image = read_image_data(open(args[1]))
if len(args) == 3:
rate = 19200
rate = args[3]
s = serial.Serial(args[2], rate)
# Give the module a chance to reset
s.flushInput()"Version before update: %s", send_command(s, 0x81, ''))
flash(s, image)
time.sleep(2)"Version after update: %s", send_command(s, 0x81, ''))
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment