I want to read data in PHP using a USB serial connection. via an Rs232-to-USB cable and a sartorius balance machine. I want to read machine data using a USB COM port and store in a database.
I am trying to use https://github.com/Xowap/PHP-Serial
I don't know how to detect which COM port are being used by my machine.
<?php
include "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("COM1");
$serial->deviceOpen();
$serial->sendMessage("Hello !");
$read = $serial->readPort();
$serial->deviceClose();
$serial->confBaudRate(2400);
echo "<pre>".var_export($serial, true)."</pre>";
?>
This code goes into an infinite loop.
I am using Node js To read serial port and send output to PHP server.
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io')
, com = require("serialport");
var WebSocketServer = require('websocket').server;
// create the server
var wsServer = new WebSocketServer({
httpServer: http.createServer().listen(1337)
});
var serialPort = new com.SerialPort("COM4", {
baudrate: 1200,
dataBits: 7,
parity: 'none',
stopBits: 1,
parser: com.parsers.readline('\r\n')
});
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
serialPort.on('data', function(data) {
//console.log('Received Message: ' + data);
fs.writeFile("data.txt", data, function(err) {
if(err) {
return console.log(err);
}
});
connection.sendUTF(data);
});
});