Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Salem, OR
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Gushing hole in memory damn;

    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); }

  • #2
    Registered User
    Join Date
    Nov 2008
    Location
    Salem, OR
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I think I got it.

    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.");
    }

    }

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Well done and thanks for posting the fix this may help other users


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •