รับข้อมูลอนุกรม USB ใน Arduino ร่วมกับเครื่องอ่าน RFID

ฉันกำลังทำโครงการ Arduino บอร์ด Arduino จะบันทึกการประทับเวลาและรหัส RFID ลงใน ROM เมื่อสแกนแท็ก RFID ข้อมูล RFID ได้รับภายใน Serial.read() แต่ตอนนี้ฉันต้องรับข้อมูลที่จัดเก็บจาก Arduino ROM ไปยังคอมพิวเตอร์ของฉัน

ในการทำเช่นนั้น ฉันต้องอ่านข้อมูลอนุกรม (ในกรณีนี้คือใช้ C#) และนั่นก็ใช้ได้ดี แต่เนื่องจากบอร์ด Arduino ของฉันต้องรู้ว่าเมื่อใดควรเขียนข้อมูลลงซีเรียล ฉันจึงต้องส่งคำสั่งไปยังบอร์ด Arduino ก่อน

เมื่อใช้ 'Serial Monitor' ของ Arduino IDE ฉันพยายามส่งข้อมูลบางอย่าง แต่ Arduino ไม่ได้รับ เมื่อฉันวางแท็ก RFID ไว้หน้า RFID บอร์ด Arduino จะรับข้อมูล สำหรับฉันดูเหมือนว่าอินพุตพอร์ตอนุกรมนั้นถูกสงวนไว้โดยเครื่องอ่าน RFID หรือไม่ เป็นไปได้ไหม?

มีใครรู้วิธีแก้ปัญหาของฉันหรือไม่? โปรดทราบว่านี่เป็นโครงการ Arduino โครงการแรกของฉันและ C เป็นโครงการใหม่สำหรับฉัน

ข้อมูลบางส่วน:

  • Arduino UNO
  • เครื่องอ่าน RFID: นวัตกรรม ID12
  • โมดูลนาฬิกา RTC
  • C
  • C#
  • .สุทธิ

รหัสด้านล่างคือโครงการ Arduino ทั้งหมด

#include "Wire.h"
#include <EEPROM.h>
#include "EEPROMAnything.h"

#define DS1307_ADDRESS 0x68
byte   zero = 0; //workaround for issue #527
int    buttonState = 0;
byte   value;
int    byteaddress = 0;
int    bytesize = 0;
int    lastTag = 0;
byte   incomingByte;

struct config_t
{
    int seconds;
    int hours;
    int minutes;
} timestamp;

int  RFIDResetPin = 13;

//Register your RFID tags here
char tag1[13] = "03000DEB55B0";
char tag2[13] = "03000DB88137";
char tag3[13] = "03000DC8E026";
char tag4[13] = "03000623FBDD";
char tag5[13] = "03000DB8B701";

void setup()
{
  pinMode(7, INPUT);
  Wire.begin();

  Serial.begin(9600);

  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  EEPROM_readAnything(0, timestamp);
  setDateTime(); //MUST CONFIGURE IN FUNCTION

  pinMode(11, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(7);

  char tagString[13];
  int index = 0;
  boolean reading = false;

  if (buttonState == HIGH) {   
    // turn LED on:    
    printDate(); 
    delay(200);
  }

  while(Serial.available()){

    buttonState = digitalRead(7);

    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }

    incomingByte = Serial.read();
    Serial.println(incomingByte);
  }

  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //eset the RFID reader
}

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  int currentTag = 0;

  if(strlen(tag) == 0 || strlen(tag) < 12)
     return; //empty, no need to continue

  if(compareTag(tag, tag1) && lastTag != 1){ // if matched tag1, do   this
    Serial.println("Scanned tag 1");
    lastTag = 1;
    currentTag = 1;
    printDate();
    bleepSucces(11);
  }
  else
  if(compareTag(tag, tag2) && lastTag != 2){ //if matched tag2, do this
    Serial.println("Scanned tag 2");
    lastTag = 2;
    currentTag = 2;
    printDate();
    bleepSucces(11);
  }
  else
  if(compareTag(tag, tag3) && lastTag != 3){
    Serial.println("Scanned tag 3");
    lastTag = 3;
    currentTag = 3;
    printDate();
    bleepSucces(11);
  }
  else
  if(compareTag(tag, tag4) && lastTag != 4){
    Serial.println("Scanned tag 4");
    lastTag = 4;
    currentTag = 4;
    printDate();
    bleepSucces(11);
  }
  else
  if(compareTag(tag, tag5) && lastTag != 5){
    Serial.println("Scanned tag 5");
    lastTag = 5;
    currentTag = 5;
    printDate();
    bleepSucces(11);
  }
  else
  {
    if(currentTag == 0 && lastTag == 0){
      Serial.println("Unknown Tag, see below:");
      Serial.println(tag); //read out any unknown tag
      //resetReader();
      lastTag = 0;
      bleepFail(11);
    }
    return;
  }
}

