Pages: [1]   Go Down
Author Topic: PHP - Readport issues  (Read 1301 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :
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:

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












Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then If not understand is necessary put a delay somewhere correct ? But from Arduino side i know how to maybe using delay function.
Code:
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
« Last Edit: April 24, 2012, 08:39:20 am by gnusso » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks
Andrea
« Last Edit: April 24, 2012, 09:39:46 am by gnusso » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ... :-(

Andrea
« Last Edit: April 24, 2012, 10:15:55 am by gnusso » Logged

Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is there someone that can help me with this ?

thanks
Andrea
Logged

Pages: [1]   Go Up
Jump to: