Do-More StructToJson
Given a memory block name, block index variable name, and struct dump from a data view, this tool we create an appropriate JSON string that can be pasted into a STRINGPRINT block.
by JCFiddle
HTML
<div>
<label>Memory Block:</label>
<input type="text" id="MemoryBlock" value="DUTs"/>
</div>
<div>
<label>Block Index:</label>
<input type="text" id="BlockIndex" value="V1"/>
</div>
<div>
<label>Struct Export:</label>
<input type="text" id="DataExport" />
</div>
<div>
<button id="Convert">Convert</button>
</div>
<div id="Results">
</div>
JavaScript
$(document).ready(function() {
$('#Convert').click(function(){
$('#Results').html('');
var memoryBlock = $('#MemoryBlock').val();
var blockIndex = $('#BlockIndex').val();
var dataExport = $('#DataExport').val();
var dataExport = dataExport
.replace( /\s\s+/g, ' ' ) // Remove duplicate spaces
.replace( /\(.*?\)/g, '' ) // Remove the parens and their contents
.replace('.', ''); // Remove the periods
var dataTokens = dataExport.split(" ");
var jsonStr = '"{';
var isFirst = true;
for (var i = 0, len = dataTokens.length; i < len; i++) {
if (!isFirst) {
jsonStr += ',\r\n ';
}
else {
isFirst = false;
}
var scrubbedToken = dataTokens[i].replace('.', '').trim();
jsonStr += "'" + scrubbedToken + "': \" " + memoryBlock + '[' + blockIndex + '].' + scrubbedToken + " \"";
}
jsonStr += '}"';
var jsonBlocks = jsonStr.match(/[\s\S]{1,1000}/g) || [];
for (var block = 0; block < jsonBlocks.length; block++) {
var curBlockStr = jsonBlocks[block];
if (block == 0) {
var lastChar = curBlockStr.substr(curBlockStr.length - 1);
if (lastChar != '"') {
curBlockStr += '"';
}
}
else {
var firstChar = curBlockStr.substring(0,1);
if (firstChar != '"') {
curBlockStr = '"' + curBlockStr;
}
}
var resBlock = $('<div><textarea rows=5 cols=80>' + curBlockStr + '</textarea></div>');
$('#Results').append(resBlock);
}
// Just a test to ensure we are generating valid JSON from our STRINGPRINT export on the PLC...
//var json = {'Expected': 20000, 'Simulated': 0, 'Measured': 0, 'Delta': 0, 'IsPresent': 0, 'IsRunning': 0, 'IsPassed': 0, 'IsFailed': 0, 'Started': 0, 'Ended': 0};
//console.log(json);
});
});