Kali ini saya akan menjelaskan mengenai bagaimana cara mengekases RFID Reader ID-12 menggunakan Arduino dengan interface yaitu visual basic 6.0, jadi prinsip kerjanya yaitu mengambil data dari tag card kemudian dikirimkan ke komputer atau interface VB 6 menggunakan Arduino. jadi Arduino hanya berfungsi untuk mengambil data atau membaca data dari tag card yang selanjutnya dikirimkan ke komputer dan diolah di program interface. untuk lebih jelasnya berikut adalah program dan skemanya.
a. Arduino Mega
b. RFID ID-12
c. Program Arduino
void setup() {
Serial.begin(9600); // connect to the serial port
}
void loop () {
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if( Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // if 12 digit read is complete
//Serial.print("5-byte code: ");
for (i=0; i<5; i++) {
if (code[i] < 16) Serial.print("0");
Serial.print(code[i], HEX);
// Serial.print(" ");
}
Serial.println();
//Serial.print("Checksum: ");
//Serial.print(code[5], HEX);
//Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
//Serial.println();
}
bytesread = 0;
}
}
}
Serial.begin(9600); // connect to the serial port
}
void loop () {
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if( Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // if 12 digit read is complete
//Serial.print("5-byte code: ");
for (i=0; i<5; i++) {
if (code[i] < 16) Serial.print("0");
Serial.print(code[i], HEX);
// Serial.print(" ");
}
Serial.println();
//Serial.print("Checksum: ");
//Serial.print(code[5], HEX);
//Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
//Serial.println();
}
bytesread = 0;
}
}
}
d. Program Interface VB 6.0
FORM 1
Private Sub cmdConnect_Click()
Dim port As Integer
On Error GoTo errcode
If MSComm1.PortOpen = False Then
MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.Settings = Combo2.List(Combo2.ListIndex) & ",N,8,1"
MSComm1.PortOpen = True
cmdConnect.Enabled = False
cmdDisconnect.Enabled = True
End If
Exit Sub
errcode:
MsgBox "Port Salah !", vbOKOnly, "Peringatan"
Combo1.SetFocus
End Sub
Private Sub cmdDisconnect_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False
End Sub
Private Sub Command3_Click()
Form2.Show
End Sub
Private Sub Command4_Click()
Unload Me
Unload Form2
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
Combo1.AddItem (i)
Next i
With Combo2
.AddItem "2400"
.AddItem "4800"
.AddItem "9600"
.AddItem "19200"
.AddItem "38400"
.AddItem "56600"
End With
Timer1.Enabled = False
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False
End Sub
Private Sub Timer1_Timer()
MSComm1_OnComm
End Sub
'=======================================
FORM 2
Private Sub Form_Load()
Form2.Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
Dim data As String
Dim buffer As String
If Form1.MSComm1.InBufferCount > 0 Then
buffer = Form1.MSComm1.Input
data = Mid$(buffer, 2, 12)
Form2.Text1.Text = data
'Form1.MSComm1.Output = "A" & Chr$(13)
End If
End Sub
'=======================================
e. VIDEO HASILNYA
No comments:
Post a Comment