我的GPS设备在COM端口上发送NMEA信息。 我想提取时间信息并设置系统时间。
在一个批次中我写道:
type COM2 | find "GPRMC"
带给我所需的信息 - 但不断。 示例(前3行):
$GPRMC,100211.279,V,4816.1496,N,01623.0965,E,0.00,0.00,280316,,,N*7A
$GPRMC,100212.279,V,4816.1496,N,01623.0965,E,0.00,0.00,280316,,,N*79
$GPRMC,100213.279,V,4816.1496,N,01623.0965,E,0.00,0.00,280316,,,N*78
因此,下一个批处理命令永远不会执行,因为GPS设备只要打开就会发送。
我想提取第二个字段,例如,它是100211,这是UTC时间10:02:11。
问候
答案 0 :(得分:1)
您可以使用:
@echo off
for /f "tokens=2 delims=,." %%a in ('type COM2 ^| find "GPRMC"') do (
set "comTime=%%a"
goto :break
)
:break
set "comTime=%comTime:~0,2%:%comTime:~2,2%:%comTime:~4,2%
echo %comTime%
pause
这会将文件中第一行的时间设置为变量comTime,格式为hh:mm:ss
请注意
这会删除.279部分时间,以保持该部分delims=,.
更改为delims=,
答案 1 :(得分:0)
下一个代码片段将从无尽的命令输出中获取行。
|
管道移至find
或findstr
(read more, see Pipes and CMD.exe paragraph)。timeout
命令,cmd
,taskkill
,for
)。 使用无尽的ping localhost -4 -t
:
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "mycommand=type COM2"
set "mytestval=$GPRMC"
rem next two lines are merely for my tests: remove them both
set "mycommand=ping localhost -4 -t"
set "mytestval=Reply"
start "bubu36260096" cmd /C ^>"%temp%\aux36260096.txt" %mycommand%
>NUL timeout /T 3 /NOBREAK
>NUL taskkill /T /F /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq bubu36260096"
>NUL timeout /T 1 /NOBREAK
rem v--- skip as many lines as you need
for /f "usebackq skip=1 tokens=1,2 delims=, " %%a in ("%temp%\aux36260096.txt") do (
rem ^--- remove this blank space in above line
if "%%a"=="%mytestval%" (
set "comTime=%%b"
goto :break
)
)
:break
rem next line for debugging purposes only
type "%temp%\aux36260096.txt"
rem set "comTime=%comTime:~0,2%:%comTime:~2,2%:%comTime:~4,2%
echo comTime ... "%comTime%"
pause