猿问

使用 Javascript 或 PHP 从数字秤的串行端口获取数据?

我不知道这是否真的可行,但我需要使用 JavaScript 或 PHP 从卡车的数字秤中获取重量。

目前,该秤连接到一台运行速度极慢的 PC,并运行 Windows XP 以及该公司开发的定制软件,该软件使该秤能够记录每天称重的不同车辆。

不幸的是,PC 无法升级,因为我会丢失所有驱动程序,并且该软件无法在较新版本的 Windows 上运行。

我在想,如果我可以通过 Javascript 或 PHP 与串行设备通信并获取数据,并获得与自定义软件相同的权重,那么我可以使用 PHP 和 MYSQL 后端制作一个小网页与自定义软件相同,但具有我实际上缺少的所有功能并升级 PC。

我买了一个串口转 USB 适配器并将设备连接到我的 Windows 10 笔记本电脑,它似乎正在正确传输数据,如您在这张图片中看到的那样

我已经搜索了使用 Javascript 与串行设备通信的方法,我认为使用 NodeJS 你可以做到这一点,但我不知道我是否可以在网页上实现它。此外,我还没有用 C 或 C++ 或 C# 编写过任何东西,所以我不知道如何使用这些语言中的任何一种来实现(我看到的很多答案都是使用其中一种来实现的)。

我还看到有一个名为 chrome.serial 的 chrome 应用程序,但我没有找到任何可以指示我如何继续的工作示例。

任何帮助将不胜感激!!!:)


POPMUISE
浏览 313回答 3
微课
3回答

Qyouu

一种方法是设置 Node.js 环境,然后试用 serial.iohttps://serialport.io/我找到了这个,也许这就是要走的路。对于实时应用程序,Node.js 是与 socket.io 一起使用的方式,用于在您的网站上进行实时更新

牛魔王的故事

我刚刚使用 PHP 和 Powershell 完成了这项工作。让 powershell 读取串行端口并将数据转储到 xampp 中的文件中。然后 PHP 可用于读取文件并用作 REST 调用处理程序。您可以通过 Ajax 调用 localhost 来获取数据。在您的机器上设置 xampp 在管理员模式下打开 Poweshell 并执行以下命令。这将释放 powershell 限制。否则 ps1 脚本无法在运行时运行。&nbsp; &nbsp; Set-ExecutionPolicy Unrestricted在 xampp/htdocs/ weight中创建一个目录。我称它为重量,你可以调用任何你想要的东西。使用以下代码在文件夹中创建 .ps1 脚本Start-Process PowerShell -Verb RunAs$COM = [System.IO.Ports.SerialPort]::getportnames()function read-com {&nbsp; &nbsp; $port= new-Object System.IO.Ports.SerialPort $COM,9600,None,8,one&nbsp; &nbsp; $port.Open()&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; $line = $port.ReadLine() | Out-File -FilePath C:\xampp\htdocs\weight\weight.txt&nbsp; &nbsp; &nbsp; &nbsp; Write-Host $line # Do stuff here&nbsp; &nbsp; }&nbsp; &nbsp; while ($port.IsOpen)}read-com添加一个 PHP 文件。我称它为 weightUtil.php。将以下代码添加到其中。<?phpheader("Access-Control-Allow-Origin: *");&nbsp; &nbsp; $data = '';&nbsp; &nbsp; $myFileName = "weight.txt";&nbsp; &nbsp; $myfile = fopen($myFileName, "r") or die("Unable to open file!");&nbsp; &nbsp; if(filesize($myFileName) > 0){&nbsp; &nbsp; &nbsp; &nbsp; $data = fread($myfile,filesize($myFileName));&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; echo $data;&nbsp; &nbsp; fclose($myfile);?>你现在已经准备好了。从 Xampp 控制面板运行 tomcat 服务器,您就可以通过 POST 请求接收权重了。您可以拨打电话至http://localhost/weight/weightUtil.php如果您发现访问 C 盘中文件的权限有任何问题,只需添加everyone对 xampp 的完全访问权限即可。事情就会顺利进行。

当年话下

嗯......在尝试了一个脚本并试图让它在 Linux 中工作之后,我无法用 PHP 完成它,因为在 Windows 中,脚本只能写入而不能从设备读取数据。我按照 Ifaruki 的建议使用 Node 和 serial.io(谢谢!!),并且使用以下脚本,我能够正确读取秤中的数据var SerialPort = require('serialport');var io = require('socket.io').listen(3000);var serialPort = new SerialPort("COM4", {&nbsp; &nbsp; baudRate: 9600,&nbsp; &nbsp; parser: new SerialPort.parsers.Readline("\n"),&nbsp; &nbsp; dataBits: 7,&nbsp; &nbsp; parity: 'none',&nbsp; &nbsp; stopBits: 1,&nbsp; &nbsp; flowControl: false});io.sockets.on('connection', function(socket){&nbsp; &nbsp; socket.on('message', function(msg){&nbsp; &nbsp; &nbsp; &nbsp; console.log(msg);&nbsp; &nbsp; });&nbsp; &nbsp; socket.on('disconnected', function(){&nbsp; &nbsp; &nbsp; &nbsp; console.log('disconnected');&nbsp; &nbsp; });});var clearData = "";var readData = "";serialPort.on('open',function(){&nbsp; &nbsp; console.log('open');&nbsp; &nbsp; serialPort.on('data', function(data){&nbsp; &nbsp; &nbsp; &nbsp; const buf2 = Buffer.from(data)&nbsp; &nbsp; &nbsp; &nbsp; let wArray = buf2.toString('utf8');&nbsp; &nbsp; &nbsp; &nbsp; //this part just removes characters I don't need from the data&nbsp; &nbsp; &nbsp; &nbsp; let wSlice = wArray.slice(3, wArray.length);&nbsp; &nbsp; &nbsp; &nbsp; let rawWeight = wSlice.slice(0, -3);&nbsp; &nbsp; &nbsp; &nbsp; let fWeight = rawWeight.trim();&nbsp; &nbsp; &nbsp; &nbsp; let weight = parseInt(fWeight);&nbsp; &nbsp; &nbsp; &nbsp; console.log(weight);&nbsp; &nbsp; });});setTimeout(function(){&nbsp; &nbsp; serialPort.close(function(){&nbsp; &nbsp; &nbsp; &nbsp; console.log("Port Closed!");&nbsp; &nbsp; });}, 3000);我对 Node.js 不是很熟悉。我现在需要做的是在浏览器中运行这个功能,这样我就可以在网页中从设备中吐出数据......如果有人能指出我正确的方向......
随时随地看视频慕课网APP
我要回答