Skip to content

Instantly share code, notes, and snippets.

@nikvoronin
Last active January 3, 2021 21:53
Show Gist options
  • Save nikvoronin/e238b36e18d2db87a04d50dc9ba67879 to your computer and use it in GitHub Desktop.
Save nikvoronin/e238b36e18d2db87a04d50dc9ba67879 to your computer and use it in GitHub Desktop.
TC/BSD: Read /dev/urandom by regular file POUs then write result to the file situated at the remote PLC
PROGRAM MAIN
VAR
state: INT;
buf: byte;
tmpStr: STRING;
hDev, hResFile: UINT;
RUN: BOOL;
Open: FB_FileOpen := (ePath := PATH_GENERIC);
Read: FB_FileRead := (sNetId := '');
Write: FB_FileWrite := (sNetId := sRemoteNetId);
Close: FB_FileClose;
END_VAR
VAR CONSTANT
sDevPath: STRING := '/dev/urandom';
sResPath: STRING := 'c:\dev\urandom.txt';
sRemoteNetId: STRING := '172.17.176.49.1.1';
END_VAR
CASE state OF
0:
IF RUN THEN
Open (bExecute := FALSE);
Write(bExecute := FALSE);
Read (bExecute := FALSE);
Close(bExecute := FALSE);
state := 100;
END_IF
100:
Open(
bExecute := TRUE,
sNetId := '',
nMode := FOPEN_MODEREAD OR FOPEN_MODEBINARY,
sPathName := sDevPath );
IF NOT Open.bBusy THEN
hDev := Open.hFile;
Open(bExecute := false);
state := 110;
END_IF
110:
Open(
bExecute := TRUE,
sNetId := sRemoteNetId,
nMode := FOPEN_MODEWRITE OR FOPEN_MODEBINARY,
sPathName := sResPath );
IF NOT Open.bBusy THEN
hResFile := Open.hFile;
Open(bExecute := false);
state := 200;
END_IF
200:
Read(
bExecute := TRUE,
hFile := hDev,
pReadBuff := ADR(buf),
cbReadLen := SIZEOF(buf) );
IF NOT Read.bBusy THEN
Read(bExecute := false);
state := SEL(Read.cbRead < 1, INT#210, INT#300);
END_IF
210:
tmpStr := CONCAT(BYTE_TO_STRING(buf), '$r$n');
Write(
bExecute := TRUE,
hFile := hResFile,
pWriteBuff := ADR(tmpStr),
cbWriteLen := INT_TO_UDINT(LEN(tmpStr)) );
IF NOT Write.bBusy THEN
Write(bExecute := FALSE);
state := SEL(RUN, INT#300, INT#200);
END_IF
300:
Close(
bExecute := TRUE,
sNetId := '',
hFile := hDev );
IF NOT Close.bBusy THEN
Close(bExecute := FALSE);
state := 310;
END_IF
310:
Close(
bExecute := TRUE,
sNetId := sRemoteNetId,
hFile := hResFile );
IF NOT Close.bBusy THEN
Close(bExecute := FALSE);
RUN := FALSE;
state := 0;
END_IF
END_CASE
END_PROGRAM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment