# Conversion of PHP function to Javascript

**URL:** <https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052>\
**Category:** General\
**Created:** [26 August 2020 16:45 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052 "2020-08-26T16:45:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [26 August 2020 16:45 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/1 "2020-08-26T16:45:22Z")

</div>

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'

```auto
<?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

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [26 August 2020 17:03 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/2 "2020-08-26T17:03:28Z")

</div>

sockets in nodejs can be made with the net module [https://nodejs.org/docs/latest/api/net.html#net\_net\_connect](https://nodejs.org/docs/latest/api/net.html#net_net_connect)

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

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [26 August 2020 18:11 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/3 "2020-08-26T18:11:34Z")

</div>

Thank you, I will investigate!

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

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [26 August 2020 19:59 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/4 "2020-08-26T19:59:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [27 August 2020 07:05 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/5 "2020-08-27T07:05:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [28 August 2020 08:44 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/6 "2020-08-28T08:44:14Z")

</div>

@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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 September 2020 08:44 UTC](https://discourse.nodered.org/t/conversion-of-php-function-to-javascript/32052/7 "2020-09-11T08:44:21Z")

</div>

Closed on 11 September 2020 08:44 UTC

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