I am feeling like I am missing something small, but I still cannot find it:
I am doing an internship and have to solve the problem asap (almost at the end). It would be a big relieve if you could help me! Here the problem:
I am using Codesys V3.5 SP11 Patch 3+ and I am working with Codesys Control for RPi
My aim is to create a csv-file in Codesys and save it on the sd card from the RPi. To make things easier I have two programs running, one on
1) Codesys Control Win V3 and one on
2) Codesys Control for RPi.
The code is the same (very simplified), only the machine and directory are different.
The program is working on the Windows machine. It is creating the file and writing in it the information I give it in the code. On the RPi however the program is running (no Error?!), but it isn't writing a file. My thinking was that it has something to do with the directory, but after researching over one week I still haven't found the right information.
I have attached the program and the subprogram so you can have a look on it. (not all variables are used bc I shortened the program)
One of the directories I tried is: 'cd \home\pi\Downloads\test.csv' (probably totally wrong)
Thank you so much for your help!!
SpeedyG
VAR    bWrite      BOOLVAR    pEnd      POINTERTOSTRINGVAR    resultdata_csv    ARRAY[1..400000] OFBYTEVAR    handleFile    DWORDVAR    dwWritten    DWORDVAR    szSeperator    STRING(1)        ';'VAR    iCounter1    INTVAR    Copy_of_bWrite    BOOL        TRUEVAR    FilePathAndName    String        'cd \home\pi\Downloads\test.csvVAR    CRLF      STRING(2)      '$R$N'VAR    LF      String(1)      '$N'VAR    Copy_of_resultdata_csv  ARRAY[1..400000] OFBYTEVAR    big      DINTVAR    i      DINTVAR    z      DINTVAR    zahl      REALVAR    Copy_of_pEnd    POINTERTOSTRINGVAR    fopen      FILE.OpenVAR    fwrite      file.WriteVAR    fclose      file.Close(*----------------------------FillHeadData--------------------------------*)IFbWriteTHEN
  pEnd :=ADR(resultdata_csv);
  pEnd :=StrCatPtr('987654321', pEnd); Â
 Â
fopen(
  xExecute:=TRUE,
  xDone=> ,
  xBusy=> ,
  xError=> ,
  sFileName:=FilePathAndName,
  eFileMode:=0,
  xExclusive:= ,
  eError=> ,
  hFile=>handleFile);fwrite(
  xExecute:=fopen.xDone,
  xAbort:= ,
  udiTimeOut:= ,
  xDone=> ,
  xBusy=> ,
  xError=> ,
  xAborted=> ,
  hFile:=handleFile,
  pBuffer:=ADR(resultdata_csv),
  szSize:=  pEnd-ADR(resultdata_csv) ,
  eError=>);
 Â
  fclose(
  xExecute:=fwrite.xDone,
  xDone=> ,
  xBusy=> ,
  xError=> ,
  hFile:=handleFile,
  eError=>);
 Â
 Â
 Â
  IF fclose.xDone
    THEN
     bWrite:=FALSE;
   Â
    END_IFEND_IF
I try to use your code, but it work only one time. I can't to write a other line on csv: csv doesn't change. I need to clean code and reload code on Rpi: it works, but first line of csv is replace. What is the problem ?
Thank for your help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I test to open file on efilemode =3, if return error code= 5104 (file not exist), I open file in efilemode=0.
Open create well file when not exist, but when I want to write on, write return error code = HANDLE INVALID.
What is the reason ?
Thanks a lot.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi there, (English and German are alright)
I am feeling like I am missing something small, but I still cannot find it:
I am doing an internship and have to solve the problem asap (almost at the end). It would be a big relieve if you could help me! Here the problem:
I am using Codesys V3.5 SP11 Patch 3+ and I am working with Codesys Control for RPi
My aim is to create a csv-file in Codesys and save it on the sd card from the RPi. To make things easier I have two programs running, one on
1) Codesys Control Win V3 and one on
2) Codesys Control for RPi.
The code is the same (very simplified), only the machine and directory are different.
The program is working on the Windows machine. It is creating the file and writing in it the information I give it in the code. On the RPi however the program is running (no Error?!), but it isn't writing a file. My thinking was that it has something to do with the directory, but after researching over one week I still haven't found the right information.
I have attached the program and the subprogram so you can have a look on it. (not all variables are used bc I shortened the program)
One of the directories I tried is: 'cd \home\pi\Downloads\test.csv' (probably totally wrong)
Thank you so much for your help!!
SpeedyG
StrCatPtr (FUN).txt [320 Bytes]
PLC_PRG (PRG)_short.txt [1.37 KiB]
cd \home\pi\Downloads\test.csv -> /home/pi/Downloads/test.csv
Ouch, that hurts. I guess it was too obvious. -.- Thank you @johnlee.
I try to use your code, but it work only one time. I can't to write a other line on csv: csv doesn't change. I need to clean code and reload code on Rpi: it works, but first line of csv is replace. What is the problem ?
Thank for your help.
Didn't try it, but i expect two problems from the code:
1st:
xExecute is not toggled, so it is only executed once. maybe this would help:
2nd:
eFileMode:= 0,
if you want to append a line, you need to set the filemode to "append".
But this was just the first impression, on first sight.
Cheers,
Ingo
Hi,
yes, if file already exist : efilemode had to be 3 and if it doesn't exist efilemode had to be 0.
I see fopen.xExecute...
I do:
I test to open file on efilemode =3, if return error code= 5104 (file not exist), I open file in efilemode=0.
Open create well file when not exist, but when I want to write on, write return error code = HANDLE INVALID.
What is the reason ?
Thanks a lot.
the handle is invalid, because your program write nothing to Hfile in this case 😉
Last edit: dkugler 2021-06-03
I make some changes but don't work more:
i change a little bit and tested it with winV3
PROGRAM POU_RPi
VAR
bWrite: BOOL;
pEnd: POINTER TO STRING;
resultdata_csv: ARRAY[1..400000] OF BYTE;
Hfile: DWORD;
//szSeperator: STRING(1):=';';
FilePathAndName: STRING:= 'c:/temp/test.csv';
CRLF: STRING(2):='$R$N';
LF: STRING(1):= '$N';
fopencreate : FILE.Open;
fopen : FILE.Open;
fwrite: file.Write;
fclose: file.Close;
test: INT :=1;
NumAction: INT;
code_erreur: INT;
NumActTrig : R_TRIG;
END_VAR
( ---------------------------- Ecrire les data dans le CSV-------------------------------- )
fopencreate(
xExecute:= (NumAction=100),
xDone=> ,
xBusy=> ,
xError=> ,
sFileName:= FilePathAndName,
eFileMode:= FILE.MODE.MWRITE,
xExclusive:= ,
eError=> ,
hFile=> );
fopen(
xExecute:= (NumAction=1),
xDone=> ,
xBusy=> ,
xError=> ,
sFileName:= FilePathAndName,
eFileMode:= FILE.MODE.MAPPD,
xExclusive:= ,
eError=> ,
hFile=> );
fwrite(
xExecute:= (NumAction=3),
xAbort:= ,
udiTimeOut:= 10000,
xDone=> ,
xBusy=> ,
xError=> ,
xAborted=> ,
hFile:= Hfile,
pBuffer:= ADR(resultdata_csv),
szSize:= pEnd - ADR(resultdata_csv),
eError=> );
fclose(
xExecute:= (NumAction=5),
xDone=> ,
xBusy=> ,
xError=> ,
hFile:= Hfile,
eError=> );
It work fine.
Thank you very much for your help.