drt12345678 2019-02-27 16:58
浏览 63

使用Node.js或PHP从PHP桌面应用程序发送和读取串行数据

as written in the title i made a stand alone web app using PHP Desktop, and now i want it to send and read data from a serial port.

I tried first to use this PHP Serial class (https://github.com/rubberneck/php-serial), but i had some problems, particulary on reading data, so i moved to javascript and node.js.

I installed Node.js and, using npm, serialport and ws packages, and I made the serialport work only with the node command on Windows cmd. I tried to follow some examples and extend the code to my PHP Desktop App using also the ws package but nothing seems to work and now I don't know how to go on.

Do you guys have any kind of suggestion? (Particullary on how to go on with the ws package)

What's the best solution to do this?

Thanks in advance.

Here's the codes that i used:

PHP code:

<?php

session_start();
include_once 'PhpSerial.php';

if(isset($_POST['serial'])) {


    $serial = new phpSerial;

    $serial->deviceSet("COM3");

    $serial->deviceOpen('w+') ;

    $serial->confBaudRate(115200);

    $serial->sendMessage($_POST['serial'],1);

    $_SESSION['serial_data'] = $serial->readPort();

    $serial->deviceClose(); 

}

else
    header('Location: index.php');

?>

JS code: (without the ws part, that didn't want to work)

var serialport = require('serialport');

var portName = "COM3";

var myPort = new serialport(portName, {
    baudRate: 115200
});

myPort.on('open', showPortOpen);
myPort.on('data', readSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + myPort.baudRate);
}

function readSerialData(data) {
   console.log(data);
}

function showPortClose() {
   console.log('port closed.');
}

function showError(error) {
   console.log('Serial port error: ' + error);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试