Anyone used a serial port with PHP?

slimplynth

Senior Member
Have installed/configured Apache/PHP/MySQL...

just playing about with it at the minute, seen (googled) some Ar***** projects using PHP and Realterm to do this... just wondered if anyone had successfully connected to a Picaxe using PHP - (tried searching the forum but obviously "php" as a search term returns quite a few results)... if anyone has achieved this, what extension was used?
 

lbenson

Senior Member
Here is PHP code to get data from a serial port on a linux device (NSLU2). To use it on a PC, I think you can just replace "/dev/ttyUSB0" with, for instance, "COM3" or whatever com port you use.

My preference now is to use Lua instead of PHP. It wouldn't matter on a PC, but PHP is a resource hog on a small device.

Code:
<?php // GetRx gets 315mHz xmits and writes to Rx01.txt
  $fh = fopen("/dev/ttyUSB0", 'r') or die("Can't open file");
  while(!feof($fh))
  {
    $t = trim(fgets($fh)); // message line, e.g., "A 22 22"
    $fh2 = fopen("Rx01.txt", 'a') or die("Can't open file");
    fwrite($fh2,"$t\n");   // write the line
    echo $t;
    fclose($fh2);
  }
  fclose($fh);
?>
 

slimplynth

Senior Member
Cheers, think I will have to build a linux machine... serial port coms seems too much of a big deal with windows.
 

lbenson

Senior Member
>serial port coms seems too much of a big deal with windows

I'm not sure that it is significantly easier with linux. The syntax is the same with PHP--just the designation of the serial port changes. It may need a colon: "COM3:" instead of "COM3"--but may not nowadays.
 

slimplynth

Senior Member
>serial port coms seems too much of a big deal with windows

I'm not sure that it is significantly easier with linux. The syntax is the same with PHP--just the designation of the serial port changes. It may need a colon: "COM3:" instead of "COM3"--but may not nowadays.
Am at work at the mo so can't look at the extensions I tried/installed but the solutions I found by others all listed the problem of not being able to read/write to the serial port when used under windows (think it was write only).. linux users reporting full access.

Thanks for the code though, will try this tonight, thanks also for the link Anobium.

I'm still very very wet behind the ears with PHP - getting it all configured was a 2 day headache in it's self. Thought that MySQL was the icing on the cake but "PHP + Serial" in google, with Picaxe in mind sounds like the future to me.
 

slimplynth

Senior Member
Evenin'... I found the code below (made a few modifications to get past the errors which were generated colons, apostrophe's etc...

my gut feeling is that PHP isn't really the best solution for serial control (that could well be factually incorrect). It seems to be the case for a noob because there doesn't seem to be many in depth guides/tutorials to follow. The easiest on the eye so far being http://www.arduinoprojects.com/?q=node/10

Below is my latest effort.. which generates no errors in google chrome on win xp (ooh mental note.. maybe try a different browser).

At the end of this post is the simple 18M2 code, it just listens (serrxd) waiting for ascii code 49. When tested in the PE terminal... the LEDs lights as expected when the '1' key is pressed.

In the PHP code I used '1' and '49' for the stop and go buttons.. as in '1' or '49' should be sent via serial to com11 when either the 'Go' or 'Stop' buttons are pressed. I thought it best to try this way in case I was getting in a pickle with...

serrxd [100,main],b0

or

serrxd [100,main],#b0

The PHP code was amended to echo the value which should be sent serially (1 or 49)

I was hoping that something obvious would jump out at me so I could delete and post a working solution.. alas.. this feels exactly the same if not worse than over 2 years ago, when a dodgy soldering job was preventing that first 18X from accepting a download.. I hate feeling this lost.

As no error messages are generated, does anyone know of a way to see what (if anything) is being sent to com11 when the button(s) are pressed? (probably another self-instruction to check google there).

I will try next with the DIO extension...


