I want to read data in php using USB serial. I am Using Rs232 to USB cable. I have sartorius balance machine. now i want to read machine data using USB com port. and store
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);
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1><a href="index.php?status=1">ON</a></h1>
<h1> <a href="index.php?status=2">off</a></h1>
<?php
//calling php serial class
require_once 'php_serial.class.php';
$serial=new phpSerial();
$serial->deviceSet('/dev/ttyACM2');
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
if($_GET['status']){
$serial->sendMessage($_GET['status']);
$read=$serial->readPort();
var_dump($read);
echo $read;
}
?>
</body>
</html>
int led=3;
void setup(){
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop(){
int x=Serial.read();
if(x=='1'){
digitalWrite(led,HIGH);
Serial.write(x);
}
if(x=='2'){
digitalWrite(led,LOW);
Serial.write(x);
}
}