Conversion of PHP function to Javascript

Hi,

Hoping for some help with a script conversion. I have searched and, as usual, Search Engines are giving me what they think I want rather than what I actually need!! Most answers direct me to PHP pages, and those I have found relating to Javascript, I find very confusing!

I have an old PHP script that simply resets a controller by writing direct to a memory location within the PAC Controller. I am trying to develop a Javascript equivalent which would be triggered if a watchdog fails to get a response. It is the conversion of the PHP Script that is the problem for me. I can muddle through the rest!

Here is the PHP script - basically writing <5> to Memory Location <F038 0000> using PHP function 'fsockopen'

<?php

#Write Quadlet routine for Opto 22 PAC R1
function writeQuad ($loc, $val) {
$comm = "0000 00 00 0000 FFFF ".$loc." ".$val;
$ipsock = pfsockopen ("172.27.123.50",2001,$errno,$errstr,3);	
	if (!$ipsock)
	echo "$errstr ($errno)<br />\n";

	{	stream_set_timeout($ipsock,3);
		echo $comm;
		$comm1 = str_replace(" ","",$comm);
		$comm3 = pack("H*", $comm1 ) ;	
	
		fwrite($ipsock,$comm3);
		$reply = fread($ipsock,20);
		$info = stream_get_meta_data($ipsock);
		fclose($ipsock);
		if ($info['timed_out'])
		echo 'connection timed out!';	
		else {
			$tcode = substr(bin2hex($reply),6,1);
			$rcode = substr(bin2hex($reply),12,1);
			$Int = substr(bin2hex($reply),24,8);
		}
	}
}
			writeQuad("F038 0000", "00000005");  //Reset Command
?>

Any help/pointers gratefully received

TIA,
Colin

sockets in nodejs can be made with the net module https://nodejs.org/docs/latest/api/net.html#net_net_connect

Or do you want to do that from within Node-RED? (TCP node...)

Thank you, I will investigate!

Yup, need to do it within Node Red, my knowledge does not extend too much further. :grin: I did look at 'sockets', but just wasn't sure. I will have a play.

you probably can just use the tcp request and send the string the php script is producing as the payload to 172.27.123.50 with port 2001.

If you only ever want to execute your writeQuad("F038 0000", "00000005"); function, then the easiest might be to add a echo $comm3; before the fwrite(...) line and then copy that payload to Node-RED.

EDIT: I just saw that there is the pack function in the php script, so you probably need to send a buffer as the payload and not a string.

1 Like

Thank you again. I have some stuff to do, but you have given me a direction to look. I will report back when I have had some proper time to play.

It is very annoying when the heating doesn't come on when commanded after you have been away for a few days! Seems something hung in the comms between Node-RED and the PAC controller (PAC read/write nodes). Hence the Watchdog and reset approach until I can find out exactly what happens to cause failure, but this is the first time in several months.

@cinhcet, many thanks for your assistance.

Took a while to get the data in the correct format - I forgot to KISS and over complicated things. Now running as needed.

[{"id":"8d420a3e.8c74a","type":"tcp request","z":"1fe2e6f5.206b79","server":"172.27.123.55","port":"2001","out":"time","splitc":"50","name":"PAC","x":450,"y":80,"wires":[["7fc2f5ab.d8776c"]]},{"id":"be487e00.c8978","type":"inject","z":"1fe2e6f5.206b79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":80,"wires":[["a3103cfa.e47258"]]},{"id":"c5e84d00.43806","type":"debug","z":"1fe2e6f5.206b79","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":80,"wires":[]},{"id":"7fc2f5ab.d8776c","type":"function","z":"1fe2e6f5.206b79","name":"PAC Reply","func":"msg.payload = msg.payload.toString(\"hex\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":80,"wires":[["c5e84d00.43806"]]},{"id":"a3103cfa.e47258","type":"function","z":"1fe2e6f5.206b79","name":"Reset PAC","func":"var str = \"000000000000FFFFF038000000000005\"\nvar buffer = Buffer.from(str, \"hex\")\nmsg.payload = buffer;\nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":80,"wires":[["8d420a3e.8c74a"]]},{"id":"d241c6e3.36e178","type":"comment","z":"1fe2e6f5.206b79","name":"Power Cycle/Hard reset PAC with OptoMMP","info":"","x":210,"y":40,"wires":[]}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.