串行端口的管道输出以cmd中的for循环卷曲

我想获取一个串行端口的输出。在它上面以UTF-8发送了一条消息。

现在我需要将通过串口收到的该消息发送到带有CURL.exe的HTTP-API

我首先尝试使用“ com5型”在cmd中打开端口。一切正常,并且打开并监视了端口。我在cmd中看到了该消息,当我使用“键入com5:>> output.txt”时,该消息已写入文件中。

之后,我尝试将“ com5类型:”-Command放入cmd的for循环中,以便在收到带有CURL的API时发送消息。

我使用的代码:

cd C:\comcon\
for /F %%i in ('type com5:') do set message=%%i & curl.exe http://sample.com/api.php?message=%message%

使用此代码无效。通过串行端口接收到的消息不会在for循环内处理,而且看起来甚至没有触发循环。

任何人都有一个想法或其他解决方案来监视串行端口中是否有新消息,并通过调用带有参数的url将这些消息发送到API。

非常感谢您!

lpf3288547 回答:串行端口的管道输出以cmd中的for循环卷曲

我会使用两个线程。
一种用于从Com-port接收消息,另一种用于处理它。
这样可以避免阻塞FOR /F。 但是您将不得不杀死第一个线程,因为它无法自动停止。

@echo off
REM *** Trampoline jump for function calls of the form ex. "C:\:function:\..\<batch>.bat"
FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L

break > async.tmp

start "" /b %~d0/:thread_read:/../%~pnx0

goto :process
exit /b

:thread_read
echo STARTING
type com5_ > async.tmp
echo END THREAD
exit

:process
setlocal EnableDelayedExpansion
call :_process  < async.tmp
exit /b

:_process
set "line="
set /p line=
if not defined line (
    goto :_process
)

echo ## !line!
本文链接:https://www.f2er.com/3127484.html

大家都在问