0

I'm using serial event to pass rfid tags read from arduino to processing. In the serial event I am parsing and converting the variable to an integer. This working for the most part, only one rfid card keeps throwing an error.

void serialEvent(Serial thisPort)
{
  String inString = thisPort.readString();
  if(inString != null)
  {
    Serial connect1 = (Serial) connections.get(0);
      if(thisPort == connect1 )
      {
        Chair chair = (Chair) chairs.get(0);
        if(inString.contains("UID Value:"))
        {
          int p2 = inString.indexOf(":");
          String pString = inString.substring(p2+1); 
          String pString2 = pString.substring (0,10);
          //println(pString2);
          pString2.trim();

          println("String length: " + pString2.length());

          chair.setRFID(pString2);
          println(pString2);
        }

      }
  }
}

void setRFID(String r)
  {  
     try{
       this.rfid = Integer.parseInt(r);
     }
     catch (Exception e) {
      e.printStackTrace();
     }

     //set position of person to chair
     for (Person person : people)  
     { 

        //println(this.rfid != chair.rfid);
        //println(this.rfid + "," +  "person: " + person.ID + "," + person.RFID);
        if(this.rfid == person.RFID)
        {
          person.setPos(this.pos);
          this.personID = person.ID;
        }
     }
  }

The try-catch is not working, and this line is causing the problem this.rfid = Integer.parseInt(r);. I thought it might be a malformed string but the strings seems ok. Here the results of checking string consistency: String length: 10 1811524219 String length: 10 1942302231 String length: 10 1010368230 String length: 10 9813023219

4

4 回答 4

6

9813023219 is an invalid Integer as it is greater than Integer.MAX_VALUE, which is 2147483647. Use Long instead, who's MAX_VALUE is 9223372036854775807.

于 2013-04-18T07:09:01.927 回答
2

You have exceeded the maximum value for an integer. I suggest using a long instead.

Check for this by displaying Integer.MAX_VALUE - no int can exceed this value.

java.long.NumberFormatException is thrown when a given string does not match the expected layout.

于 2013-04-18T07:09:13.343 回答
1

The number 9813023219 is out of range for int data type, try changing your data type to long and it should work.

于 2013-04-18T07:08:31.937 回答
1

9813023219 is Invalid Integer, You can use Long for your requirement. If RFID is not exceeding Long.MAX_VALUE.

于 2013-04-18T07:11:27.633 回答