Last active
November 10, 2023 18:41
-
-
Save SmileYzn/617dfdb70a920eb553835b085eb27783 to your computer and use it in GitHub Desktop.
Ler dados do TCP/IP e enviar a um webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leitor da Balanca Capital TCP/IP (192.168.92.93:8080 (Soja), 192.168.92.94:8081 (Farelo) e 192.168.92.95:8080 (Oleo Degomado)) | |
param([string] $endereco = "127.0.0.1", [int] $porta = 8082, [string] $url = "https://dominio.br/balanca.php") | |
try | |
{ | |
$produto = "Balanca Desconhecida" | |
switch($endereco) | |
{ | |
"192.168.92.93" | |
{ | |
$produto = "Balanca de Soja" | |
} | |
"192.168.92.94" | |
{ | |
$produto = "Balanca de Farelo de Soja" | |
} | |
"192.168.92.95" | |
{ | |
$produto = "Balanca de Oleo Degomado" | |
} | |
} | |
$Host.UI.RawUI.WindowTitle = $produto | |
Write-Host "Conectando a $produto $endereco : $porta -> " -NoNewLine | |
try | |
{ | |
$socket = New-Object System.Net.Sockets.TcpClient($endereco, $porta) | |
Write-Host -ForegroundColor Green "Conectado com sucesso" | |
} | |
catch | |
{ | |
Write-Host -ForegroundColor Red "Falha ao conectar." | |
start-sleep -m 3000 | |
exit -1 | |
} | |
$stream = $socket.GetStream() | |
$writer = New-Object System.IO.StreamWriter($stream) | |
$buffer = New-Object System.Byte[] 1024 | |
$encoding = New-Object System.Text.AsciiEncoding | |
while($socket.Connected) | |
{ | |
while($stream.DataAvailable) | |
{ | |
$read = $stream.Read($buffer, 0, 1024) | |
if($read) | |
{ | |
$texto = $encoding.GetString($buffer, 0, $read) | |
if($texto) | |
{ | |
Write-Host ($texto.Trim()) | |
Invoke-WebRequest -Uri $url -TimeoutSec 9999 -Method POST -Body @{texto=$texto.Trim();ip=$endereco;porta=$porta} | Out-Null | |
} | |
} | |
} | |
$writer.WriteLine("Ping Pong: $produto") | |
$writer.Flush() | |
start-sleep -m 1000 | |
} | |
} | |
catch | |
{ | |
Write-Host -ForegroundColor Red "$produto finalizada" | |
} | |
finally | |
{ | |
if($writer) | |
{ | |
$writer.Close() | |
} | |
if($stream) | |
{ | |
$stream.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment