我使用串行事件将从arduino读取的rfid标签传递给processing。在串行事件中,我解析变量并将其转换为整数。这在很大程度上是有效的,只有一张RFID卡不断抛出错误。
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;
}
}
}try-catch不起作用,这一行导致了问题this.rfid = Integer.parseInt(r);。我认为它可能是一个格式错误的字符串,但字符串看起来没有问题。检查字符串一致性的结果如下:字符串长度: 10 1811524219字符串长度: 10 1942302231字符串长度: 10 1010368230字符串长度:109813023219
发布于 2013-04-18 15:08:32
数字9813023219超出了int数据类型的范围,请尝试将您的数据类型更改为long,它应该可以工作。
https://stackoverflow.com/questions/16076560
复制相似问题