EL-UHF-RC4 via RS232 dengan Arduino untuk Active Inventory

Menggunakan Arduino Uno kita dapat mengambil hasil inventory Reader EL-UHF-RC4-2 dari interface Serial RS232-nya menggunakan Max3232.

Persiapan

Berikut beberapa barang yang diperlukan:

Koneksi antar Pin

Adapun koneksi antar Pin seperti pada gambar berikut.

TTL Header Cable to
RS232 Casing (Male)
Arduino Uno
Yellow (1) - DCD3.3 V
Orange (2) - TXD2
Red (3) - RXD~3
Brown (5) - GNDGND

Kode dan Penjelasan

Penjelasan blok/tiap byte command/response dapat dilihat di dokumentasi protokol.

1. rc4.ino

#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(2, 3); // RX, TX
 
const int HEADER_LENGTH = 5;
const int CHECKSUM_BYTES = 2;
 
void setup() {
  Serial.begin(57600);
  mySerial.begin(57600);
}
 
void loop() {
  if (!mySerial.available()) return;
 
  // 1. Get header
  uint8_t header[HEADER_LENGTH];
  readBytesFromSerial(header, HEADER_LENGTH);
 
  // Serial.print("Header\t-> ");
  // printResponse(header, HEADER_LENGTH);
 
  // CF: Header
  // 00 01: Command
  if (header[0] != 0xCF || header[2] != 0x00 || header[3] != 0x01) {
    Serial.println("Invalid header format");
    return;
  }
 
  //////////////////////////////////////////////
 
  // 2. Get body (length of body in index 4th of header)
  const int bodyLength = header[4] + CHECKSUM_BYTES;
 
  uint8_t body[bodyLength];
  readBytesFromSerial(body, bodyLength);
 
  // Serial.print("Body\t-> ");
  // printResponse(body, bodyLength);
 
  // Validate, status equals to 00
  const int status = body[0];
  if (status != 0x00) {
    Serial.print("Status != 0x00");
    return;
  }
 
  //////////////////////////////////////////////
 
  // 3. Get complete response (combine header + body)
  const int responseLength = HEADER_LENGTH + bodyLength;
  uint8_t response[responseLength];
 
  memcpy(response, header, HEADER_LENGTH);
  memcpy(response + HEADER_LENGTH, body, bodyLength);
 
  // Serial.print("Response-> ");
  // printResponse(response, responseLength);
 
  //////////////////////////////////////////////
 
  // 4. Final: get EPC (tag)
  // 4.1. RSSI
  const uint8_t rssi[2] = {body[1], body[2]};
  int decimalRSSI = rssi[0] << 8 | rssi[1];
  decimalRSSI /= 10;
 
  // 4.2. Antenna
  const int antenna = body[3];
 
  // 4.3. Channel
  const int channel = body[4];
 
  // 4.4. Tag length
  const int tagLength = body[5];
 
  // Validate tag length
  // 8: status(1) + RSSI (2) + Antenna(1) + Channel(1) + Tag length(1) + Checksum(2)
  if (tagLength != bodyLength - 8) {
    Serial.print("Invalid tag length");
    return;
  }
 
  uint8_t tag[tagLength];
  memcpy(tag, body + 6, tagLength);
 
  Serial.print("RSSI: ");
  Serial.print(decimalRSSI);
 
  Serial.print(" Tag -> ");
  printResponse(tag, tagLength);
}
 
void readBytesFromSerial(uint8_t data[], int length) {
  unsigned long startMillis = millis();
  for (int i = 0; i < length; i++) {
    // Wait for data with a timeout
    while (!mySerial.available()) {
      // Check for timeout
      if (millis() - startMillis > 100) {  // Adjust the timeout value as needed
        Serial.println("Timeout waiting for data");
        return;
      }
    }
    // Read a byte from Serial
    data[i] = mySerial.read();
  }
}
 
void printResponse(byte data[], int size) {
  for (int i = 0; i < size; i++) {
    if (data[i] < 16) Serial.print("0");
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}

Jika buka Serial Monitor (57600 bps) akan tampil seperti berikut:

15:00:49.488 -> RSSI: -46 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:49.520 -> RSSI: -48 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:49.596 -> RSSI: -47 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:49.644 -> RSSI: -45 Tag -> 12 34 56 78 90 12 34 56 78 90 A0 01 
15:00:49.691 -> RSSI: -45 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:49.724 -> RSSI: -46 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:49.813 -> RSSI: -53 Tag -> 12 34 56 78 90 12 34 56 78 90 A0 02 
15:00:49.860 -> RSSI: -46 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:49.906 -> RSSI: -47 Tag -> 12 34 56 78 90 12 34 56 78 90 A0 01 
15:00:49.951 -> RSSI: -48 Tag -> 12 34 56 78 90 12 34 56 78 90 A0 01 
15:00:49.998 -> RSSI: -48 Tag -> 12 34 56 78 90 12 34 56 78 90 A0 02 
15:00:50.075 -> RSSI: -47 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:50.117 -> RSSI: -53 Tag -> 12 34 56 78 90 12 34 56 78 90 A0 02 
15:00:50.152 -> RSSI: -53 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:50.195 -> RSSI: -57 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06 
15:00:50.231 -> RSSI: -59 Tag -> F1 75 DC 51 CB BE AF 50 D6 F1 7A 06

Video

Cara atur work mode, dapat dilihat di video berikut: