4

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!.

dooooooofai
  • 88
  • 1
  • 15

2 Answers2

0

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.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • sorry for the late reply. Trying using session but unfortunately i can't because this code will become localhost and some of computer has error when doing the session. Thx for the reply, that documentation really helpful for me – dooooooofai Jul 31 '19 at 03:53
0

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.

Andrei Lupuleasa
  • 2,677
  • 3
  • 14
  • 32
  • thx you for the reply, sir! trying your solution it seems working, but now i has error from echoing the result2. "You cannot call a method on a null-valued expression. At line:1 char:1 + $port.ReadExisting() ; + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull" – dooooooofai Jul 31 '19 at 04:11
  • Trying to using GetVariable on result2 and it seems the variable is missing so thats why i can't call the $port – dooooooofai Jul 31 '19 at 04:14