この項目では、SM130用のライブラリを用いずにSM130を制御する方法を説明する。
一部デバイスの制御を簡単にするため、Windows APIを用いている。
プログラムを開発した環境は、VisualStudio.netを用いた。
また、CやVisualStudioの文法や仕様に関しての説明は含んでいない。
Windos OSではなく他の組み込みデバイスを用いる場合には、そのデバイス独自のI/Oを用いて以下のパケットをSM130に投げれば制御することができる。
#include <windows.h>
// HANDLE mifareHandle;
unsigned char readPacket[20]; // array for receive
unsigned char writePacket[] = "hoge"; // array for send
DWORD size = 5; // 目的の長さ
DWORD receivedSize; // 実際に読み取ったバッファの長さ
// send "size" size "writePacket" data
if( !WriteFile(mifareHandle, writePacket, size, &receivedSize, NULL) ) {
return; // return error
}
size = 20;
// receive "size" size data to "readPacket"
if( !ReadFile(mifareHandle, readPacket, size, &receivedSize, NULL) ) {
return; // return error
}
また、SM130のパケットフォーマットは次のようになっている。Header | Reserved | Length | Command | Data | Check Sum |
---|---|---|---|---|---|
1 byte | 1 byte | 1 byte | 1 byte | N byte | 1 byte |
HANDLE mifareHandle;
mifareHandle = CreateFile(
"COM1:", // open COM1 port
GENERIC_READ|GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
NULL );
DCB dcb; // status of device
if( !GetCommState( mifareHandle, &dcb ) ) {
printf("error: can not get dcb");
CloseHandle(mifareHandle);
return;
}
dcb.BaudRate = CBR_19200; // 19200 bps
dcb.ByteSize = 8;
dcb.Parity = NOPARITY; // no parity check
dcb.StopBits = ONESTOPBIT;
if( !SetCommState( mifareHandle, &dcb ) ) {
printf("error: can not set dcb");
CloseHandle(mifareHandle);
return;
}
これでCOM1からデータの送出を行えるようになる。
unsigned char packet[6];
unsigned char res[10];
DWORD size;
//-- set seek command
packet[0] = 0xFF;
packet[1] = 0x00;
packet[2] = 0x01;
packet[3] = 0x82;
packet[4] = 0x83;
// send packet to COM1
if( !WriteFile(mifareHandle, packet, 5, &size, NULL) ) {
printf("seek error");
return;
}
// receive 1st respoinse from COM1
for( int i=0; i<6; i++ ) { res[i] = 0x00; }
if( !ReadFile(mifareHandle, res, 6, &size, NULL) ) {
printf("error: can not get Response1");
return;
}
if( res[4]!= 0x4C ) {
printf("SEEK command does not execute correctly");
return;
}
// receive 2nd response from COM1
for( int i=0; i<10; i++ ) { res[i] = 0x00; }
if( !ReadFile(mifareHandle, res, 10, &size, NULL) ) {
printf("error: can not get Response2");
return;
}
//--- authenticate ---
unsigned char packet[22];
unsigned char res[22];
int blockNum = 4; // access to 5th block
packet[0] = 0xFF; // Header
packet[1] = 0x00; // Reserved
packet[2] = 0x09; // Length
packet[3] = 0x85; // Command
packet[4] = blockNum;
packet[5] = 0xAA; // key type A
// set authenticate key
for( int i=0; i<3; i++ ) {
packet[i*2 +6] = 0xD3;
packet[i*2+1+6] = 0xF7;
}
// calculate check sum
packet[12] = 0x00; // check sum
for( int i=2; i<12; i++ ) { packet[12] += packet[i]; } //packet[12] = 0x9A;
// send packet to COM1
if( !WriteFile(mifareHandle, packet, 13, &size, NULL) ) {
printf("authenticate error");
return;
}
// receive response packet from COM1
for( int i=0; i<6; i++ ) { res[i] = 0x00; }
if( !ReadFile(mifareHandle, res, 6, &size, NULL) ) {
printf("error: can not get Authenticate response");
return;
}
if( res[4] != 0x4C ) {
printf("error: Authentication failure");
return;
}
//--- write block ---
packet[0] = 0xFF;
packet[1] = 0x00;
packet[2] = 0x12; // packet length
packet[3] = 0x89; // command
packet[4] = blockNum;
packet[21] = 0x12 + 0x89 + blockNum; // check sum;
for(int i=0; i<16; i++) { // writing data
packet[5+i] = data[i];
packet[21] += data[i];
}
//---
if( !WriteFile(mifareHandle, packet, 0x22, &size, NULL) ) {
printf("write block error");
return;
}
for( int i=0; i<22; i++ ) { res[i] = 0x00; }
if( !ReadFile(mifareHandle, res, 22, &size, NULL) ) {
printf("error: can not get WriteBlock response");
return;
}
/**
//--- read block ---
packet[0] = 0xFF;
packet[1] = 0x00;
packet[2] = 0x02;
packet[3] = 0x86;
packet[4] = blockNum;
packet[5] = 0x02 + 0x86 + blockNum;
//---
if( !WriteFile(mifareHandle, packet, 6, &size, NULL) ) {
printf("read block error");
return false;
}
for( int i=0; i<22; i++ ) { res[i] = 0x00; }
if( !ReadFile(mifareHandle, res, 22, &size, NULL) ) {
printf("error: can not get ReadBlock response");
return;
}
if( res[2]==2 ) {
printf("error: can not Read Block");
return;
}
*/
CloseHandle( mifareHandle );