Go Down

Topic: Reading serial port through PHP on linux (Read 3 times) previous topic - next topic

mohitdaksh

Feb 21, 2012, 02:07 pm Last Edit: Feb 21, 2012, 02:21 pm by mohitdaksh Reason: 1
Hello,
I am making a home automation system which will work over the web.
RIght now, I am trying to do basic serial port IO through PHP. For some strange reason PHP serial class is not working in my ubuntu installation.

So, I tried file IO functions in PHP and fwrite() worked well and sent character data to arduino which my arduino could recognise correctly.

The code for writing to the port is this :
Code: [Select]
<?PHP
$f_pointer =fopen("/dev/ttyS1", "w");
fwrite($f_pointer, chr(1));
flose($f_pointer);
?>

This code sends ASCII of 1 which I read on my arduino and do some stuff.


But the fread function doesn't seem to work, maybe something else is needed than this but I am not sure.

Code: [Select]
<?PHP
$f_pointer =fopen("/dev/ttyS1", "r");
$var=fread($f_pointer, 8); //8 is the character length to be read. arduino sends a char length data
print "$var";
flose($f_pointer);
?>



Is the above code correct? What can be done to read the serial port if PHP serial is not working??

PaulS

Quote
For some strange reason PHP serial class is not working in my ubuntu installation.

The serial class does something. You want it to do something. Clearly, those two somethings are not the same thing, or you would not have posted. But, what those two somethings are you have not explained.

We can't help if you don't.

Quote
But the fread function doesn't seem to work

Pity. I'm sure that the author of the function thought differently before releasing it.

The fread function does something. You want it to do something. Clearly, those two somethings are not the same thing, or you would not have posted. But, what those two somethings are you have not explained.

We can't help if you don't.

By the way, if you ave not modified your Arduino, opening the serial port resets the Arduino. You are not allowing any time for the Arduino to reset before writing/reading. Closing the serial port resets the Arduino, too.

mohitdaksh

PaulS you replied to my earlier post about PHP serial and there I tried everything you suggested and still PHP serial did not work for me. That is why I had to use fwrite. And that is why I have not explained about the PHP serial problem here.


This is the arduino code that I using to test fread
Code: [Select]
    void setup() {
      Serial.begin(9600);
    }


    void loop() {
    Serial.print('1');
    }


And as you earlier suggested I disabled autoreset and still I could not read any data because the arduino is not actually resetting.

coocooworld

Were you able to resolve the problem?

It seem I have the same problem on windows XP. I can write to it fine, I can not read from it unless I use the Arduino compiler/Serial Monitor at least one.

Here's a link to my thread.
http://arduino.cc/forum/index.php/topic,138335.0.html

Go Up