Pages: [1]   Go Down
Author Topic: Reading serial port through PHP on linux  (Read 3199 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
discovering the fun in electronics
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
<?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:
<?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??
« Last Edit: February 21, 2012, 08:21:07 am by mohitdaksh » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49330
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
discovering the fun in electronics
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: