Jeszcze jedno podejście do skryptów admina i SSI z odczytem przez php działa. W skrypcie html jest błąd. Poniżej poprawna wersja:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#Button1').click(function() { //action on the button click
var address = $('#address').text().replace(/ /g,''); //take the address from the filed and trim all spaces
$.ajax({
type: 'POST',
url: 'getdata.php',
data: {ADR: address}, //send to the PHP file the address
success: function(data){ //what to do if data received
if (data.charAt(0) == '<') { //if reply starts with "<" it means that the address was wrong, and WAGO returns structured html
$('#Result').html(data.substring(4)); //show the WAGO message but without the initial "<br>"
}
else { //otherwise the data received is what we wanted
$('#Result').text('Reply: ' + address + ': ' + data); //show the result
}
},
error: function(jqXHR, textStatus, errorThrown) { //in case of connection error, print whatever you can get from the server
$('#Result').text(jqXHR.statusText+', '+textStatus+', '+errorThrown);
}
}); //End of AJAX
}); //End of Button.click()
}); //End of document.ready()
</script>
<style type="text/css">
.Button {height: 30px; width: 300px; border: gray dotted 2px; padding: 2px; text-align: center; line-height: 30px;}
</style>
</head>
<body>
<div id="addressField" style="width: 300px;"> Address to check: <div id="address" contenteditable="true" style="float: right; border: 1px solid black">QX0.0</div></div>
<br>
<div id ="Button1" class="Button">Click to read!</div>
<br>
<div id ="Result" class="Button"></div>
</body>
</html>
Skrypt php jest poprawny:
<?php
if(isset($_POST['ADR'])) {
$url = "http://192.168.0.61/READPI"; //change the IP to your WAGO's IP
$data = array('ADR' => $_POST['ADR'], 'FORMAT' => '%d');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
}
?>
Teraz będę się biedził nad przerobieniem skryptu php z ssi na wersję modbus. Może ktoś miałby ochotę pomóc? Zastanawiam się jak rozwiązać translację wejść/wyjść/zmiennych na adresy pamięci, tak, aby w skryptach posługiwać się nazwami zmiennych a nie numerami komórek.