PHP - Readport issues

Hi all,
I've did this I can send data to the port but I'm not able to reach the answer from arduino maybe there is something that I've missed ? Im using ubuntu configured well below the code :

<html>
<body>
<h1>It works!</h1>
<p>ARDUINO PHP.</p>
<p>LA MIA PRIMA PAGINA IN PHP.</p>

<p>dentro allo script</p>

<?php
require("php_serial.class.php"); 

$read = "Initial Value";
function writeToArduino($str){

     $serial = new phpSerial();
     $serial->deviceSet("/dev/ttyACM0");
     $serial->confBaudRate(9600);
     $serial->confCharacterLength(8);

     $serial->deviceOpen();
           for ($i=0; $i<strlen($str); $i++){
                 $serial->sendMessage($str[$i]);
                 $read = $serial->readPort();
                 

           }
 
     $serial->deviceClose();

}
$tmptext = "12345ABCDE";
 
writeToArduino($tmptext);
print("<H3> Get back the value from arduino $read </H3>\n"); 
?> 
</body>
</html>

Arduino code:

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, 
		Serial.write(45); // send a byte with the value 45
                int bytesSent = Serial.write(“hello”); 
        }
}

could you help me ? thanks gnusso

Every time you open and close the serial port, you reset the Arduino. With that code, the Arduino has barely had time to start collecting the input data when you reset it by closing the serial port.

Obviously, it does not have time to collect the data and formulate a reply, let alone send the reply. That is why you can't read the response.

Then If not understand is necessary put a delay somewhere correct ? But from Arduino side i know how to maybe using delay function.

 delay(1000);

but from php side how i can do ? could you give kindly me a tip ? in order to proceed with sense and in the right way ?
at least a simply code example to follow for sycronize trasmission from php and the answer from arduino ...

Thanks 1000 for your help gnusso

PHP has a sleep() function. Time to sleep is in seconds.

Ok, but in which point I've to put the sleep in my php and delay in arduino code for syncronize everyhting ?

Thanks
Andrea

You don't need a delay on the Arduino. It's going to take long enough resetting, as it is.

After you send data to the Arduino, you need to sleep() long enough for the Arduino to reset, read the data that was sent, and generate a response.

Or, you might want to look at some of the ways of preventing the Arduino from resetting.
http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

I think that for the moment is better use sleep time, but how know many time I've to sleep ? how I can sure that I received back the message on my php page in the right moment ? and the rest of the php code i think is correct ... fixed this I think that I can start to understand better how it's working thanks for the support ...

I've tried to put sleep(1) between send data and readport, but still to have the variable void ... :frowning:

Andrea

is there someone that can help me with this ?

thanks
Andrea