PHP fread arduino serial com doesn't work. PLEASE Help!!!

I've been searching for the last two weeks to solve a simple problem but have no luck. I'm registering so I can ask the experts here at Arduino.

Here's the operating system specs

OS: Windows XP
Programs: Apache PHP
Hardware: Arduino nano v3, USB communication via COM3 port

Arduino Code (pseudo code):

I send the Arduino a command (read something), it read a port and send back a number via Serial.print().

Results: this works great when I use the Arduino's compiler (v1.0.1) or serial monitor to send and receive command

The problems:
Sending the commands via PHP or sending anything to Arduino works great, it picks up everything.
BUTTTT, reading from the serial COM port is a bigggg problem

PHP code:

<?php

exec (`mode COM3: BAUD=9600 PARITY=N data=8 stop=1 xon=off`);

if (@$fp=fopen("COM3", "rb+")) {

        fwrite($fp, "r");   //tell Arduino to read
	sleep(5);             //wait 5 seconds before getting result
	$raw="";

	$raw=stream_get_contents($fp);         //this doesn't work
	//$raw=fread($fp,4096);                       //this doesn't work
        //$raw=fget($fp,4096);                         //this doesn't work
        /******The below doesn't work
	while(!feof($fp)){
    	    $raw.=stream_get_contents($fp, 2);
  	}
        ******/
	
	
	fclose($fp);
}

echo $raw;


?>

Here is what I observed:

When arduino board first turn on (plug into my usb, without using Arduino's Serial Monitor ),
Using PHP:

  1. I CAN use fwrite
  2. I CAN NOT use fread even with delays
  3. I CAN NOT use stream_get_contents

Here is what weird....
When arduino board first turn on (plug into my usb),
Using Arduino's 1.0.1 compiler serial monitor:

  1. I open the serial monitor
  2. I send it a command and received a response
  3. I then closed the serial monitor
    After using the Arduino's serial monitor or compiler without switching the board off
  4. I CAN use fwrite
  5. I CAN use fread
  6. I CAN use stream_get_contents

I'm sure the solution lies somewhere in how the Arduino first initiate the board and the COM port to allow fread to access it freely. Without first initializing the Arduino through the Arduino's compiler, I CAN NOT access the board using fread, I can send command to it just fine.

I'm sure someone with more experience in how Arduino initiate the COM port can solve this. I've searched the web for two weeks and have not found the answer. The "Google God" did not provide an answer this time. Please help.

**TLDR: ** fread doesn't work unless you first run Arduino's "Serial Monitor" or compiler to talk to it first

Begging for solutions. Sincerely, CooCooWorld

Begging for solutions.

Use a real operating system. Windows sucks.

The PHP serial class can not read from the serial port on Windows due to some fundamental flaw in how Windows handles the serial port stream.

I dug into exactly what the issue was, years ago, but I've forgotten exactly what the problem was. I just know that it can't be fixed given Window's handling of the serial port.

There are, of course, plenty of alternative scripting languages that use Windows-exposed APIs to deal with the serial port that can work bi-directionally on Windows.

Or, create an executable application, instead.

While I'll eventually move to Linux once I get my Rpi, I would like to resolve this problem for future potential Arduino users.

Obviously, the developers of Arduino managed to resolve the issue with Windows serial communication to allow their boards to be programmed. I am hoping someone from the team can share some insight/codes on how they managed to initialize the board for reading and writing.

Or, perhaps is there a way I can execute the "Serial Monitor" once and then close it so I can access the board with PHP. Perhaps a standalone version of "Serial Monitor" from the Arduino team can be develop to run and then close.

There are, of course, plenty of alternative scripting languages that use Windows-exposed APIs to deal with the serial port that can work bi-directionally on Windows.

Or, create an executable application, instead.

Perhaps you can share with us what those programs are or how to create and executable application as I am new to the development world.

Thanks.

You are probably a victim of the bootloader timeout.

Opening the serial port resets the arduino. There is a two second period after you open the port from PHP during which your program is not yet running. If you send data during this window, the bootloader eats it.

In the PHP code it appears you write to the serial port immediately after opening it. Try waiting two seconds after the open and see if it helps.

-br

Writing to the port works flawlessly. The Arduino picks it up right away. It's the reading afterward that is the problem. If I use the Arduino's compiler to upload code or use it's Serial Monitor once before running my PHP, everything works great.

It is the cold start of the board without activating the Arduino Compiler or Serial Monitor that my PHP can't read the board. Writing to it still work on cold start.

@billroy

To bypass to bootloader timeout, I disabled the AutoReset by using this method:

http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection

Noted that to upload to the board, you must first disconnect this resistor.

Obviously, the developers of Arduino managed to resolve the issue with Windows serial communication to allow their boards to be programmed.

Not using PHP, they didn't. Don't get me wrong. I love PHP for dynamic web pages, but it is not an ideal tool for communicating with the serial port on Windows.

The PHP serial class can not read from the serial port on Windows due to some fundamental flaw in how Windows handles the serial port stream.

I think you have that backwards. Windows handles serial port activities with properly written aplications with no problems. As far as I know the PHP developers did not include hardware I/O in their application. PHP is a file/text/database type of application. Windows will treat a com port as a file and write to it when requested. Note that the "f" in fopen and fwrite probably stands for "file".

Someone have already answered.
Php can't do that in windows; you need a php extension to have the job done in windows, that means c+zend if you want to use "full" php function.
Php_serial_class works read/write only on linux (casualty?)
If you can, better to use linux or take a look at this project on sourceforce ArduinoPHP download | SourceForge.net).
Good luck