1

I have a piece of simple code that works on a Windows - WAMP environment, e.g.

<?php

`mode com3: BAUD=38400 PARITY=N data=8 stop=1 xon=off`;

file_put_contents(com3, chr(1).chr(255).chr(1).chr(4).chr(64).chr(5));

?>

It connects via a USB cable (using USB-Serial drivers) to a circuit-board to light some LEDs, and it works fine - so similarly windows software like 232Analyser, can connect to COM3 and send code in DEC like 1,255,255,255,255,5, and light the LEDs. The number 255, is a DEC number from: 1,2,4,8,16,32,64,128,255 which will light a certain LED depending on which number is called.

Anyway, the code above works fine on Windows, and lights LEDs by calling this PHP file. So can call a URL like: http://localhost/lightled.php which works ok.

Now I need it to work via Linux, on a Raspberry Pi, so I have just installed standard Raspberry Linux, and Apache with PHP.

Then attached the USB cable, and it appears as /dev/ttyUSB0 I have then CHMOD 777 /dev/ttyUSB0

And changed the PHP code to:

<?php

`mode /dev/ttyUSB0: BAUD=38400 PARITY=N data=8 stop=1 xon=off`;

file_put_contents('/dev/ttyUSB0', chr(1).chr(255).chr(1).chr(4).chr(64).chr(5));

?>

However calling this file on Linux in a browser is not Lighting the LEDs, as it does on Windows.

Now when I call this file it goes through with no errors, without chmod 777, it gave a permission denied error. So it seems like it goes through ok, but something else is wrong.

So question is anyone know how to make it work on Linux, it might be I am calling the USB wrong, or Raspberry Linux needs some kind of drivers, or " `mode " needs to be defined differently ...or maybe the decimal/binary code sent is not right like "chr(2)" etc. needs to be different and wont be sent in same way on a LAMP setup.

Any ideas on what I can try?

1
  • Put a monitor at the other end of the serial link and see what the Pi is sending.
    – joan
    Commented Sep 5, 2015 at 7:46

3 Answers 3

1

Try composer require hyperthese/php-serial

The only tiny gotcha is you need to make sure the user/program is able to access the dialout group. For example, apache requires a sudo usermod -a -G dialout www-data

You can access it via composer autoload or just load the file directly and set up an object to control the lights, for example:

include_once 'PhpSerial.php';

if (!function_exists('open_serial_comms')) {
    function  open_serial_comms()
    {
        // Let's start the class
        $serial = new PhpSerial;

        // First we must specify the device. This works on both linux and windows (if
        // your linux serial device is /dev/ttyS0 for COM1, etc)
        $serial->deviceSet("USB0");

        // We can change the baud rate, parity, length, stop bits, flow control
        $serial->confBaudRate(38400);
        $serial->confParity("none");
        $serial->confCharacterLength(8);
        $serial->confStopBits(1);
        $serial->confFlowControl("xon/xoff");

        // Then we need to open it
        $serial->deviceOpen();

        //Finally we need to return the object
        return $serial; // all open boss, send some data!
    }
}

When you are done with it you can $serial->deviceClose()

https://github.com/Xowap/PHP-Serial

0

On a Linux system, you will not use the mode command to configure your com ports. This can be done with setserial or stty instead. See here for syntax.

You probably want stty 38400 cs8 -parenb -cstopb ixon -F /dev/ttyUSB0

38400 = 38400 baud

cs8 = 8bit data

-parenb = no parity check

-cstopb = 1 stop bit

ixon = enable xon/xof

-F = the device file you're accessing

0

I found this old question and it is look like a recent issue a had with permissions. Did you set the "chmod 777" permission to the device?

sudo chmod 777 /dev/ttyUSB0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.