int maxLoops = 511;
int count = 0;
while(count < maxLoops) {
count += 1;
if(this.port == null){
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailedPortNotSetup"));
return;
}
//We recieved some data, now parse it.
try{
InputStream in = this.port.getInputStream();
SerialDataSourceVO vo = ((SerialDataSourceVO)this.getVo());
int data;
while (( data = in.read()) > -1 ){
index += 1;
buffer.push(data);
if (vo.getUseTerminator()) {
if(isTerminatorFound())
break;
}
}
if(!vo.getUseTerminator()) {
String msg = new String(buffer.peekAll());
searchRegex(msg, 0);
}
if(!isTerminatorFound())
return;
String msg = buffer.popString(index, Charset.forName("ASCII"));
index = 0;
if(!this.dataPoints.isEmpty()){
//DS Information
String messageRegex = vo.getMessageRegex(); //"!([A-Z0-9]{3,3})([a-zA-Z])(.*);";
int pointIdentifierIndex = vo.getPointIdentifierIndex();
Pattern messagePattern = Pattern.compile(messageRegex);
Matcher messageMatcher = messagePattern.matcher(msg);
if(messageMatcher.matches()){
//Parse out the Identifier
String pointIdentifier = messageMatcher.group(pointIdentifierIndex);
//Update all points that have this Identifier
for(DataPointRT dp: this.dataPoints){
SerialPointLocatorRT pl = dp.getPointLocator();
SerialPointLocatorVO plVo = pl.getVo();
if(plVo.getPointIdentifier().equals(pointIdentifier)){
Pattern pointValuePattern = Pattern.compile(plVo.getValueRegex());
Matcher pointValueMatcher = pointValuePattern.matcher(msg); //Use the index from the above message
if(pointValueMatcher.matches()){
String value = pointValueMatcher.group(plVo.getValueIndex());
PointValueTime newValue = new PointValueTime(DataValue.stringToValue(value, plVo.getDataTypeId()),
Common.timer.currentTimeMillis());
dp.updatePointValue(newValue);
}//end if value matches
}//end for this point id
}
}else{
raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT,System.currentTimeMillis(),true,new TranslatableMessage("event.serial.patternMismatch",vo.getMessageRegex(),msg));
}
}
returnToNormal(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis());
}catch ( IOException e ){
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed",e.getMessage()));
}
}
}