php serial communication in linux

前端 未结 4 1948
时光说笑
时光说笑 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: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.

提交回复
热议问题