The two byte values can be read from the file as demonstrated by the following code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileName=%TEMP%\%~n0.tmp"
(
echo [0xA1 rr]
echo I2C START BIT
echo WRITE: 0xA1 ACK
echo READ: 0x61
echo READ: ACK 0xA8
echo NACK
echo I2C STOP BIT
echo I2C^>
echo I2C^>
)>"%FileName%"
set "Byte01="
set "Byte02="
for /F "usebackq skip=3 tokens=2,3" %%I in ("%FileName%") do if not defined Byte01 (set /A "Byte01=%%I") else (set /A "Byte02=%%J" & goto Output)
:Output
if defined Byte01 (
echo The byte values are:
echo(
set Byte
)
del "%FileName%"
endlocal
The command FOR with the used options skips the first three lines of the file.
The fourth line is first split up into substrings using the default string delimiters normal space and horizontal tab. The second substring is 0x61
which is assigned to the specified loop variable I
. There is no third substring on fourth line.
The IF condition checks if the environment variable Byte01
explicitly undefined above the FOR loop is still not defined in which case the command SET is used to evaluate an arithmetic expression which interprets the string value 0x61
assigned to the loop variable I
as hexadecimal value and assigns this value in decimal to the environment variable Byte01
.
Then FOR processes the fifth line with splitting the line up once again into substrings using space/tab as delimiters and assigns the second substring (token) ACK
to loop variable I
and the third substring 0xA8
to next but one loop variable J
according to the ASCII table.
There is executed next once again the IF condition, but this time Byte01
is already defined resulting in continuing batch file processing on ELSE command block with command SET used to evaluate the arithmetic expression to define the environment variable Byte02
with the hexadecimal value assigned to loop variable J
converted to a decimal value AND command GOTO to continue processing of the batch file on the line below the label Output
. That results in exiting the loop before it processes more lines from the file.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
... explains %~n0
(name of argument 0 which is the batch file name)
del /?
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?
See also: