php serial communication in linux

前端 未结 4 1933
时光说笑
时光说笑 2021-01-12 07:02

I have installed xampp in fedora 13.I am trying to communicate with microcontroller through serial port using php serial class. My code is example.php

inclu         


        
相关标签:
4条回答
  • 2021-01-12 07:20

    I did this once with Debian to control an Arduino board with a PHP script and initially ran into the same problem.

    In Debian, you need to add the Apache user to the dialout group in order to allow it to make serial connection requests. I would assume the same is true for Fedora.

    In Debian the command is:

    useradd -G dialout www-data
    

    However I believe Fedora names the Apache user as apache instead. I don't have a Fedora machine to test on, but I would assume the command you need to run is:

    useradd -G dialout apache
    

    You will then need to restart your xampp server.

    See the following for reference:

    http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/ http://fedoraproject.org/wiki/Administration_Guide_Draft/Apache#Apache_File_Security

    Neal

    0 讨论(0)
  • 2021-01-12 07:29

    Credit goes to Marc B's comment for causing me to look this up, and he's dead on: http://www.phpclasses.org/browse/file/17926.html

    function deviceSet ($device)
    {
        if ($this->_dState !== SERIAL_DEVICE_OPENED)
        {
            if ($this->_os === "linux")
            {
                if (preg_match("@^COM(\d+):?$@i", $device, $matches))
                {
                    $device = "/dev/ttyS" . ($matches[1] - 1);
                }
    
                if ($this->_exec("stty -F " . $device) === 0)
                {
                    $this->_device = $device;
                    $this->_dState = SERIAL_DEVICE_SET;
                    return true;
                }
            }
            elseif ($this->_os === "windows")
            {
                if (preg_match("@^COM(\d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device)) === 0)
                {
                    $this->_windevice = "COM" . $matches[1];
                    $this->_device = "\\.\com" . $matches[1];
                    $this->_dState = SERIAL_DEVICE_SET;
                    return true;
                }
            }
    
            trigger_error("Specified serial port is not valid", E_USER_WARNING);
            return false;
        }
        else
        {
            trigger_error("You must close your device before to set an other one", E_USER_WARNING);
            return false;
        }
    }
    

    I believe that calling $serial->deviceSet("/dev/ttyUSB0"); will fix it, but you may have to modify the source of php_serial.class.php to work on /dev/ttyUSB instead of /dev/ttyS.

    0 讨论(0)
  • 2021-01-12 07:37

    First test a hello world type php script to testyour basic installation.

    Then verify the web server / php engine is running as a user which is in a group allowed to access the applicable /dev/ttyWHATEVER device file corresponding to the serial port. It would be surprising if that were true by default - you'll probably have to add it to the 'dialout' or similar group.

    Add some fault checking / reporting to your code.

    0 讨论(0)
  • 2021-01-12 07:45

    Could you post the lines near / related to "/opt/lampp/htdocs/xampp/php_serial.class.php on line 147"?

    I suspect that you are trying to set the device incorrectly (as Marc indicated). Either that or the port is already in use from other testing you are conducting at the same time. I'm not sure if the script you are running provides errors specific to ports you are trying to attach to already being in use.

    0 讨论(0)
提交回复
热议问题