I want to tell server via web browser to output control codes to hardware on a COM port.
Any ideas why the following fragment doesn't work- I added the flush() because I thought that was the problem, but no joy...

  	$FileName    ="LPT1, w";
$FilePointer =fopen( $FileName, "w+");
fwrite( $FilePointer, "0");
//ob_flush();
//flush();
fclose( $FilePointer);

    Looks like overkill- I just want to 'bit twiddle' individual lines of the port.
    Still trying... 🙂 Thanks!

      Saw that already- but it is parallel port LPT1 I'm trying to use...

        How about this:-

        $data = "mydata";
        exec("type ".$data." > lpt1");
        

        This is for Windows, would need to use a different external command on *NIX.

          Well that would work if "mydata" is the name of a file in the same directory as the script; if you're looking to output data, you'd probably want to use the echo command instead.

          EDIT: Why do you have "LPT1, w" as the filename for the fopen() command? It should just be "LPT1" - where'd the ", w" come from?

            Still not able to get it to work. I want to use a web page to toggle LPT lines. Present version puts up buttons, calls itself, but shows no sign of changing state of the lpt lines. In other versions I try to call lptout.exe, a program to set lpt lines which works fine from CMD in windows, but again not via php!! Have been trying all sorts of versions of exec, system, type.....
            PS If I change the lpt1 to con in the below, shouldn't I see outpiut on console???
            PPS Yess, the extra ,w was a typo..

            Thanks for suggestions so far... PS the first defns of output bytes should I guess be $oup =chr( 255) etc, but makes no difference,

            <?php

            $oup = 255;
            $oright = 128;
            $odown = 64;
            $oleft = 0;

            if ( isset($_POST['up']))
            {
            exec( "type ".$oup." > lpt1");
            }

            if ( isset($_POST['right']))
            {
            exec( "type ".$oright." > lpt1");

            }

            if ( isset($_POST['down']))
            {
            exec( "type ".$odown." > lpt1");
            }

            if ( isset($_POST['left']))
            {
            exec( "type ".$oleft." > lpt1");
            }

            ?>

            <HTML>

            <HEAD>
            <TITLE>webcam controls</TITLE>
            </HEAD>

            <BODY>

            <center>

            <h1>Web control of server parallel port output pins</h1>

            <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
            <INPUT type='submit' name='up' value='up'>
            <INPUT type='submit' name='right' value='Right'>
            <INPUT type='submit' name='left' value='Left'>
            <INPUT type='submit' name='down' value='Down'>
            </form>

            </body>

            </html>

              Have you tried swapping 'type' for 'echo' in your exec commands? As bradgrafelman stated, 'type' will output file contents, echo will output data supplied as a command line argument in the way you're using.

              You will also need to use '$oup = chr(255)' as you said, if you want to send equivalent of binary 11111111.

              Can you get the output you want by issuing the exec's command directly from within a DOS console?

                No joy using echo. This is all very good for getting me up the php learning curve!
                My intention was to use lptout.exe (off the internet http://smendes.com/el31p/parallel.htm) which works fine from a DOS console, happily setting bit-patterns. It DOES have the idiosyncracy noted by another user that you have to issue it twice per output!

                However, neither the 'echo > lpt1' method, nor my attempts using 'exec' with 'start' and lptout seem to be getting anywhere. VERY frustrating.

                  Well if you have a program that works, let's try getting that working.

                  Can you show us the code you tried to execute this external program? Also, can you tell us where you placed the executable on your system? Where any error messages displayed in PHP?

                    Typical attempt (may have // unnecessarily to escape / symbol??)
                    lptout.exe is in rooot of C:
                    Where am I going wrong??

                    <?php

                    // It is known that 'lptout.exe', in root of C:, correctly works via RUN ->CMD ->command window
                    // eg 'lptout 0' or 'lptout 255' or 'lptout $aa' all work ( if isued twice- see earlier comment)
                    // and LEDs on outport change appropriately.

                    // What I seem unable to do is call the exe to control them from php!!!

                    // Not only does it do nothing on the port, it creates a very slow reload while it waits for something.

                    $xff =chr( 255);
                    $x80 =chr( 128);
                    $x40 =chr( 64);
                    $x0 =chr( 0);

                    if (isset($_POST['up']))
                    // Using the syntax:- start -b(lack boxed) ["window name"] "path to exe", output$, return$
                    // ie I want to issue basically the commands like 'lptout 128' as shown above.

                    {
                    exec( 'start //B "C:/lptout.exe"', $xff);
                    exec( 'start //B "C:/lptout.exe"', $xff);
                    }

                    if (isset($_POST['right']))
                    {
                    exec( 'start //B "C:/lptout.exe"', $x80);
                    exec( 'start //B "C:/lptout.exe"', $x80);
                    }

                    if (isset($_POST['down']))
                    {
                    exec( 'start //B "C:/lptout.exe"', $x40);
                    exec( 'start //B "C:/lptout.exe"', $x40);
                    }

                    if (isset($_POST['left']))
                    {
                    exec( 'start //B "C:/lptout.exe"', $x0);
                    exec( 'start //B "C:/lptout.exe"', $x0);
                    }
                    ?>

                    <HTML>

                    <HEAD><TITLE>webcam controls</TITLE></HEAD>

                    <BODY>
                    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                    <INPUT type='submit' name='up' value='up'>
                    <INPUT type='submit' name='right' value='Right'>
                    <INPUT type='submit' name='left' value='Left'>
                    <INPUT type='submit' name='down' value='Down'>

                    </form>

                    </body>

                    </html>

                      tenochtitlanuk wrote:

                      may have // unnecessarily to escape / symbol??

                      Why would '/' need to be escaped? Furthermore, why would another '/' escape it since the escape character is a backslash?

                      Try getting rid of the double forward slash - this is likely causing an error ("Invalid switch - '/'.").

                      Also, try using [man]system/man instead of exec() to see if anything is being outputted (e.g. an error message)

                        Tried taking out the 'B' and now see the command window I wanted to suppress.
                        I had read elsewhere that if open it made the system wait for completion of a called task... which I do not want.
                        It is open at the folder where the exe exists, rather than root C:, and if I type in the lptout commands they operate. (Belt-and-braces, I had it both in root and the folder holding the php files. Same happens if I use system rather than exec.
                        ie the 'lptout vvv' is not getting inserted in the command window and entered???

                          If I type
                          start /B "John" lptout 33
                          or
                          lptout 33
                          at a CMD window in the Windows XP environment it works.

                          I CANNOT persuade PHP to create the same effect, using
                          exec( ' ') as a way of passing the command.
                          I don't think it's me, but I've tried all sorts of ways to ensure the
                          quotes charcters are correctly passed...

                          Either I misunderstand how to phrase the string in PHP syntax OR PHP
                          (or Apache?) are intercepting calls to hardware addresses, or XP is
                          behaving weirdly.

                          I'd LOVE to know what is/is not going on...
                          Must say it sems to be Microsoft blocking using parallel (& it seems serial) ports. Better move back to my Linux server then, I suppose, where I gather these accesses <are> implemented- if no-one can tell me otherwise. Or, since the screen is regarded benignly by Windows XP on and by browsers, put an light sensor in front of the screen to pick up on/off signals from areas of the screen!

                            6 days later

                            Well I've learned a lot! Code below seems to work. (needs the images- if you want to test it- look at www.diga.me.uk/remote/remote.php)) I changed to sending data to a file, serial at first, found that worked OK using a serial port monitor. Of course I knew serial data would be exactly that, and transitory: I had not realised that parallel data just appears strobed on the output rather than be latched. If I run MS PortMon it seems to show the data IS getting to the port- up to me to make a hardware latch now if I want to stay with LPT- or buy a serially-commanded interface board. I enjoyed making a series of LED images to echo back to the client...

                            Thanks for help- Looks like I'm away now...

                            <HTML>
                            
                            <HEAD><TITLE>webserver screen controls</TITLE></HEAD> 
                            
                            <center>
                            <body bgcolor ="A0C080">
                            
                            <h1>Controlling server output & local client binary LED mimics.</h1>
                            
                            128 controls highest bit, 64 next, then 32....  or 0 for 'all off'.
                            <br>Currently fails to output on server- thanks, MicroSloppy.
                            
                            <br>
                            
                            <img src ="images/key.jpg">
                            
                            <br>
                            
                            <?php 
                            $state = $_POST["state"];
                            if ( $state >255) { $state =255;};
                            if ( $state <0)   { $state =  0;};
                            
                            //$fh =fopen( "LPT1:", "w+"); // for parallel output
                            $fh =fopen( "COM1:", "w+"); // for serial output
                            fputs( $fh, chr( $state));
                            fclose( $fh);
                            
                            if (( $state & 128) ==128)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &  64) == 64)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &  32) == 32)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &  16) == 16)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &   8) ==  8)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &   4) ==  4)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &   2) ==  2)  { echo '<img src ="images/led.jpg">'; }     else { echo '<img src ="images/off.jpg">'; }
                            if (( $state &   1) ==  1)  { echo '<img src ="images/led.jpg"><br>'; } else { echo '<img src ="images/off.jpg"><br>'; }
                            ?>
                            
                            <img src="images/val.jpg">
                            
                            <br>
                            
                            <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                            			<input type ="integer" name="state"  size  =4>
                            			<?php echo '<br>' ?>
                            			<input type ="submit" name ="Submit" value ="Send!">
                            
                            </form>
                            
                            <hr>
                            
                            </body>
                            
                            </html> 
                            
                            
                              Write a Reply...