Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: TheGreatMarklar on October 07, 2021, 01:23:46 PM

Title: Add data to a CSV file?
Post by: TheGreatMarklar on October 07, 2021, 01:23:46 PM
 I have a tab-delimited CSV file from a Cmore chart. I've used FTP to copy it to the BRX file system so I can rename it and add several strings to the end. How do I format the string for the FILEWRITE command? The command succeeds, but all it does is add a string of gibberish to the CSV, so I assume the string is formatted incorrectly.

String programmed in STRPRINT: SS50 "$T" SS51 "$T" SS52 "$T" SS53 "$T" SS54 "$T" SS55 "$T" SS56 "$T" "$T" "$T" "$T" "$T" "$T" "$T" "$T" "$T"

writes to SL1: SS50 "$09" SS51 "$09" SS52 "$09" SS53 "$09" SS54 "$09" SS55 "$09" SS56 "$09" "$09" "$09" "$09" "$09" "$09" "$09" "$09" "$09"

There are 16 fields in the CSV, so this string needs 15 <TAB> characters, right?
Title: Re: Add data to a CSV file?
Post by: Greg on October 08, 2021, 09:04:57 AM
I used your string in a STRPRINT. I set the short strings (SS50-SS56) to "A", "B", etc. Then I used a FILEOPEN with the Mode as "Write (Create/Append if exists)." In this case I chose the RAM file system (not the SD card). I called the file MyLog.txt and created a File Handle Struct called FILE_0. Then I used the FILEWRITE of SL1. Then I used a FILECLOSE. All completed with success.

Then I used PLC --> Browse PLC File Systems... chose the RamFS, browsed to root, copied the file to my PC, and opened it in NotePad. The result:

A   B   C   D   E   F                              

Each character is separated by a horizontal tab as expected, with 9 tabs appended to the end.

I did this multiple times and never ended with "gibberish".

Can you post the CSV file you started with? Or email it to support@hosteng.com?
Title: Re: Add data to a CSV file?
Post by: TheGreatMarklar on October 11, 2021, 09:06:35 AM
Forum won't let me attach the file; I'll email. It is a Chart Log generated by a Cmore.
Title: Re: Add data to a CSV file?
Post by: Greg on October 11, 2021, 01:50:28 PM
Forum won't let me attach the file; I'll email. It is a Chart Log generated by a Cmore.
The issue here is that the C-more generated file is in Unicode ASCII, which Do-more doesn't support. If you open up your file in a hex editor, you can see that after each ASCII byte, there is another byte. In this case, that added byte is an ASCII null character. So, instead of ASCII for "ABCD" being 41 42 43 44, it is actually 41 00 42 00 43 00 44 00. The text editor that you observe the file in comprehends this Unicode and displays merely, "ABCD." So, when you created your string using the STRING print, your string has single-byte ASCII, not double-byte Unicode. And when you append this record to your Unicoded file, the text editor you are using, tries to interpret that string as Unicode (16-bit) when it is actually only 8-bit. That's why it looks like gobbly goop at the end.

Here's what I did to make it work:


This, or something similar, should make it work properly. I don't pretend to be a super-programmer, but the above code worked for me using your original file. NotePad (which obviously recognized Unicode), now shows the appended record properly instead of the gobbly goop it was previously showing.
Title: Re: Add data to a CSV file?
Post by: TheGreatMarklar on October 11, 2021, 01:55:05 PM
Thanks again, Greg! Works great!