Jump to content

KenHorse

Members
  • Posts

    70
  • Joined

  • Last visited

Posts posted by KenHorse

  1. I found the fix

    On the Debian 12 server, I added the following to /etc/ssh/sshd_config

     

    HostKeyAlgorithms +ssh-rsa
    PubkeyAcceptedKeyTypes +ssh-rsa

     

    As this server is on a private LAN, security isn't an issue

  2. I've been using the following script snippet to connect to a remote server in order to run lm-sensors on the remote server and it's worked fine (I've changed the IP and password for this post of course)
     

    $connection = ssh2_connect('xxx.xxx.xxx.x', 22); 
    ssh2_auth_password($connection, 'root', 'xxxxxx');
    
    if($output = ssh2_exec($connection, 'sensors')) { 
      stream_set_blocking($output, true); 
      $x = stream_get_contents($output); 
    }


    This script runs on a Debian 10 machine and the remote server USED to also be Debian 10 but it was recently upgraded to Debian 12 (Bookworm) and now the script reports the following errors:
     

    PHP Warning: ssh2_connect(): Error starting up SSH connection(-5): Unable to exchange encryption keys in /var/www/html/get_temp.php on line 3 
    PHP Warning: ssh2_connect(): Unable to connect to xxx.xxx.xxx.x in /var/www/html/get_temp.php on line 3 
    PHP Warning: ssh2_auth_password() expects parameter 1 to be resource, bool given in /var/www/html/get_temp.php on line 4 
    PHP Warning: ssh2_exec() expects parameter 1 to be resource, bool given in /var/www/html/get_temp.php on line 6


    I can connect using ssh from the command line (ssh @ xxx.xxx.xxx.x) and that works fine.

    I'm not a total newbie to Linux but I'm no expert either so any help would be appreciated

  3. 10 minutes ago, ginerjm said:

    OK - I cleaned up your function to be able to make better sense of it (nothing wrong) and this is what I see - Check the CAPS.

    function checkInput($stuff)	// WHAT IS $STUFF USED FOR? Not in here.
    {
    	$starttime = microtime(true);	
    	//  WHERE ARE $READ AND $TIMEDIFF DEFINED - NOT IN THIS FUNCTION
    	while($read == "" && $timediff < '5')
    	{
    		$read = $serial->readPort();	// WHERE IS SERIAL DEFINED - NOT IN THIS FUNCTION
    		$substring = substr($read,0,1);
    		if($substring == "-")
    		{			
    			print"<CENTER><H2>We encountered an error when sending $read". "Please close window and try again</H2></CENTER>";	
    			$noDataToSend = "False";	
    			exit();		
    		}
    		$endtime = microtime(true);
    		$timediff = $endtime - $starttime;
    		if($substring == "+")
    		{   //if ACK received, reset timer
    			$starttime = microtime(true);
    		}
    		else
    		{
    			if($substring != "")
    			{
    				echo "Substring " . "$substring\r\n";
    			}			
    		}		
    		if($timediff > '4')
    		{
    			print"<CENTER><H2>No response from the Station. Please close this window, check your serial connections and try again</H2></CENTER>";	
    			$noDataToSend ="False";	
    			exit();
    		}
    		sleep(0.1);	
    
    	}//while
    } //end function

    Do you understand how functions work with variables?  If one is not defined inside the function it has to be passed into it either as an argument (preferred) or as a global variable.  

    As my OP said, I'm using php_serial_class, which is loaded in global.php (hence the include):

    require_once("php_serial.class.php");
    
    // Let's start the serial class
    $serial = new phpSerial;
    
    $comport = "/dev/ttyUSB0";
    
    $serial->deviceSet($comport);
    
    // We can change the baud rate
    $serial->confBaudRate(19200);
    $serial->confParity("none");
    $serial->confCharacterLength(8);
    $serial->confStopBits(1);
    $serial->confFlowControl("none");
    
    // Then we need to open it
    $serial->deviceOpen();
    //clear out crap
    $serial->sendMessage("\r");
    $serial->sendMessage("\r");
    sleep(0.1);
    
    // Or to read from
    //$read = $serial->readPort();
    
    //to send to
    //$serial->sendMessage("data to send");
    
    // If you want to change the configuration, the device must be closed
    //$serial->deviceClose();
    //and then reopened

    Also, $stuff is not yet used but I plan on using it in the function after I get passed this issue

    As for $timediff, yes. I have not defined it yet. Again, I need to get passed this to clean other things up (I guess I should point out that this code was originally written by someone else long before PHP 7.0 was released. I'm trying to get it functional under 7.3xxx but I am certainly not overly fluent in PHP yet)

  4. function checkInput($stuff){
    	$starttime = microtime(true);	
    	while($read =="" && $timediff < '5'){	
    		$read = $serial->readPort();
    		$substring = substr($read,0,1);
    		//echo "SubString " . "$substring";
    		if($substring =="-"){			
    				print"<CENTER><H2>We encountered an error when sending $read". "Please close window and try again</H2></CENTER>";	
    				$noDataToSend ="False";	
    				exit();		
    		}
    		$endtime = microtime(true);
    		$timediff = $endtime - $starttime;
    		if($substring =="+"){   //if ACK received, reset timer
    			$starttime = microtime(true);
    		}else{
    			if($substring !=""){echo "Substring " . "$substring\r\n";}			
    		}		
    		if($timediff > '4'){
    			print"<CENTER><H2>No response from the Station. Please close this window, check your serial connections and try again</H2></CENTER>";	
    			$noDataToSend ="False";	
    			exit();
    		}
    		sleep(0.1);	
    		
    	}//while
    } //end function

    And the line calling it:

    $senddata = "122222";		
    $serial->sendMessage("$senddata\r");
    checkInput($senddata);	

    Yes, I am not currently using the passed parameter ($stuff) but am planning to after I fix this problem

  5. BTW, PHP version is 7.3.29-1

     

    So I changed the variable in the Function:

     

    function checkInput($stuff){

     

    No change

    PHP Fatal error:  Uncaught Error: Call to a member function readPort() on null in /var/www/controlserial.php:371

     

  6. I have a file (global.php) that used by many different other files in a project I'm doing. This file contains many things including connecting to a MySQL database as well as loading a php_serial_class.

    So at the top of php file that needs these resources, I use

    include("global.php");

    In one of those other php files, I also have a function that I'm using that calls the php_serial_class:

    $read = $serial->readPort();

    This throws an error:

    PHP Fatal error:  Uncaught Error: Call to a member function readPort() on string in /var/www/controlserial.php:370


    Stack trace:
    #0 /var/www/controlserial.php(104): checkInput('11111')
    #1 {main}
      thrown in /var/www/controlserial.php on line 370
    root@mypi:/var/www/#

    Line 104 is the call to the function and Line 370 is the actual read call to the class

    I assume this error is thrown as the function doesn't know about that class? If so, can I simply also load the include within the function or is there a better way to do it?

  7. Ok, finally figured out what I needed to do in order to install the dio.so extension and that succeeded. Updated both php.ini (web and cli) to load extension dio.so

    I created a test file with the following contents:

     

    $fd = dio_open('/dev/ttyUSB0', O_RDWR | O_NOCTTY | O_NONBLOCK);
    
    dio_close($fd);

    Now, when I run it, I receive the following error:

     

    PHP Warning:  PHP Startup: Unable to load dynamic library 'dio.so[PHP]' (tried: /usr/lib/php/20180731/dio.so[PHP] (/usr/lib/php/20180731/dio.so[PHP]: cannot open shared object file: No such file or directory), /usr/lib/php/20180731/dio.so[PHP].so (/usr/lib/php/20180731/dio.so[PHP].so: cannot open shared object file: No such file or directory)) in Unknown on line 0

    I have checked /usr/lib/php and it IS there:

     

    root@mypi:/var/www/html/WebRCP# ls /usr/lib/php/20180731/
    build        curl.so  fileinfo.so  iconv.so     mysqlnd.so    phar.so      simplexml.so  sysvshm.so    xmlreader.so
    bz2.so       dio.so   ftp.so       json.so      opcache.so    posix.so     sockets.so    tokenizer.so  xmlwriter.so
    calendar.so  dom.so   gd.so        mbstring.so  pdo.so        readline.so  sysvmsg.so    wddx.so       xsl.so

     

     

  8. On 10/18/2021 at 2:58 PM, kicken said:

    This code worked for me using a VM with ubuntu on it.  When reading the results after sending the command it will hang for at least as long as the timeout period before returning results since there's no way to determine when the serial device is done sending data otherwise.

    <?php
    
    $cmd = implode(' ', array_slice($argv, 1));
    
    $device = serialSetup('/dev/ttyS0', 115200, 8);
    var_dump(serial($device, $cmd ?: 'uname -a'));
    fclose($device);
    
    
    function serialSetup($device, $baud, $data){
        $cmd = sprintf('stty -F %s %d cs%d -echo raw', $device, $baud, $data);
        exec($cmd, $output, $ret);
        if ($ret !== 0){
            var_dump($output);
            throw new \RuntimeException('Unable to configure serial device');
        }
    
        $f = fopen($device, "w+");
        if (!$f){
            throw new \RuntimeException('Unable to open serial device.');
        }
    
        stream_set_blocking($f, false);
        return $f;
    }
    
    function serial($device, $cmd){
        fwrite($device, $cmd . "\n");
        $result = '';
        do {
            try {
                $result .= serialRead($device, 5);
                $timeout = false;
            } catch (RuntimeException $ex){
                $timeout = true;
            }
        } while (!$timeout);
    
        return $result;
    }
    
    function serialRead($device, $timeout = 30){
        $r = [$device];
        $w = $e = [];
        $n = stream_select($r, $w, $e, $timeout, 0);
        if ($n === 1){
            $line = fgets($device) ?: '';
            if ($line !== ''){
                return $line;
            }
        }
    
        throw new \RuntimeException('Timeout reading from serial device');
    }

    For the curious, this also sort of works on windows.  The timeout cannot be controlled on windows, so you're stuck with the default (around 120 seconds).  The stty call needs to be replaced with the following call to mode and of course use the appropriate device name (ie, COM1).  The important bit seems to be the to=on parameter in the call to mode, without it windows seems to never timeout on a read and the script would hang indefinitely.

    $cmd = sprintf('mode %s BAUD=%d PARITY=n DATA=%d to=on', $device, $baud, $data);

     

    I changed to /dev/ttyUSB0 and baud rate of 57600 to match my needs. I should receive "+1111" back from the external device. Script is named test

    root@mypi:~# php test 11111

    string(0) ""
    root@mypi:~#

  9. 2 hours ago, gw1500se said:

    This is probably really a raspbian issue so you may want to try the raspberry forum. However, what do you get when you run this script?

    for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
        (
            syspath="${sysdevpath%/dev}"
            devname="$(udevadm info -q name -p $syspath)"
            [[ "$devname" == "bus/"* ]] && exit
            eval "$(udevadm info -q property --export -p $syspath)"
            [[ -z "$ID_SERIAL" ]] && exit
            echo "/dev/$devname - $ID_SERIAL"
        )
    done

     

    /dev/snd/controlC1 - C-Media_Electronics_Inc._USB_PnP_Sound_Device
    /dev/ttyUSB0 - FTDI_FT232R_USB_UART_AH05WJ52

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.