Hardware Reference
In-Depth Information
SonMicro Communications
Protocol
The SonMicro readers use a binary communications
protocol to communicate with other devices. The protocol
is similar whether you're communicating over asynchro-
nous serial or I2C. The I2C version looks like this:
If you were reading from memory address 04, the
checksum would be as follows (note that these values are
in hexadecimal):
0x02 (length) + 0x86 (command) + 0x04 (address) = 0x8C
(checksum)
For the asynchronous serial protocol, the header byte is
always 0xFF, and the reserved byte is always 0. They're
not added in the checksum, so you can use the same
commands and calculations as you do for the I2C version,
and just add 0xFF and 0x00 at the beginning. The replies
sent back from the reader follow this same protocol.
Length
Command
Data
Checksum
1 byte
1 byte
N bytes
1 byte
The serial version just adds two extra header bytes. It
looks like this:
There are both Processing and Arduino libraries to handle
this protocol so you don't have to, but it's useful to know
the basics. To start out, write a simple Processing sketch
that sends the command to get the firmware version, and
then reads the reply. Readers come with different firmware
versions, and you might need to change the firmware on
yours (see the sidebar on page 336), so you'll save a lot of
troubleshooting time up front by knowing the firmware
version.
Header Reserved Length Command Data
Checksum
1 byte
1 byte
1 byte
1 byte
N bytes
1 byte
The commands for the reader are single-byte values. The
most common ones are shown below:
byte value
Command
0x80
Reset
When you run this sketch, you can see the responses in
hexadecimal at the top, and in ASCII at the bottom (see
Figure 9-17). The firmware command is the only one that
returns any ASCII, because this is a binary protocol. The
select tag command returns a binary string similar to the
command protocol. It starts with the header and reserved
byte (0xFF, 0x00), then the data length (0x06 if there's a
tag, 0x02 if not), the command received (0x83), the tag
type (0x02 means Mifare classic), four bytes representing
the tag number (0x0A, 0xD4, 0xF0, 0x28, in my case), and
the checksum (0x81). The ASCII representation, on the
other hand, looks like garbage.
X
0x81
Get firmware version
0x82
Seek for tag
0x83
Select tag
0x85
Authenticate
0x86
Read memory block
0x89
Write memory block
0x90
Antenna power (turns on or off antenna)
The length byte indicates the length of the command,
plus any data that goes with it. So, for example, a reset
command—which has no data to follow the command—
has a length of 1. A read command has one byte of data
indicating the address you want to read from, so the length
is 2.
The checksum is an error-checking value. To calculate the
checksum, add together the command and data bytes;
and if the value is greater than 255, take the lowest byte.
For example, Here's the reset command's checksum:
0x01 (length) + 0x80 (command) = 0x81 (checksum)
 
Search WWH ::




Custom Search