void bleepSucces(int pin)
{
  digitalWrite(pin, HIGH);
  delay(300);
  digitalWrite(pin, LOW);
  delay(20);
  digitalWrite(pin, HIGH);
  delay(150);
  digitalWrite(pin, LOW);
}

void bleepFail(int pin)
{
  digitalWrite(pin, HIGH);
  delay(1200);
  digitalWrite(pin, LOW);
}

void resetReader()
{    //Reset the RFID reader to read again.
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[])
{
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
  for(int i = 0; i < strlen(one); i++)
    one[i] = 0;
}

boolean compareTag(char one[], char two[])
{ //compare two tags to see if same,
  //strcmp not working 100% so we do this

  if (strlen(one) == 0)
     return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i])
      return false;
  }
  return true; //no mismatches
}

void setDateTime()
{
  byte second =      20; //0-59
  byte minute =      37; //0-59
  byte hour =        20; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    3; //1-31
  byte month =       4; //1-12
  byte year  =       12; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();
}

byte decToBcd(byte val)
{  // Convert normal decimal numbers to binary coded decimal
  return val / 10 * 16  +  val % 10;
}

byte bcdToDec(byte val)
{  // Convert binary coded decimal to normal decimal numbers
  return val / 16 * 10  +  val % 16;
}

void printDate()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //Set variables in our struct
  timestamp.hours = hour;
  timestamp.minutes = minute;
  timestamp.seconds = second;

  //Save the timestamp to EEPROM
  bytesize = EEPROM_writeAnything(byteaddress, timestamp);
  byteaddress = byteaddress + bytesize;

  //if (Serial.available() > 0) {
    writeSerial();
  //}
}

void writeSerial()
{  
  Serial.print("{");
  // read a byte from the current address of the EEPROM
  for (int i=0; i <= byteaddress + 4; i = i + 2){
      value = EEPROM.read(i);
      Serial.print(value, DEC);

      if (i < byteaddress+4)
         Serial.print(((i-1)%3 == 0) ? ", " : " ");
  }
  Serial.println("}");     
}

person Boyd    schedule 05.04.2012    source แหล่งที่มา


คำตอบ (3)


Arduino UNO มีพอร์ตอนุกรมเพียง 1 พอร์ตเท่านั้น พิน 0 และ 1 เชื่อมต่อกับพอร์ต usb จริง ๆ

คุณจะต้องใช้ softwareserial เพื่อสร้างพอร์ตอนุกรมในซอฟต์แวร์เพื่อพูดคุยกับเครื่องอ่าน RFID ของคุณและ เชื่อมต่อเครื่องอ่าน RFID เข้ากับพินอื่นจากนั้น 0 และ 1

person Sibster    schedule 05.04.2012

Sibster เป็นพินที่ถูกต้อง 0 และ 1 ใช้สำหรับการเชื่อมต่อฮาร์ดแวร์แบบอนุกรมและ USB กับพีซี ใช้ SPI, I2c หรือซอฟต์แวร์อนุกรมสำหรับ RFID (ขึ้นอยู่กับสิ่งที่คุณมี) นอกจากนี้คุณควรใช้การ์ด SD จะดีกว่า

person Stkabi    schedule 06.04.2012

เมื่อคุณอ่านจาก arduino ide
หากคุณป้อน '2' ซึ่งจะเท่ากับ 50 เป็น ('0'==48)

ลองเปลี่ยนคำสั่ง if เป็น readByte != '2'

เว้นแต่ว่าคุณกำลังป้อนอักขระควบคุมบางประเภทซึ่งจริงๆ แล้วเท่ากับ 2

person Paul    schedule 28.06.2012