Search code examples
phphtmlnode.jsscada

how to run a nodejs Script in php page


im trying to build a html scada system using nodejs , php , html and css.

my program : i have this little 9 lines code in nodejs ; i use this code to communicat with a PLC in modbus rtu protocol. the code is working perfectly if i run it from terminal ;

my problem : im trying to run this code usinga php submit button inside html page, but i think that i missing something cuz its not working.

thank you . nodejs code :

// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a serial port
   client.connectRTUBuffered("COM2", { baudRate: 9600 ,parity: "even", dataBits: 8, stopBits: 1} , write );
     function write() {
    client.writeCoil(22, 1);
      
 }

html code : :

 <html>
<head>
<title>PHP isset() example</title>
</head>
<body>
<form method="post">
<input type="submit" value="Sum" name="Submit1"><br/><br/>
<?php
if(isset($_POST["Submit1"]))
{
  exec('write.js  >/dev/null/ 2>&1 &');  
}
?>
</form>
<script src="write.js"  type="text/javascript" ></script>
</body>
</html>

Solution

  • You want to execute a js file which is not possible without an interpreter. Your exec code should look like this:

    exec('node write.js')
    

    Here node is the executable of you node instance.