php - 如何在 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>
解决方案
您想要执行一个没有解释器就无法执行的 js 文件。您的exec
代码应如下所示:
exec('node write.js')
这node
是您的节点实例的可执行文件。
推荐阅读
- java - ViewDragHelper.getTouchSlop() 上的 NullPointerException
- python - 在 MacOSX 上清理和管理 Python 版本
- css - Bootstrap 4 多级下拉菜单略微未对齐
- laravel - 如何显示数组中的刷新值
- php - 如何安装自定义版本的 symfony?
- reactjs - 如何将 SVG 导入 React 中基于类的子组件?
- php - 替换字符串中的多个短语
- ios - UIPickerView 将数据传递给容器视图
- php - php array_diff错误reuslt
- python-3.x - 在决策树上制作决策面图时出错