2016-03-28 77 views
1

我的GPS設備在COM端口上發送NMEA的信息。 我想提取時間信息並設置系統時間。帶GPS的批處理文件設置系統時間

在批處理我寫道:

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

是'複製COM2:CON:'連續流呢? – JosefZ

回答

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 

這臺第一線的時間將文件格式爲hh變量COMTIME在:MM:SS

注意

這將刪除.279部分時間,以保持零件更改delims=,.delims=,

+1

感謝Denis,由於GPS設備連續發送,您的'for循環'掛起。它從來沒有來到休息。 ;-( –

0

下一段代碼將從無盡的命令輸出中獲取行。

用無盡的測試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