Two Shell_Exec Command with PHP button but still in the One powershell?

笑着哭i 提交于 2019-12-07 09:31:54

问题


I have Powershell syntax that can get data using ReadExisting(), but the problem is... that syntax must compele php condition before (and some shell_exec syntax when page load)

I trying Get COM1 Data using powershell, and its working with this code

cmd powershell

in that image, the first ReadExisting() can't declare the output because the device in COM1 not showing new result, when the devices show the result, trying the ReadExisting() again and i get what i want.

So (what in my opinion) the logic i should implement on PHP is : - Execute $port and $port.Open() first - Execute ReadExisting() after that.

I'm trying making the php code like this

<?php
    $result = shell_exec('powershell $port = New-Object System.IO.Ports.SerialPort COM1,9600,None,8,one ; Get-Variable ;  $port.Open(); $port.ReadExisting() ');

    echo $result 

?>

<form method="post">
    <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>

<?php
    function testfun()
    {
      $result2 = shell_exec('powershell $port.ReadExisting()');
      echo $result2;
    }

    if(array_key_exists('test',$_POST)){
       testfun();
    }
?>

but the button not showing the result like that in the powershell. What i can see problably because i'm making two shell_exec?, and yes the $result is working (i'm already testing it and i can see the port connection variable)

it's there possible method making continues shell command with button condition? thank you!.


回答1:


I think the sessions of Powershell can be of help. This kind of problems are usually solved with that. The detailed documentation of starting and maintaining of sessions are available here

If you use a session, the commands can have continuity.




回答2:


Use this: (2>&1 in shell_exec)

$result2 = shell_exec('powershell $port.ReadExisting() 2>&1');
echo $result2;

>& is the syntax to redirect a stream to another file descriptor - 0 is stdin, 1 is stdout, and 2 is stderr.

2 refers to the second file descriptor of the process, i.e. stderr.

> means redirection.

&1 means the target of the redirection should be the same location as the first file descriptor, i.e. stdout.



来源:https://stackoverflow.com/questions/56032763/two-shell-exec-command-with-php-button-but-still-in-the-one-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!