System_open-Baustein
Kurz-Information
Name | System_open |
---|---|
→POE-Typ | →Funktion |
Kategorie | Weitere Systembausteine, Bausteine zum Aufrufen von Systemfunktionen, Baustein mit interner Fehlerdiagnose |
Grafische Schnittstelle | |
Verfügbar ab |
|
Funktionalität
Der Baustein bereitet eine Datei, ein Gerät oder eine Gerätedatei vor. Diese werden durch nachfolgende Systemaufrufe verwendet, z.B. durch System_ioctl-Baustein, System_read-Baustein oder System_write-Baustein.
Vorausgesetzte Kenntnisse
Für die effektive Verwendung dieses Bausteins müssen Sie mit der Funktionsweise des zugrunde liegenden Systemaufrufs des Betriebsystems vertraut sein, das auf der SPS verwendet wird.
Eingänge, Ausgänge, Ergebniswert
Bezeichner | →Datentyp | Beschreibung | |
---|---|---|---|
Eingänge: | pathname | STRING | Zeichenfolge, die die Ressource repräsentiert |
flags | DINT | Zugriffsmodus und/oder Erstellungsoptionen Mögliche Werte für Windows finden Sie unter: http://www.delorie.com/djgpp/doc/incs/fcntl.h Sie können die Werte durch den | |
mode | DINT | Dateisystem-Berechtigungen (optional; POSIX-Style) Mögliche Werte für Windows finden Sie unter: https://www.rpi.edu/dept/cis/software/g77-mingw32/include/sys/stat.h | |
Ausgänge: | rc | DINT | Ergebniscode:
|
errno | DINT | der Fehlercode, der vom Systemaufruf geliefert wird Windows-Standardwerte (für die integrierte SPS gültig): siehe https://msdn.microsoft.com/en-us/library/t3ayayh1.aspx | |
Ergebniswert: | – | DINT | liefert den Datei-Deskriptor im Erfolgsfall |
Der Eingang EN
und der Ausgang ENO
sind für den →Aufruf des Bausteins verfügbar. Siehe "Ausführungssteuerung: EN, ENO" für Informationen zum Eingang EN
und zum Ausgang ENO
.
Interne Fehlerdiagnose für Baustein
Der Baustein prüft die folgenden Fehlerfälle:
- Der Systemdienst wurde nicht geladen.
- Der Ergebniswert des Bausteins ist ein negativer Wert.
- Der Ergebniscode des Funktionsaufrufs entspricht einem Fehler (Ausgang
rc
≠ Wert0
).
In einem solchen Fehlerfall wird der Ausgang ENO
des Bausteins auf den Wert FALSE
(oder eine Entsprechung) gesetzt.
Beispiel für Verwendung im ST-Editor
FUNCTION_BLOCK ExampleSystemOpen VAR data : ARRAY[0..63] OF BYTE; fd : DINT := -1; flagsWriteAppend : DINT := -1; flagsCreateTruncWrite : DINT := -1; mode : DINT := -1; path : STRING[64]; didOpen : BOOL; writeOk : BOOL; END_VAR VAR CONSTANT /* Append new information to the end of the file. */ Windows_O_APPEND : DINT := 8; /* If the file does not exist, create it. If the O_CREAT option is used, then you must include the third parameter (). */ Windows_O_CREAT : DINT := 256; /* Combined with the O_CREAT option, it ensures that the caller must create the file. If the file already exists, the call will fail. */ Windows_O_EXCL : DINT := 1024; /* Open the file so that it is read only. */ Windows_O_RDONLY : DINT := 0; /* Open the file so that it can be read from and written to. */ Windows_O_RDWR : DINT := 2; /* Initially clear all data from the file. */ Windows_O_TRUNC : DINT := 512; /* Open the file so that it is write only. */ Windows_O_WRONLY : DINT := 1; /* Only reading permitted. */ Windows_S_IREAD : DINT := 256; /* Writing permitted. (In effect, permits reading and writing.) */ Windows_S_IWRITE : DINT := 128; /* Append new information to the end of the file. */ Linux_O_APPEND : DINT := 8; /* If the file does not exist, create it. If the O_CREAT option is used, then you must include the third parameter (mode). */ Linux_O_CREAT : DINT := 512; /* Combined with the O_CREAT option, it ensures that the caller must create the file. If the file already exists, the call will fail. */ Linux_O_EXCL : DINT := 2048; /* Open the file so that it is read only. */ Linux_O_RDONLY : DINT := 0; /* Open the file so that it can be read from and written to. */ Linux_O_RDWR : DINT := 2; /* Initially clear all data from the file. */ Linux_O_TRUNC : DINT := 1024; /* Open the file so that it is write only. */ Linux_O_WRONLY : DINT := 1; END_VAR IF isTargetWinX86() THEN path := 'c:\tmp\log.dat'; /* write access, append to file content */ flagsWriteAppend := IOR(Windows_O_WRONLY, Windows_O_APPEND); /* write access, create file and truncate file to length 0 */ flagsCreateTruncWrite := IOR(Windows_O_WRONLY, Windows_O_CREAT, Windows_O_TRUNC); /* read and write access for the user */ mode := IOR(Windows_S_IREAD, Windows_S_IWRITE); END_IF; IF isTargetLinuxX86() THEN path := '/tmp/log.dat'; /* write access, append to file content */ flagsWriteAppend := IOR(Linux_O_WRONLY, Linux_O_APPEND); /* write access, create file and truncate file to length 0 */ flagsCreateTruncWrite := IOR(Linux_O_WRONLY, Linux_O_CREAT, Linux_O_TRUNC); /* read and write access for the user, read access for the group: rw-r----- */ mode := 2#110100000; END_IF; fd := System_open(pathname := path, flags := flagsWriteAppend, ENO => didOpen); /* file does not exist yet */ IF NOT(didOpen) THEN fd := System_open(pathname := path, flags := flagsCreateTruncWrite, mode := mode, ENO => didOpen); Assert(fd >= 0); END_IF; IF didOpen THEN /* modify values of data array */ data[0] := 16#48; data[1] := 16#69; System_write(fd := fd, count := 2, data := data, ENO => writeOk); Assert(writeOk); System_close(fd := fd); System_delete(pathname := path); END_IF; END_FUNCTION_BLOCK
Bei der Erstellung Ihrer Anwendung im ST-Editor erstellen Sie den Aufruf eines Bausteins, indem Sie den laut Syntax erforderlichen Text eintippen oder die Inhaltshilfe verwenden.