首页 > 解决方案 > 如何在 php 页面中运行 nodejs 脚本

问题描述

我正在尝试使用 nodejs、php、html 和 css 构建一个 html scada 系统。

我的程序:我在 nodejs 中有这个 9 行的小代码;我使用此代码在 modbus rtu 协议中与 PLC 通信。如果我从终端运行代码,代码运行良好;

我的问题:我试图在 html 页面中使用 php提交按钮运行此代码,但我认为我错过了一些东西,因为它不起作用。

谢谢你 。 节点代码

// 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代码: :

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

标签: phphtmlnode.jsscada

解决方案


您想要执行一个没有解释器就无法执行的 js 文件。您的exec代码应如下所示:

exec('node write.js')

node是您的节点实例的可执行文件。


推荐阅读