JavaLikeCalc.JavaScript
//Same request to the device
function req(fnc, data) {
	if(data == EVAL)	data = "";

	req = SYS.strFromCharCode(0xCC, addr, fnc) + data;

	//Calc and append the CRC
	for(CRC = 0, iR = 0; iR < req.length; iR++)	CRC += req.charCodeAt(iR);
	req += SYS.strFromCharCode(CRC&0xFF, (CRC>>8)&0xFF, 0xEE);

	SYS.messDebug("/TDS/"+this.cfg("SHIFR"), tr("Request")+": "+SYS.strDecode(req,"Bin"," "));

	resp = tr.messIO(req);
	//Wait for acceptance
	while(resp.length && (resp.length < 8 || resp.length != (resp.charCodeAt(3,"UTF-16LE")+8)) && (tresp=tr.messIO("")).length) resp += tresp;

	if(resp.length < 8 || resp.charCodeAt(0) != 0xCC || resp.charCodeAt(resp.length-1) != 0xEE || resp.length != (resp.charCodeAt(3,"UTF-16LE")+8))
		return tr("3:No response or the response is incompleted. ");
	if(resp.charCodeAt(2) != fnc)
		return tr("4:The response function is improper to the request one.");

	//Calc and check the CRC
	for(CRC = 0, iR = 0; iR < resp.length-3; iR++)	CRC += resp.charCodeAt(iR);
	if((CRC&0xFFFF) != resp.charCodeAt(resp.length-3,"UTF-16LE"))	return tr("3:Error the response CRC. ");

	SYS.messDebug("/TDS/"+this.cfg("SHIFR"), tr("Response")+": "+SYS.strDecode(resp,"Bin"," "));

	data = resp.slice(5,-3);

	return "0";
}

//Parse float
function floatGet(str) {
	return ((str.charCodeAt(1)&0x80)?-1:1) * ((str.charCodeAt(0,"UTF-32BE")&0x7FFFFF)/pow(2,23)) * pow(2,str.charCodeAt(0));
}
//Produce float
/*function floatSet(vl) {
	exp = 0;
	while(vl/pow(2,exp) > 1 && exp < 128)		exp++;
	while(vl/pow(2,exp) < 0.5 && exp > -127)exp--;
	mts = (vl/pow(2,exp))*pow(2,24) + 0.5;
	if(vl >= 0) mts = mts&0x7FFFFF;
	return ((mts>>16)&0xFF).toString(16,2)+(exp+127).toString(16,2)+(mts&0xFFFF).toString(16,4);
}*/

//Seting value to the archive
function archSet(attr, vTm, arh, vl) {
	SYS.messDebug("/TDS/archSet","attr="+attr+"; vTm="+SYS.strftime(vTm)+"; arh="+arh+"; vl="+vl);
	atrO = this[attr];
	if(!atrO || atrO.isEVal()) return;
	//arguments[attr] = vl;	//Set the current value
	//if(vTm > atrO.time())	atrO.set(vl,vTm,0,true);
	if(atrO.arch())	atrO.arch().setVal(vTm*1e6, vl, arh);
}

//Main process
// Set transport and init it
if(f_start) {
	f_err = "0";
	transport_ = transport;
	tr = SYS.Transport.nodeAt(transport, ".");
	cTm = 0;
}
if(f_start || f_stop) return;

t_err = "0";
tmCur = SYS.time();

//Check for the transport change and connect
if(!tr || transport != transport_)	{
	tr = SYS.Transport.nodeAt(transport, ".");
	transport_ = transport;
}
if(!tr)	t_err = "1:"+tr("Output transport '%1' error.").replace("%1",transport);
else if(addr < 0 || addr > 255)
	t_err = "2:"+tr("Address '%1' out of range [0...255].").replace("%1",addr.toString());
else if((tmCur-cTm) > tryPer) {
	cTm = tmCur;
	// Currents reading
	data = SYS.strFromCharCode(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	if(!t_err.toInt() && !(t_err=req(0x30,data)).toInt()) {
		dtTm = data.charCodeAt(0,"UTF-16BE").toString(16)+"-"+data.charCodeAt(2).toString(16,2)+"-"+data.charCodeAt(3).toString(16,2)+
			"T"+data.charCodeAt(4).toString(16,2)+":"+data.charCodeAt(5).toString(16,2)+":"+data.charCodeAt(6).toString(16,2);
		fWork = floatGet(data.slice(7,11));
		fResult = floatGet(data.slice(13,17));
		t = floatGet(data.slice(17,21));
		p = floatGet(data.slice(21,25));
		//SYS.messInfo("/TDS/"+this.cfg("SHIFR"), tr("Response")+": "+SYS.strDecode(data,"Bin"," "));
	}

	// Archive reading
	data = SYS.strFromCharCode(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	if(arhH.length && !t_err.toInt() && !(t_err=req(0x01,data)).toInt())
		for(off = 0; off < data.length; off += 28) {
			tmStmp = SYS.mktime(data.charCodeAt(off+6).toString(16).toInt(),
				data.charCodeAt(off+5).toString(16).toInt(),
				data.charCodeAt(off+4).toString(16).toInt(),
				data.charCodeAt(off+3).toString(16).toInt(),
				data.charCodeAt(off+2).toString(16).toInt()-1,
				data.charCodeAt(off,"UTF-16BE").toString(16).toInt());
			archSet("fWork", tmStmp, arhH, floatGet(data.slice(off+7,off+11)));
			archSet("fResult", tmStmp, arhH, floatGet(data.slice(off+13,off+17)));
			archSet("t", tmStmp, arhH, floatGet(data.slice(off+17,off+21)));
			archSet("p", tmStmp, arhH, floatGet(data.slice(off+21,off+25)));
		}
}

if(!t_err.toInt())	t_err = "0";
f_err = t_err;