Code:
<?php
    

    if (isset($_POST["rcmd"])) {
        $rcmd = $_POST["rcmd"];
        switch ($rcmd) {
            
	    case 'Stop':
		'mode COM11: BAUD=9600 PARITY=N data=8 stop=1 xon=none';
    		$fp =fopen("COM11", "w+");
                $chrtr = 49;
                fwrite($fp,chr($chrtr)); /* this is the number that it will write */
		fflush($fp);
	 	sleep(1);
                fclose($fp);
                break;

            case 'Go':
		'mode COM11: BAUD=9600 PARITY=N data=8 stop=1 xon=none';
    		$fp =fopen("COM11", "w+");
                $chrtr = 1;
                fwrite($fp,chr($chrtr)); /* this is the number that it will write */
		fflush($fp);
	 	sleep(1);
                fclose($fp);
                break;
            
	default:
                die('???');
        }
    }
?>
<html>
    <head><title>Picaxe Control</title></head>
    <body>
        <center><h1>Picaxe Control</h1><b></b></center>

        <form method="post" action="<?php echo $PHP_SELF;?>">
            <table border="0">
                <tr>
                    <td></td>
                    <td>

                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Stop" name="rcmd"><br/>
                    </td>
                    <td></td>
                    <td>
                        <input type="submit" value="Go" name="rcmd"><br/>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><br><br><br><br><br>

                    </td>
                    <td></td>
                </tr>
            </table>



        </form>

<table>
<tr>
<td>
<?php echo $chrtr;?>
</td>
</tr>
</table>
    </body>
</html>

and the 18M2 prog...
Code:
;comport 11
setfreq M8
symbol LED_Pause = 100

main:

'sertxd ("waiting for data...")

serrxd [100,main],b0

if b0=49 then Goto LED_Flash

Goto Main

LED_Flash:

sertxd ("echo...",b0)

for b1 = 1 to 10
High B.4
pause LED_Pause
High B.5
pause LED_Pause
High B.6
pause LED_Pause
High B.7
pause LED_Pause
Low B.4,B.5,B.6,B.7
pause LED_Pause
next b1

goto main
 

slimplynth

Senior Member
... this was a joke that wasn't funny, i deleted it and replaced it with a sensible waste of space.
 
Last edited:

hippy

Technical Support
Staff member
serrxd [100,main],b0

I'm not sure I understand the need for any timeout; why not simply wait for whatever turns up, whenever that turns up, rather than baling out and waiting again ?

The only thing a timeout gives you is an increased chance of not being in SERRXD when the character is sent from the PHP server.

As to monitoring if anything is sent via COM11 from the PHP server; a LED+R between TX and 0V with cathode towards 0V should flash whenever a character is sent. Or simply connect with a cross-over cable to another serial port / PC running Programming Editor Terminal or some other terminal emulator and see what arrives. I'd change "$chrtr = 1;" to "$chrtr = 48;", then you are always sending printable / displayable characters, "0" or "1".

If data isn't coming out of the PHP server, I'd suspect that the COM port isn't being initialised correctly, most likely handshaking, though I've never used PHP. You can probably add 'die' or even a file write so you can log the code going through the case statements.

You can also test the PICAXE program by sending to it from Programming Editor Terminal or a terminal emulator.
 

slimplynth

Senior Member
Thanks Hippy, i'll digest all that in a min.. though the reason I used 1 and 49 was avoid any confusion with #b0 and b0... though I think i was doing fine, it was a last ditch attempt to prove before i snapped. I'm guessing the PNS net server will eventually seem to have been the more economical route.

Thanks again for your help.
 

slimplynth

Senior Member
oh yes the timeout... i should prolly take that out, you're (as always right - even though you haven't tried PHP).. i put it in there because originally
(now commented out - i wanted the message 'waiting for data...' to be displayed.. that was early on in the mission to read serial data.

Any chance you could just absorb php and tell me (everyone) how to do a simple LED on/off? :D i'll get it eventually (like everything else i need: sever security...) - it's just at what point do you think "any more time and i'd have been better off buying the PNS"...

I keep dropping hints about birthday/xmas but people seriously seem to think christmas is nothing to do with toys any more... had to buy my own rc helicopter... a 3Ch offering from maplins which is surprisingly good to fly for 30 quid. (had to ask a pensioner for 'mi chopper back' on christmas day.. felt like a 12 yr old again)
 
Top