...

View Full Version : Gushing hole in memory damn;



washad
11-06-2008, 06:33 PM
Hello all;

I use a programmable logic controller (PLC) with an attached Web Server. It allows me to pull data registers directly from the processor of the PLC via an XML file. Each time that I pull in the XML file, the XML data fields are filled with real PLC data.

I modified the code below from that provided by the vendor. This is my first walk down the path of AJAX.

The code below works, but leaks memory like a sieve. I can watch my process monitor count up like the national debt while I have the page open.

Can anybody shed some light on why?

Thanks, SteveJ

==========================================================


var _DataFile = "plc?PlcReadData.xml";
var _PlcData;
var _Interval = 500;
var _XmlData;



// EVENT: ON WINDOWS LOAD
// **********************************************************
window.onload = function() {
updateData();
setInterval(updateData, _Interval);
}



// FUNCTION: UPDATE DATA
// **********************************************************
// Calls AJAX request to get the XML File of PLC Data
function updateData() {

var xhr = createXMLHttpRequest();

xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var xml = xhr.responseXML;
var root = xml.getElementsByTagName("plc")[0];
$("txt_ActualTension").value = root.getElementsByTagName("ActualTension")[0].firstChild.nodeValue;
}
else
alert("Message returned, but with error status.");
}

delete xhr;
}

// Get PLC Data
xhr.open("GET", _DataFile, true);
xhr.setRequestHeader( "If-None-Match", "*");
xhr.send(null);
}




//FUNCTION: SEND DT INTEGER
//********************************************************
//Sends a 16bit integer to the PLC
function sendDTint(address, value)
{ var add = "0" + address; //Generate PLC submit string,
var adl = add.length; // for example: DT123 <-- 456
var par = "{DT_" + add.substring(0,adl-1) + "_" + add.substring(adl-1,adl);
par = par + "_770_%i}=" + value; // result: "{DT_12_3_770_%i}=456"
var xhr = createXMLHttpRequest(); // "770_%i" stands for 16 bit integers
xhr.onreadystatechange = function() // Detais see "C.) Programming Examples..."
{ if (xhr.readyState == 4)
{ if (xhr.status != 200) //Check http answer
alert("Submit post returned, but with error status.");
}
}
xhr.open("POST","/plcpost",true); //Open a http POST connect
xhr.send(par); // and submit CGI parameter
}


//Request data from http server
function createXMLHttpRequest()
{ try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
try { return new XMLHttpRequest(); } catch(e) {}
alert("Browser does not support XMLHttpRequest.");
return null;
}


//Shortcut for DOM access
function $(id) { return document.getElementById(id); }

washad
11-06-2008, 06:55 PM
An appropriately placed xhr=null; delete xhr; seems to have fixed it.



xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var xml = xhr.responseXML;
var root = xml.getElementsByTagName("plc")[0];
$("txt_ActualTension").value = root.getElementsByTagName("ActualTension")[0].firstChild.nodeValue;

xhr = null;
delete xhr;

}
else
alert("Message returned, but with error status.");
}

}

barkermn01
11-10-2008, 03:08 PM
Well done and thanks for posting the fix this may help other users



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum