News:

  • October 02, 2026, 07:50:25 PM

Login with username, password and session length

Author Topic: String Formatting with variables  (Read 10227 times)

Bobby

  • Sr. Member
  • ****
  • Posts: 60
String Formatting with variables
« on: November 29, 2018, 11:37:51 AM »
Hello all,

I am stuck and hoping someone can guide me.

I am doing a 'STRPRINT' using the following: "$02" "$13" "$31" "$21" "$20" "$03" "$94" "$04"

and then I am streaming that out to another device.

When doing this I get what I expect. The problem is I need to make some of those parts of the strings variables. I cannot seem to find a way to take say D0 and get the ability to do $D0.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3847
    • Host Engineering
Re: String Formatting with variables
« Reply #1 on: November 29, 2018, 12:34:53 PM »
You can do multiple characters in a single string literal.

Not sure what our data is or if it is ASCII or raw binary.

$02 is STX
Not sure of the $13 thru $94 (some may be checksum?)

$03 is ETX
$04 is EOT

What is the actual specification form of your packet?

To answer your original question, just do

"$02$13" D0 "$03$94$04"

But I am guessing you will need checksum for $94 part???


Bobby

  • Sr. Member
  • ****
  • Posts: 60
Re: String Formatting with variables
« Reply #2 on: November 29, 2018, 12:54:02 PM »
This what what I am trying to do here:

STX      1      Start of transmission (0x02)

MID   1      Message ID.  This alternates between 0x13 and 0x14 to prevent
messages from being processed twice.

       TO_ID   1      ID of the requested.
            TO_ID= 0x31

       FROM _ID   1      ID of the replying.
            FROM_ID=XXXX ID = 0x31

       LEN      1      Length in bytes of CMD and DATA plus 0x20 (fixed offset)

       CMD   1      Command Byte (see ?Command and Definitions? table)

       DATA   0..50      Data bytes associated with CMD.  These bytes are always sent as ASCII
characters.

       ETX      1      End of Message (0x03)

       CHK      1      Checksum.  XOR of MID, ID, LEN, CMD, and all DATA bytes with bit 7 of
the result always set to 1.

       EOT      1      End of Transmission (0x04).

The problem I am running into is how do I calculate and then include this checksum into my messages. I also cannot find a way to get the data to send right if not using the $ symbol in front, so if I called say D0 it doesnt come through right, I need it to work as something with a $ in front of it. so the $hh version of that I belive.

Bobby

  • Sr. Member
  • ****
  • Posts: 60
Re: String Formatting with variables
« Reply #3 on: November 29, 2018, 01:29:03 PM »
I added an image to clear up the issue I am trying to fix.

If you look at the attached image. You will see the Print Script I currently have. Where the odd characters are the result of using "$31" etc. etc.

The problem I am having is I need the D0 to also go through as that unprintable character so the resulting HEX sent out is 94 and not 39 34 like it is in the image.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3847
    • Host Engineering
Re: String Formatting with variables
« Reply #4 on: November 29, 2018, 01:55:52 PM »
STRPRINT has a Raw(value, #bytes <,optional swap>) command, so something like

STRPRINT SL0 "$02" Raw(MsgID,1) Raw(ToID,1) Raw(FromID,1) Raw(Cmd,1) Data
*** EDIT FORGOT DATA IS ASCII, SO DO NOT DO RAW ON Data ***

Then use the CHECKSUM instruction with 0-based offset range of 1 to the end of the string (note I did NOT include "$03" in the first STRPRINT)

MATH the Checksum byte by or-ing in 0x80
MATH CheckSum "CheckSum | 0x80"

Then do another STRPRINT to append the ETX and raw check checksum byte (there is an option in STRPRINT to APPEND)
STRPRINT (append) SL0 "$03" Raw(Checksum,1) "$01"

Make sure you UNcheck Automatically Insert a Space on BOTH STRPRINTs.


franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3847
    • Host Engineering
Re: String Formatting with variables
« Reply #5 on: November 29, 2018, 02:00:14 PM »
Hit F1 to bring up STRPRINT Help, then look for the String Print Scripting Language Reference in the Related Topics section near the bottom.

Bobby

  • Sr. Member
  • ****
  • Posts: 60
Re: String Formatting with variables
« Reply #6 on: November 29, 2018, 02:30:41 PM »
Hit F1 to bring up STRPRINT Help, then look for the String Print Scripting Language Reference in the Related Topics section near the bottom.

Thank you so much! The raw command is what I needed! I was looking in that help files but hadn't made it down that far on the string page  ;D

This is why I love the do-more PLC and recommend it to everyone I know. The support is great!