Hello everyone.
please i wanna ask you about how to create, open and write in a file. txt or .csv.
Notice : My Pc had a good connection whith the plc via FTP protocol, but i just need an example of code so as to create, open and write in a file. txt or .csv.
You just need to drag an drop a file from the left side (e.g. a txt file) to the right side or vice versa. FTP is a file transfer protocol.
Hello Yacine,
If you would like to do file handling on the PLC side you can have a look at the FileIO library:
It also contains examples of how to do this is Structured text.
Regards,
Tobias
Hello Tobias,
i wanna create a file in the plc so as to transfer it to my pc.
i have this code
//open file
fbOpen(
enable := TRUE,
pDevice := ADR('D:'),
pFile := ADR('data.txt'),
mode := FileIO.fiWRITE OR FileIO.fiCREATE,
pHandle := ADR(fileHandle),
status => statusOpen
);
// Préparer la ligne à écrire
buffer := REAL_TO_STRING(Vibration_physique) + ';' + REAL_TO_STRING(t) + CHAR(13) + CHAR(10);
// Écrire la ligne
fbWrite(
enable := fbOpen.done,
handle := fileHandle,
pSrc := ADR(buffer),
len := LEN(buffer),
pBytesWritten := ADR(bytesWritten),
status => statusWrite
);
// Fermer le fichier
fbClose(
enable := fbWrite.done,
handle := fileHandle,
status => statusClose
);
and i have those errors
yes but i don’t create a file in the plc and that what i want to do create and write in the file in plc and then transfer the file to my pc so as to use it in a python programme
I think you have forgotten to import the correct library.
If you press the 3 dots when in the type field you should get a window like this.
When you select the FileOpen here it will automatically import the library
Also i can there is no done output for the FileOpen. It has a status. This can be seen when you open the help on the FileOpen function block
Okay great. Then i think you need to have a look at what inputs and outputs the actual function blocks hav.
You could look at the example provided in the help:
thank you for your support but i didn’t inderstand the code in B&R online help and it seems like there are a lot of variables that we should declare.
it’s really complecated for me
You are welcome.
If you are new to programming in Structured text i recommend contacting your local B&R office for some training, looking at more examples in the help or finding some guides on youtube.
If you have any specific things you do not understand please ask
thank you so much tobias i really appreciate your help.
I haven’t found any useful resources on YouTube to help me with my situation. Since I’m currently doing an internship, I don’t have the budget to purchase a course or consult a professional.
anyway thank you all. if i have other questions i will tell you.
Hello @Yacine ,
your code has multiple issues, some of which the compiler is already telling you. Some of these errors can be easily solved by utilizing the autocomplete features of AS → by typing “fbWrite.”, the popup will automatically show you it’s members, and so does “fbOpen.” do.
- The FBs both do not have any “done” outputs, which the AS help also shows.
You can check the status by checking the fub’s “.status” output for ERR_OK (then it’s done) or anything unlike “ERR_FUB_BUSY”, then it’s an error. - Also, please don’t connect the output of one to the input of another FUB, this is clearly code that’s supposed to be run one-after-another, so a state-machine is the proper way to do this.
- I also do not know where you got the “pBytesWritten” member from, it’s neither part of FileWrite nor FileWriteEx.
- “status=>statusWrite” is also invalid structured text.
The fiWRITE and fiCREATE constants also don’t need nor must have any prefix of “FileIO.”
That all being said, AI is known to hallucinate various things and this pretty much looks like one gave you very wrong code. If you wrote it yourself, please see the FUB’s documentation and don’t use any in- or outputs that are not explicitly mentioned.
Best regards
could you help me to correcte the code because i’m a newbe in the Structured text programme.
and thank your for your support Michael
Hi there,
sorry but we can not provide a replacement for learning the things first, in that case a whole programming language.
AS includes some examples and FileIO is among them giving you working code. You can try reading, understanding and then running that code first and then go on to modify it to do what you actually want to do.
If you aren’t comfortable learning ST or lacking the time for, maybe you already know any of the other languages that AS supports. That will still require learning how to work with AR / cyclic code, reading the help and other things.
All of that is not a task for a few days, unfortunately, but takes time.
Best regards
Agree with @michael_w, without understanding basic principle of structure text, Automation Studio and FileIO library it will be very difficult for you to solve it. Another option would be to ask someone there to provide you functional task, and buy him a coffee or something. So do not ask to fix something what was, very likely, generated by AI.
I have tried another time but without any result i don’t know where is the problem.
please help me:
my code :
PROGRAM _INIT
t := 0;
Vibration_physique := 0;
END_PROGRAM
PROGRAM _CYCLIC
(* Vibration :=
SHL(DINT#16#000000FF AND measure4, 24) OR
SHL(DINT#16#000000FF AND measure3, 16) OR
SHL(DINT#16#000000FF AND measure2, 8) OR
(DINT#16#000000FF AND measure1);*)
(*Vibration_physique := DINT_TO_REAL(Vibration)* (10 / 8388607)/ 0.0001;*)
(*BUTTON_AUTOMATE := Channel1;
IF BUTTON_AUTOMATE THEN
LED_AUTOMATE := TRUE; // Allume la LED si le bouton est appuyé
ELSE
LED_AUTOMATE := FALSE; // Éteint la LED sinon
END_IF;
// Écriture sur la sortie
Channel2 := LED_AUTOMATE;*)
t := t + 0.001;
IF t > 1000.0 THEN // Pour éviter un débordement
t := 0.0;
END_IF;
// Calcul du signal sinusoĂŻdal
Vibration_physique := SIN(2 * 3.14159 *100000* t)*SIN(2 * 3.14159 *100* t)-SIN(2 * 3.14159 *5000* t);
(*IF count < 5001 THEN
signal[count] := Vibration_physique;
time[count] :=t;
count := count + 1;
ELSE
status := FALSE;
count := 0;
END_IF*)
// Programme cyclique
// Conversion de la valeur à écrire (avant l'étape d'écriture)
// Conversion de la valeur à écrire (avant l'étape d'écriture)
(* Initialize variables *)
CASE step OF
0: // Créer le fichier
fbOpen.enable := TRUE;
fbOpen.pDevice := ADR(deviceName);
fbOpen.pFile := ADR(fileName);
fbOpen.mode := 3; // Lecture/écriture (vérifie la valeur dans ta doc)
fbOpen();
IF fbOpen.status = 0 THEN
hFile := fbOpen.ident; // Récupérer le handle
fbOpen.enable := FALSE;
step := 1;
ELSIF fbOpen.status <> 0 AND fbOpen.status <> 65535 THEN
status := fbOpen.status;
fbOpen.enable := FALSE;
step := -1; // Erreur
END_IF
1: // Écriture
fbWrite.enable := TRUE;
fbWrite.ident := hFile;
fbWrite.pSrc := ADR(writeBuffer);
fbWrite.len := SIZEOF(writeBuffer);
fbWrite.offset := 0;
fbWrite();
IF fbWrite.status = 0 THEN
fbWrite.enable := FALSE;
step := 2;
ELSIF fbWrite.status <> 0 THEN
status := fbWrite.status;
step := -1;
END_IF
2: // Fermer
fbClose.enable := TRUE;
fbClose.ident := hFile;
fbClose();
IF fbClose.status = 0 THEN
fbClose.enable := FALSE;
step := 3;
ELSIF fbClose.status <> 0 THEN
status := fbClose.status;
step := -1;
END_IF
-1: // Erreur
// status contient l’erreur
3: // Terminé avec succès
;
END_CASE
END_PROGRAM
PROGRAM _EXIT
END_PROGRAM
Variables :
VAR
Channel1 : BOOL;
Channel2 : BOOL;
measure1 : USINT;
measure2 : USINT;
measure3 : USINT;
measure4 : USINT;
Vibration_physique : REAL;
t : REAL; (*variable read or write in file *)
fbOreate : FileOpen;
fbWrite : FileWrite;
fbClose : FileClose;
fbOpen : FileOpen;
hFile : UDINT := 0;
deviceName : WSTRING[80] :="D:/";
fileName : STRING[255] := 'data.txt';
writeBuffer : STRING[255] := 'Bonjour depuis FileIO !';
bytesWritten : DINT := 0;
step : INT := 0;
status : DINT := 0;
END_VAR
Edit by michael_w: Formatted code properly
Hi @Yacine
First of all you need to understand that the FTP server is pointing to a specific partition of the PLC, in your hardware configuration you should see the config for FTP Server:
It’s from AS6 but should be pretty much the same on AS4.
So the FTP Server pointing to the USER partition of the PLC.
The FileIO library required a deviceName to acces the partition, so in the hardware configuration you have a section “File devices” and you can configure a couple “Name” and “Path”. The FileIO require the pDevice to be a pointer to a STRING variable that contains the FileDevice name you have define in the hardware configuration.
So first of all you need to check the hardware configuration of the PLC.
Next check your variable declaration and the default value you had set in.
Then retry.
Note that in your code you only use FileOpen FBK that can open an existing file, it will not create it:
The FBK should return error that he cannot found the file.
So based on this error you can use FileCreate FBK to create the file if he didn’t exist.
Hope it’s more clear.
Note 1: Based on the code and your sceenshot your code goes in error step (-1) and the FileOpen as an status of 20707, you can just type the status code in the help to know what is it, in the case you use an illegal mode (B&R Online Help)
Note 2: As other people answer I also recommend to learn bases of the ST language and the B&R environnement
Hope it help.
Regards,
Florent
thank you for your support.
well i do the same like what i found in the [B&R online help](https://B&R online help) but it gives me to errors 20798 and thats mean that the directory is not created. i don’t know how this can be resolved.
this my config for the filedevice and the ftp server.

The variable “deviceName” is the name of the File device you have declared.
In your case it’s “Floppy” and the variable type is not WSTRING it’s STRING.
Here is the correction :
deviceName : STRING[80] := "Floppy";