TheOtherMarklar (Customer) asked a question.

How can I append strings to a text file?

Hardware: BRX-DM1E and Cmore EA9

 

I use the Cmore to record a log file during a factory acceptance test. When the test completes, the BRX gets the file via FTP, and adds a string to the end of the file containing the job number, serial, etc. The problem is that when I open the text file, the appended string is a bunch of gibberish. How do I format a PLC String Literal to append it to a Cmore test log file?


  • HOST_Greg (AutomationDirect)

    Here's what I did to make it work:

    1. Started with your original file, which is in Unicode (i.e. ASCII + code). If you view this file in a hex-editor, you can see that after each ASCII byte, there is another byte, a null (0x00). So, the issue is we need to add a "$0" after every ASCII in the string you want to write.
    2. Since in your original file (Data_S6_original_211006.txt), there is a time/date stamp, I prepended that in the same format: STRPRINT, using FmtTime($Now,24h) " " FmtDate($Now,us).
    3. Then I used the same STRPRINT you said you were using and prepended that time/date stamp to it. But, I also added, "$0D$0A" which is the carriage return/line feed that your original file also shows is there.TaskUnicode2
    4. I wrote a Task that copies that string to another working string (e.g. SL1 --> SL2).
    5. This Task has a FOR/NEXT loop, Index: V0, From:0, To: SL1.Length, By Step 1.TaskUnicode
    6. If V0 has reached the last character, I don't have to process, so it has a BREAK
    7. Normal procedure is to use STRINSERT to put the ("$0") after every character. After this TASK runs, the final string (e.g. SL2) now has "Unicoded" ASCII (essentially the ASCII byte + null byte)
    8. After we have the string (e.g. SL2) formatted correctly, do the normal FILEOPEN, FILEWRITE, FILECLOSE.

     

    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.

     

    Expand Post
    Selected as Best
    • TheOtherMarklar (Customer)

      I've attached the original file, as created by the Cmore.

       

      The string I'm creating is: SS50 "$09" SS51 "$09" SS52 "$09" SS53 "$09" SS54 "$09" SS55 "$09" SS56 "$09" "$09" "$09" "$09" "$09" "$09" "$09" "$09" "$09"

       

      • Data_S6_Record_211006
  • Garry (Customer)

    It looks like you are printing HEX values into the file instead of ASCII.

    Are you using the STRPRINT instruction in the BRX before appending it to the file?

    Regards,

    Garry

    Here is a post showing the conversion of Hex to ASCII in the BRX. It is part of the Modbus ASCII protocol but demonstrates the STRPRINT instruction.

    https://accautomation.ca/brx-do-more-plc-modbus-ascii-protocol/

     

    Expand Post
    • TheOtherMarklar (Customer)

      Yes, I'm using STRPRINT to create a single string. The hex characters are the control characters for <TAB> outlined in the Script Help in DMD. Are there different characters I should be using for writing to ASCII?

      • Garry (Customer)

        Can you attach the part of the program dealing with the appending of the file, including the STRPRINT instruction?

        I'll try to duplicate on my equipment tonight.

        P1000 STRPRINT tabThe tab is $09 in the string. The shortcut is $T for scripting.

        Thanks,

        Garry

        Expand Post
      • TheOtherMarklar (Customer)

        Thanks much. I'm sure it's something small I'm missing. Attached the program block.

        • filewrite project
      • Garry (Customer)

        I cannot load the program file. I'm getting errors and it will not load.

        Can you zip the program and send it to me.

        Thanks,

        Garry

      • Garry (Customer)

        Thanks for the file. When I open up your program and do a search on SL0. I get the following references to this location.

        P1000 STRPRINT tab 1Address SL2 and above should be used for your STRPRINT instruction they are not used elsewhere in the program.

        Regards,

        Garry

         

         

        Expand Post
10 of 23