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?
For sure you must use LEN(). SIZEOF() means you write a number of bytes based on the size of the variable type. For your STRING this will be 80 characters, but this will write unexpected characters in your file.
π
1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Tim is right about the size. SIZEOF gives you the declared size of the string, so you end up writing the whole buffer, including the unused bytes after the terminating zero, and that is where those odd characters in the CSV come from. LEN is the right value for szSize.
The empty file with LEN usually caught me out for a different reason, though. In my case the write was firing before the string was actually built, or the execute input stayed TRUE so the block never saw a new rising edge. I ended up sequencing it strictly: open the file and wait for its done flag, then build s_Palavra, then trigger the write once, wait for its done flag, and only then close the file. If the close runs in the same scan as the write, you can lose the data too.
What helped me debug it was watching s_Palavra and LEN(s_Palavra) side by side with the write block's done and error flags in simulation, and checking that the file handle was valid before the write ran. If LEN shows 0 at the moment of the write, the string just is not ready yet, even if it looks right a scan later. And if you are writing line by line, make sure each line ends with a line break, otherwise the next row just runs onto the same line in the CSV.
Try that order first and see what the error flag says, that usually points straight at whether it is the size, the timing, or the file handle. I would try it in simulation, or on a spare controller, before running it on a live machine, and keep file handling well away from anything safety related.
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 ...
For sure you must use LEN(). SIZEOF() means you write a number of bytes based on the size of the variable type. For your STRING this will be 80 characters, but this will write unexpected characters in your file.
Tim is right about the size. SIZEOF gives you the declared size of the string, so you end up writing the whole buffer, including the unused bytes after the terminating zero, and that is where those odd characters in the CSV come from. LEN is the right value for szSize.
The empty file with LEN usually caught me out for a different reason, though. In my case the write was firing before the string was actually built, or the execute input stayed TRUE so the block never saw a new rising edge. I ended up sequencing it strictly: open the file and wait for its done flag, then build s_Palavra, then trigger the write once, wait for its done flag, and only then close the file. If the close runs in the same scan as the write, you can lose the data too.
What helped me debug it was watching s_Palavra and LEN(s_Palavra) side by side with the write block's done and error flags in simulation, and checking that the file handle was valid before the write ran. If LEN shows 0 at the moment of the write, the string just is not ready yet, even if it looks right a scan later. And if you are writing line by line, make sure each line ends with a line break, otherwise the next row just runs onto the same line in the CSV.
Try that order first and see what the error flag says, that usually points straight at whether it is the size, the timing, or the file handle. I would try it in simulation, or on a spare controller, before running it on a live machine, and keep file handling well away from anything safety related.