Sign in to follow this  
Followers 0
vinny

TWINCAT Structure to CSV

4 posts in this topic

Hi All I have a Structure consisting of different datatypes and want to read/write values for this struct using a csv. i tried using the CSV_Examples, but I am not able to get my around to using a structure to write the csv rather than an array of values. Thank you for the help

Share this post


Link to post
Share on other sites
Hello vinny, You should easily do this by following the example in this link: Beckhoff help system link Just in case you can’t, could you explain in detail which are your problems? Good luck!
1 person likes this

Share this post


Link to post
Share on other sites
Hi Thanks for the reply In the example given, in P_TextModeWrite, Step 3 (attached below), the array is converted to a string and is incremented to next value based on index. However when I use a Structure, there isnt a fixed increment size (different data types = different sizes), which is where I have hit a wall. I have attached the Struct as an export too, and am trying to create a csv file which holds the var name with its value and read back the values to fill the structure (* Convert one PLC record to CSV format *) sCSVLine := ''; fbWriter.eCmd := eEnumCmd_First;(* Write first field value *) IF nRow <= MAX_CSV_ROWS THEN FOR nColumn := 0 TO MAX_CSV_COLUMNS BY 1 DO sCSVField := STRING_TO_CSVFIELD( database[ nRow, nColumn ], FALSE );(* TODO: Get field value from your application *) (* Add new field to the record buffer *) fbWriter( pBuffer := ADR( sCSVLine ), cbBuffer := SIZEOF( sCSVLine ) - 1, putValue := sCSVField, pValue := 0, cbValue := 0, bCRLF := ( nColumn = MAX_CSV_COLUMNS ) );(* bCRLF == TRUE => Write CRLF after the last field value *) IF fbWriter.bOk THEN fbWriter.eCmd := eEnumCmd_Next;(* Write next field value *) ELSE(* Error *) step := 100; RETURN; END_IF END_FOR(* FOR nColumn := 0... *) (* FB_FilePuts adds allready CR (carriage return) to the written line. We have to replace the $R$L characters with $L character to avoid double CR. *) IF RIGHT( sCSVLine, 2 ) = '$R$L' THEN sCSVLine := REPLACE( sCSVLine, '$L', 2, LEN( sCSVLine ) - 1 ); END_IF nRow := nRow + 1;(* Increment number of created records (rows) *) step := 4;(* Write record to the file *) ELSE(* All rows written => Close file *) step := 10; END_IF

Share this post


Link to post
Share on other sites
Hello again vinny, You should definitely take a look at the P_BinaryModeWrite to get the desired result, if that method is not giving you the correct output as it writes binary information inside the CSV file, then you should go with your approach, but you'll need to make a little bit more of work: As far as I've understood the Beckhoff sample, they are not converting the array to the string and then they start operating with the indexes, they are using in each loop cycle a different array position and then they are converting that position into text. If you want to do that in a CSV and you want to put your text into an excel cell, you should do the same but before converting it into a string, then you should start parsing the structure and concatenate the just-converted strings to put them inside the STRING_TO_CSVFIELD method. This should work, less effort with the binary approach. not so difficult if you do it by yourself tuning a little bit the sample. PS: Keep in mind that depending on the controller type you can be in problems: if that controller has a CFCard or a SDCard, then you can burn the card soon, those devices have a limited number of write cycles. Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0