How to write a string literal to a tag-based PLC via Macro

How to write a string literal to a tag-based PLC via Macro
none 0.0 0

I have a string that is in a codesys variable
which is selected by a macro
GetData(Name[0], “Weintek Built-in CODESYS”, “Application.PersistentVars.Channels[0].Title”, 16)

and stored in a LW internal variable
SetData(Name[0], “Local HMI”, “String”, 16)

So if my codesys variable contains ‘12121212’
the LW variable has ‘1111’

it is then displayed in a text display
As ‘1111’
I can imagine why this is , but cannot fix it
what the soulition.
I 1mport the tags as 2 chr$ per word, and have tried it the other way around.

Thanks

Due to the specification of Macro, Macro uses WORD to calculate and map string data of a Codesys PLC. When using Macro to transfer string data from or to a Codesys PLC device, the variable must be defined as SHORT, so it can map every character with PLC correctly.

macro_command main()

short Name[40]

GetData(Name[0], “Weintek Built-in CODESYS”, “Application.PersistentVars.Channels[0].Title”, 8)

SetData(Name[0], “Local HMI”, “String”, 8)

end macro_command

1 Like

If I want to set a string literal in a macro such as:
char Name[32]=“”

macro_command main()
StringCopy( “XH2 TANK Pressure(PSIG)”,Name[0])
SetData(Name[0], “Weintek Built-in CODESYS”, “Application.PGVL.Channels[0].Title”, 16)

THen i onlly see the fist character ‘X’ i assume for the same reasons but

short Name[16]={0}
macro_command main()
StringCopy( “XH2 TANK Pressure(PSIG)”,Name[0])

dosen’t work

How do I assign Name a value ?

macro_command main()

char Name[32]
short s_Name[16]

StringCopy( “XH2 TANK Pressure(PSIG)”,Name[0])
SetData(Name[0], “Local HMI”, “String”, 32)

GetData(s_Name[0], “Local HMI”, “String”, 16)
SetData(s_Name[0], “Weintek Built-in CODESYS”, “Application.PGVL.Channels[0].Title”, 16)

end macro_command

@david,

The method @TimWusa described is a shortcut for remapping the char sequence to a word array. The reason this is necessary is that tag-based PLCs like CODESYS require STRING types to be written using 16-bit arrays. To remap this in code, it would look like:

char Name[32]=""

macro_command main()
	StringCopy("XH2 TANK Pressure(PSIG)",Name[0])
	
	short sName[16], tmp = 0
	// To convert data to word format from byte
	short i[3] = {0,0,0}
	for i[0] = 0 to 15
		i[1] = i[0] * 2
		i[2] = i[1] + 1
		tmp = Name[i[2]] << 8
		sName[i[0]] = tmp + Name[i[1]]
	next
	
	SetData(sName[0], "CODESYS V3 (Ethernet)", "Application.PLC_PRG.sData", 16)
end macro_command

The method Tim outlined is a shortcut that uses HMI registers as placeholders such that the char (byte) data is first set within the HMIs register and then re-read as a short (word) prior to transfer:

char Name[32]
short s_Name[16]

SetData(Name[0], “Local HMI”, “String”, 32)

GetData(s_Name[0], “Local HMI”, “String”, 16)

Thanks to both of you. Good information.