问题描述
我一直在使用HTML和JavaScript来创建图形化网页,以显示来自我的西门子S7 1500 PLC的数据。当请求信息的网页由PLC Web服务器提供并与具有JSON结构的文件位于同一目录中时,我一直使用$ .getJSON命令成功读取PLC中的值,并且所有需要的值。
我有一台PC通过以太网连接到我的PLC,并希望在PC上本地运行网页,并读取PLC中页面提供的值Web服务器。
当读取的数据位于Web服务器上的相同目录中时,我当前正在读取值的代码如下所示:
< script type =text / javascript>
$(document).ready(function(){
$ .ajaxSetup({cache:false});
$ b $ setInterval(function(){
$ .getJSON(inputs.htm,function(data){
//变量声明
engineSpeed = data.engineSpeed;
engineFuelLevelScaled = data.engineFuelLevelScaled;
powerEndDischargePressurePSI = data.powerEndDischargePressurePSI;
powerEndDischargeFlowRateBBLM = data.powerEndDischargeFlowRateBBLM;
powerEndSuctionPressurePSI = data.powerEndSuctionPressurePSI;
});
},1000);
});
< / script>
inputs.htm文件很简单:
{
engineSpeed::=WebData.engineSpeed:,
engineFuelLevelScaled::=WebData.engineFuelLevelScaled: ,
powerEndDischargePressurePSI::=WebData.powerEndDischargePressurePSI:,
powerEndDischargeFlowRateBBLM::=WebData.powerEndDischargeFlowRateBBLM:,
powerEndSuctionPressurePSI: =WebData.powerEndSuctionPressurePSI:
其中WebData是一个数据块正在更新我的PLC上的值。
我很满意这是如何工作的,但是当我尝试在本地运行一个页面来查看输入时。
我的PLC的IP地址为172.17.2.11,我已将$ .getJSON更改为:
$。getJSON(http://172.17.2.11/awp/GeminiOnline/inputs.htm,函数(数据){
和
$ .getJSON(172.17。 2.11 / awp / GeminiOnline / inputs.htm,函数(data){
虽然都没有工作。我知道这些是正确的网址,因为我可以去其中任何一个网站,并阅读我尝试访问的值。
我在网上设置了权限我的PLC的服务器允许所有用户完全访问,因此不再需要登录。我想知道是否有一个步骤我失踪或$ .getJSON结构的一些限制阻止我阅读这样的。
任何输入将是赞赏。如果您有任何其他方法可以读取PC上本地托管的页面中的当前PLC值,这也是有帮助的。
预先致谢。我最终解决了我的问题,尽管它需要一种完全不同的方法来攻击它。它不喜欢我的跨域请求,并试图让JSONp工作,我无法完全管理它。在一位同事的帮助下,我们开发了以下内容:
< script>
函数httpGet(theUrl)
{
//调用初始请求
xmlhttp = new XMLHttpRequest();
var date = new Date();
xmlhttp.open(GET,theUrl +?+ date.getTime(),false); //注意+? + data.getTime()用于欺骗浏览器使用前一页请求的缓存结果
xmlhttp.send();
//设置时间间隔
setInterval(function(){
//如果请求成功,则解析字符串并开始一个新请求
if (xmlhttp.readyState == 4& xmlhttp.status == 200)
{
//提取JSON字符串
var jsonString = xmlhttp.responseText;
data = $ .parseJSON(jsonString);
//您的代码可以在这里使用数据,就像您经常使用
}
},1000);
}
httpGet(http://172.17.2.10/awp/GeminiOnline/inputs.htm);
无论如何,使用XMLHttpRequest()和其他函数,我能够读取其他数据页面,就像我之前使用.getJSON()一样。
I've been working with HTML and javascript to create graphical web pages to display data from my Siemens S7 1500 PLC. I've been using a $.getJSON command to successfully read values from the PLC when the web page that requests the information is served up by the PLC web server and in the same directory as the file with the JSON structure and all of the desired values.
I have a PC connected to my PLC via ethernet and wish to run a web page locally on the PC and read the values served up by a page from the PLC web server.
My current code for reading the values when the data to be read is in the same directory on the web server looks like:
<script type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({ cache: false });
setInterval(function() {
$.getJSON("inputs.htm", function(data){
// Variable Declaration
engineSpeed = data.engineSpeed;
engineFuelLevelScaled = data.engineFuelLevelScaled;
powerEndDischargePressurePSI = data.powerEndDischargePressurePSI;
powerEndDischargeFlowRateBBLM = data.powerEndDischargeFlowRateBBLM;
powerEndSuctionPressurePSI = data.powerEndSuctionPressurePSI;
});
},1000);
});
</script>
The "inputs.htm" file is simply:
{
"engineSpeed" : ":="WebData".engineSpeed:",
"engineFuelLevelScaled" : ":="WebData".engineFuelLevelScaled:",
"powerEndDischargePressurePSI" : ":="WebData".powerEndDischargePressurePSI:",
"powerEndDischargeFlowRateBBLM" : ":="WebData".powerEndDischargeFlowRateBBLM:",
"powerEndSuctionPressurePSI" : ":="WebData".powerEndSuctionPressurePSI:"
}
where "WebData" is a data block being updated with the values on my PLC.
I'm happy with how this has worked, but when I try to run a page locally to look at the "inputs.htm" page, it hasn't worked.
My PLC has the IP address 172.17.2.11 and I've changed the $.getJSON to:
$.getJSON("http://172.17.2.11/awp/GeminiOnline/inputs.htm", function(data){
and
$.getJSON("172.17.2.11/awp/GeminiOnline/inputs.htm", function(data){
though neither have worked. I know these are the correct web addresses as I can go to either of them and read the values I'm trying to access.
I have set the permissions on the web server of my PLC to allow all users full access so the login is no longer required. I'm wondering if there is a step I'm missing or some limitation of the $.getJSON structure that is preventing me from reading like this.
Any input would be appreciated. If you have any other methods by which I could read the current PLC values in a page hosted locally on the PC that would also be helpful.
Thanks in advance.
I did end up solving my problem, though it required a completely different method of attacking it. It wasn't liking my cross domain requests and try as I might to get JSONp to work, I couldn't quite manage it. With the help of a colleague we developed the following:
<script>
function httpGet(theUrl)
{
// Calls the initial request
xmlhttp = new XMLHttpRequest();
var date = new Date();
xmlhttp.open("GET", theUrl + "?" + date.getTime(), false ); // Note that the + "?" + data.getTime() is used to trick the browser from using the cached results from a previous page request
xmlhttp.send();
// Sets a timing interval
setInterval(function(){
// If the request was successful, then parse the string and start a new request
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// Extract the JSON String
var jsonString = xmlhttp.responseText;
data = $.parseJSON(jsonString);
//YOUR CODE CAN GO HERE, USING THE DATA AS YOU WOULD REGULARLY
}
}, 1000);
}
httpGet("http://172.17.2.10/awp/GeminiOnline/inputs.htm");
Anyways, using the XMLHttpRequest() and the other functions I was able to read the data from my other page, just like I had with the .getJSON() from before.
这篇关于从西门子S7 1500 PLC的Web服务器页面读取JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!