Arduino and php serial class

Hello again :slight_smile:
I figured out these days how to control my Arduino with PHP, using the php_serial.class (PHP Serial: Communicate with a serial port - PHP Classes).
I am able to send messages to Arduino and make it turn on and off two leds.
The problem appears when I want to read the port for messages sent by the Arduino. If I keep the Serial Monitor running I can see that Arduino sends responses and they appear in the Serial Monitor, however I can't seem to read those messages using PHP.
Here is my PHP code:

 <?php 


/* Simple serial script for turning two leds on 
and off from the web! 

Utilizes the PHP Serial class by Rémy Sanchez <thenux@gmail.com> 
(Thanks you rule!!) to communicate with the Arduino Serial! 

*/ 


//check the GET action var to see if an action is to be performed 
if (isset($_GET['action'])) { 
    //Action required 
     
    //Load the serial port class 
    require("php_serial.class.php"); 
     
    //Initialize the class 
    $serial = new phpSerial(); 

    //Specify the serial port to use... in this case COM1 
    $serial->deviceSet("/dev/ttyACM0"); //SET THIS TO WHATEVER YOUR SERIAL DEVICE HAPPENS TO BE, YOU CAN FIND THIS UNDER THE ARDUINO SOFTWARE'S MENU
     
    //Set the serial port parameters. The documentation says 9600 8-N-1, so 
    $serial->confBaudRate(9600); //Baud rate: 9600 
	$serial->confParity("none");  //Parity (this is the "N" in "8-N-1")
	$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
	$serial->confStopBits(1);

    //Now we "open" the serial port so we can write to it 
    $serial->deviceOpen(); 

    //Issue the appropriate command according to the Arduino source code 0=Green On, 1=Green Off, 2=Red On, 3=Red Off.
    if ($_GET['action'] == "greenon") { 

        //to turn the GREEN LED ON, we issue the command  
        $serial->sendMessage("0\r"); 
$read = $serial->readPort();
echo $read;
     
    } else if ($_GET['action'] == "greenoff") { 
        //to turn the GREEN LED OFF, we issue this command 
        $serial->sendMessage("1\r"); 
$read = $serial->readPort();
echo $read;
    }
	
	if ($_GET['action'] == "redon") { 
        //to turn the RED LED ON, we issue the command  
        $serial->sendMessage("2\r"); 
$read = $serial->readPort();
echo $read;
     
    } else if ($_GET['action'] == "redoff") { 
        //to turn the RED LED OFF, we issue this command 
        $serial->sendMessage("3\r"); 
$read = $serial->readPort();
echo $read;
    } 
     
    //We're done, so close the serial port again 
    $serial->deviceClose(); 

} 


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Arduino Project 1: Serial LED Control</title> 
</head> 
<body> 

<h1>Arduino Project 1: Serial LED Control</h1> 
<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenon" ?>">
Click here to turn the GREEN LED on.</a></p> 
<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenoff" ?>">
Click here to turn the GREEN LED off.</a></p> 
<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=redon" ?>">
Click here to turn the RED LED on.</a></p> 
<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=redoff" ?>">
Click here to turn the RED LED off.</a></p> 
</body> 
</html>

And here is the Arduino code:

 int ledPin13 = 13; // the pin that the green LED is attached to
 int ledPin12 = 12; // the pin that the red LED is attached to
 int incomingByte;      // a variable to read incoming serial data into
 void setup() {
   
   Serial.begin(9600); // initialize serial communication
  
   pinMode(ledPin13, OUTPUT);  // initialize the green LED pin as an output
   pinMode(ledPin12, OUTPUT);  // initialize the red LED pin as an output
 }
 void loop() {
   // see if there's incoming serial data:
   if (Serial.available() > 0) {
     
     incomingByte = Serial.read(); // read the oldest byte in the serial buffer
//Preform the code to switch on or off the leds
    if (incomingByte == '0') {
Serial.println("0"); 
    digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on
  } 
   if (incomingByte == '1') {
   digitalWrite(ledPin13, LOW); //If the serial data is 1 turn red LED off
 Serial.println("1"); 
 }
 
     if (incomingByte == '2') {
    digitalWrite(ledPin12, HIGH); //If the serial data is 2 turn green LED on
Serial.println("2"); 
} 
   if (incomingByte == '3') {
   digitalWrite(ledPin12, LOW); //If the serial data is 3 turn green LED off
 Serial.println("3"); 
 }

   }
 }

From what I've seen, there is no easy way to use PHP to obtain data from a serial port.

Is there a hard way then? :slight_smile:

It's possible that dio may help you. It is helpful for me in reading serial from Arduino.

http://pecl.php.net/package/dio