Serial comm with PHP on Windows
Every solution above is either inefficient or too much work.
You can just use the PHP-DIO library (dio_fcntl, dio_open, dio_read, dio_write, dio_seek, ...). It's also in the PHP manual's entry for DIO:
This PECL package isn't available by default. To get it for Windows if you have PHP 5.2.x greater than 5.2.6, you can download it as part of a ZIP:
Thread-safe (for Apache)
Non-thread-safe (for IIS)
Both of these links were found in http://www.deveblog.com/index.php/download-pecl-extensions-for-windows/
Here is the build from Linux, just get it and do the phpize/configure/make/make install thing.
I don't know whether it should be used in an Apache session, but go for it.
Read data form serial port on windows with php
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);
});
});
Communicating serial port on windows with php
You need to determinate COM port number of your USB device via some external command called trough PHP's shell_exec().
For Windows you can try this small tool:
http://todbot.com/blog/2012/03/02/listcomports-windows-command-line-tool-for-usb-to-serial/
https://github.com/todbot/usbSearch/
After you call this tool via shell_exec(), you need to parse it's output (RegExp) and look for exact COM port number based on company/device name.
PHP - Reading COM Port from Windows
If you can guarantee that you're on Windows, I might recommend an interesting approach: Use either COM (as in, Microsoft's COM, not serial port) or .NET.
There's a free .NET class that I regularly use called CommStudio Express. I've found it to be very reliable, but you can always use the standard SerialPort class built into .NET if you don't need to worry about a USB-to-serial adapter getting unplugged randomly.
In any case, it's easy enough to get at a .NET class in PHP with the DOTNET class:
$serial = new DOTNET('system', 'System.IO.Ports.SerialPort');
$serial->PortName = 'COM3';
$serial->Open();
I haven't tested this code (not on Windows at the moment), but something like that should work just fine. You can then proceed to use all of the regular .NET methods within PHP.
Related Topics
Laravel Change Connection Dynamically
Is There a Function Similar to Settimeout() (Javascript) for PHP
Symfony: How to Refresh the Authenticated User from the Database
How to Get a List of Databases
Separate String in Two by Given Position
Counting Values in Multidimensional Array
PHP Date - Get Name of the Months in Local Language
How to Display PHP Code in HTML
PHP Pdo Select Query Returns Double Values
Saving File Using Curl and PHP
Swap Two Words in a String PHP
$_Post Empty on Utf-8 Characters
Perl Equivalent of PHP's Escapeshellarg