I am trying to create a routine in CODESYS to export a report to a CSV file using the CAA.File library. The file is created successfully, but I am getting some unexpected/unknown characters in the generated CSV.
The problem seems to be related to the SIZEOF() function. I am using it for the szSize parameter of File.Write, as recommended in the CODESYS documentation. However, when I use SIZEOF(), the CSV contains extra characters and parts of previous strings.
I also tried using LEN(), but in my application the file is created empty, even though the string and the calculated length appear to be correct.
Note: The code I am attaching does not include the execution/sequence management of the function blocks. I have omitted this part to keep the example focused on the CSV writing routine. The function blocks are triggered and managed by a separate state machine.
I am not sure what the correct approach is in this case, especially since the official documentation recommends using SIZEOF() for szSize.
I will attach my code and a screenshot of the generated CSV showing the issue.
Has anyone experienced something similar or could suggest the correct way to write a changing STRING to a CSV file using CAA.File.Write?
Hi clesio, I have hit the same thing before, and SIZEOF really is the part to look at first. For a STRING, SIZEOF gives you the declared size of the variable, not the length of the text in it right now, so if sLine is declared as STRING(255) you end up writing the whole 256-byte buffer, including the unused bytes after the terminating zero. Those extra bytes are what show up as the strange characters at the end of your CSV, especially when you open it in Excel.
LEN is the right idea, so the empty file with LEN usually points at the sequencing around the block rather than at LEN itself. FILE.Write is asynchronous, it does not finish in the cycle you trigger it, so I would keep xExecute TRUE until xDone comes back, and only call Close after that. If the Close runs in the same cycle, or the trigger is only a one-cycle pulse, the write can get cut off before anything lands in the file. It is also worth checking that sLine is already fully built, with its line ending, before the Write is triggered, and watching xError, eError and how many bytes were actually written, because that tells you quickly whether the block ran at all or ran with a zero length.
The pattern that worked for me was something like this:
with bWrite staying TRUE until fbWrite.xDone, then resetting it and moving on to Close. Try that first on a bench setup or in simulation with a throwaway file, before using it to write reports on a live machine, and keep the safety logic separate from file handling.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello everyone,
I am trying to create a routine in CODESYS to export a report to a CSV file using the CAA.File library. The file is created successfully, but I am getting some unexpected/unknown characters in the generated CSV.
The problem seems to be related to the SIZEOF() function. I am using it for the szSize parameter of File.Write, as recommended in the CODESYS documentation. However, when I use SIZEOF(), the CSV contains extra characters and parts of previous strings.
I also tried using LEN(), but in my application the file is created empty, even though the string and the calculated length appear to be correct.
Here is the relevant part of my code:
Note: The code I am attaching does not include the execution/sequence management of the function blocks. I have omitted this part to keep the example focused on the CSV writing routine. The function blocks are triggered and managed by a separate state machine.
I am not sure what the correct approach is in this case, especially since the official documentation recommends using SIZEOF() for szSize.
I will attach my code and a screenshot of the generated CSV showing the issue.
Has anyone experienced something similar or could suggest the correct way to write a changing STRING to a CSV file using CAA.File.Write?
more posts ...
Hi clesio, I have hit the same thing before, and SIZEOF really is the part to look at first. For a STRING, SIZEOF gives you the declared size of the variable, not the length of the text in it right now, so if sLine is declared as STRING(255) you end up writing the whole 256-byte buffer, including the unused bytes after the terminating zero. Those extra bytes are what show up as the strange characters at the end of your CSV, especially when you open it in Excel.
LEN is the right idea, so the empty file with LEN usually points at the sequencing around the block rather than at LEN itself. FILE.Write is asynchronous, it does not finish in the cycle you trigger it, so I would keep xExecute TRUE until xDone comes back, and only call Close after that. If the Close runs in the same cycle, or the trigger is only a one-cycle pulse, the write can get cut off before anything lands in the file. It is also worth checking that sLine is already fully built, with its line ending, before the Write is triggered, and watching xError, eError and how many bytes were actually written, because that tells you quickly whether the block ran at all or ran with a zero length.
The pattern that worked for me was something like this:
fbWrite(xExecute := bWrite, hFile := hFile, pBuffer := ADR(sLine), szSize := TO_UDINT(LEN(sLine)));
with bWrite staying TRUE until fbWrite.xDone, then resetting it and moving on to Close. Try that first on a bench setup or in simulation with a throwaway file, before using it to write reports on a live machine, and keep the safety logic separate from file handling.