我最近组建了一个程序,通过USB端口连续获取数据并将其传输到COSM。通过serialevent
方法连续获得的数据未传递给update
方法,该方法用于在COSM上传上述数据。但是,从synchronized
方法的签名中删除serialevent
说明符后,此问题已得到解决。
public synchronized void serialEvent(SerialPortEvent oEvent)//Event handler for Serial communication
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try {
val=input.read();//Variable where data is received.Initialized to 0.
this.update(78164);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
上传方法:
private void update(int feedid) throws PachubeException, IOException ,Exception
{
Feed f = this.pachube.getFeed(feedid);
//System.out.println(f==null);
System.out.println("updating ...");
f.updateDatastream(8345, (double) val);//Expected value=77,Obtained value=0
f.updateDatastream(6274, (double) val);//Expected value=77,Obtained value=0
f.updateDatastream(1044, (double) val);//Expected value=77,Obtained value=0
System.out.println("updated");
}
我的问题是synchronized
说明符如何拒绝变量中存在的数据从serialevent
方法传递到upload
,即使我没有使用多个线程?