Author Topic: Read values in Nano-10 from another web page  (Read 13071 times)

wittmane

  • Newbie
  • Posts: 5
  • I'm a llama!
    • View Profile
Read values in Nano-10 from another web page
« on: August 11, 2014, 02:06:27 PM »
I have some JavaScript code to read the value of DM[1] in my Nano-10, but it uses
Quote
HOSTLINK
which I'm not completely sure what it is. It appears to be some relative path dealing with the device, but I would like to make absolute, so that I could call the command from a separate web page.

Here is my JavaScript Code:
Code: [Select]
function h2d(h) {
  var val16 = parseInt(h,16)
  if (val16 > 32767) val16 += -65536
  return val16
}

function timestamp(){ return "?%0C"+new Date().getTime()}

function createXMLhttp() {var xmlhttp;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
     try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")}
     catch (e) { try {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
          catch (E) {xmlhttp=false}
     }
    @else
     xmlhttp=false
    @end @*/

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
     try { xmlhttp = new XMLHttpRequest()}
     catch (e) { xmlhttp=false}
  }

  if (!xmlhttp && window.createRequest) {
     try {xmlhttp = window.createRequest()}
     catch (e) {xmlhttp=false}
  }
  return xmlhttp
}

function errormsg(txt){alert("Connection Failed:"+txt)}
var errCount=0

function checkSvrResp(xmlobject, from){
   if (xmlobject.status!=200 || xmlobject.responseText==""){
     errCount++
    if (errCount > 3) {alert("Server Error!");return 0;}
    else return 1
   }
   errCount=0
   return 1
}

function getTemperature(){
  var cmd = "HOSTLINK/RD00010014"+timestamp() // read first 20 DMs (max 50 in one read)
  var xmlhttpObj=createXMLhttp()
  xmlhttpObj.open("GET",cmd, true)
  var DM = "Nothing..."
  try {
    xmlhttpObj.onreadystatechange=function() {
      if (xmlhttpObj.readyState==4) {
        if (!checkSvrResp(xmlhttpObj,"getTemperature")) return
      dataStr = xmlhttpObj.responseText.substring(4)
      DM = h2d(dataStr.substring(0,4))
      // display the temperature
      document.getElementById("Temperature").innerHTML = ""+DM
      setTimeout(getTemperature, 10000);
      }
    }
    xmlhttpObj.send(null)
  } catch (e){errormsg("Failed at getDM"+e)}
}

The code is essentially just a stripped down version of the pre-built webpage. It does exactly what I want when run through the Nano-10, but I want it to be run through some other device. I feel that the
Quote
HOSTLINK
(in the getTemperature function) should just change to an IP address, or some slight modification of that. Any help would be appreciated.

davidg

  • Newbie
  • Posts: 11
    • View Profile
    • My Mobile HMI | Connect a mobile App to your PLC
Re:Read values in Nano-10 from another web page
« Reply #1 on: September 17, 2014, 09:09:18 AM »
When you say "I want to run the webpage through another device"  I assume you are hosting the webpage in a different place than the NANO-10 built in web server.  The problem with this is you run into something called cross domain policy that won't allow to get data in the same way from a different domain - i.e. the NANO-10.

On my site I talk about a workaround to test if this is the issue you are having.

http://www.mymobilehmi.com/chrome-cross-domain-extension#!chrome-cross-domain-extension/crfl