1

In the Main function of this script I have two if statements, each having two conditions that should be met before running. In the first if statement both conditions are met but the code is not running. Below is the code and the output. Any insight would be valuable!

$port= new-object system.io.ports.serialport com4
$port.open()
$gpi= 0
$toggle1 = 0
$toggle2 = 0

function read-com{
$port.write("gpio read 0`r")
$dump = $port.readline()
$script:gpi = $port.readline()
}
function low-com{
C:\Users\ccrcmh1djg\Videos\Reel\default.m4v
}
function high-com{
C:\Users\ccrcmh1djg\Videos\Reel\on-air.m4v
}
function main{
do{
        read-com
        sleep -m 500
        'read-com'
        $toggle1 
        $gpi
        if(($gpi -eq 0) -and ($toggle1 -eq 0)){
            $toggle1 = 1
            $toggle2 = 0
            low-com
           'low-com conditions met'
         }else{write-host 'low-com condition not met'}
         $toggle1
         $gpi
         if(($gpi -eq 1) -and ($toggle2 -eq 0)){
             $toggle1 = 0
             $toggle2 = 1
             high-com
             'high-com conditions met'
         }Else{Write-Host 'high-com condition not met'}
         'loop'

}while ($port.IsOpen)

}
main

Output:

read-com
0
0
low-com condition not met
0
0
high-com condition not met
loop

Thanks!

Dan
  • 13
  • 3
  • 2
    Maybe it is because of the indentation, but I don't see where you actually call main. – EBGreen May 10 '18 at 18:04
  • 3
    Also, this looks like a situation where a [MCVE] would help. As a matter of fact I suspect that you would solve your issue as you tried to create one. – EBGreen May 10 '18 at 18:07
  • 1
    I'd be dollars to donuts you're getting a non-visible character back from the port read into `gpi`. For example, `$foo = "1\`t"; echo $foo; echo ($foo -eq 1)` It looks like 1 but its not. – Adam May 10 '18 at 18:42
  • I think you are correct @Adam. When I manually set $GPI=1 it works. How would I standardize the variable then? – Dan May 10 '18 at 19:56
  • 1
    @Dan That's the million dollar question. Without knowing what character might be your culprit, its tough. You can fudge it with a `$foo -match 1`. You could try replacing `$foo -replace '[^\x00-\x7f]'` to expunge anything over ASCII 127. But at least you now know the variable isn't what you think. – Adam May 10 '18 at 20:23
  • @Adam I included a function in my answer which should make those characters visible. – Jacob Colvin May 10 '18 at 20:32

2 Answers2

0

$gpi might have been changed by line $script:gpi = $port.readline() in low-com function.

mpg
  • 59
  • 5
0

In this case, $port.ReadLine() likely just needs to be cleaned up a bit as per @Adam in the comments. You can use the following function written by another SO user to display any additional characters which are not visible in the console.

https://stackoverflow.com/a/45356836/4868262

Load the function, then write:

$gpi = $port.readline()
$gpi | Debug-String

Once you find the problem some character, try using -replace '' to remove it.

Jacob Colvin
  • 2,625
  • 1
  • 17
  • 36
  • `'r0` is the output from Debug-String. Nice function. I'm not familiar with `-replace`. How would you remove the extraneous `'r` characters? – Dan May 11 '18 at 00:07
  • 1
    `$gpi -replace "\`r", ''` – Adam May 11 '18 at 01:09