Copying Structure into String Variable ANSI C

Dejo

Level-2
Beiträge
14
Reaktionspunkte
1
Zuviel Werbung?
-> Hier kostenlos registrieren
Hi guys,

I have a little problem here...I'm currently working with B&R and I'm creating a project in ANSI C. The problem that I have is to following:
How can I copy the entire content of a variable (which is a structure) to a single variable (which is a String). For example:
VAR
StringVar : STRING[400];
Message : statusProperties_typ;
END_VAR

where
TYPE
statusProperties_typ : STRUCT
TextVar1 : STRING[80];
TextVar2 : STRING[80];
TextVar3 : STRING[80];
END_STRUCT;
END_TYPE

Now, I want to copy the content from the structure Message into the variable StringVar. How can I do that in ANSI C?
I know how to do it in ST, but with ANSI C i have problems...
In ST it should look like:
StringVar := 'Message'; -> whit this the entire content of Message is copied into the variable StringVar.

can anyone give a tipp or two?

Thanks
 
If "STRING" is equivalent to "char" then you can use sprintf (from stdio.h) to create the string, simular to this:

StringVar[0] = '\0';
sprintf(StringVar, "%s%s%s", statusProperties_typ.TextVar1, statusProperties_typ.TextVar2, statusProperties_typ.TextVar3);

or with string concatenation (from string.h):

strcpy(StringVar, statusProperties_typ.TextVar1);
strcat(StringVar, statusProperties_typ.TextVar2);
strcat(StringVar, statusProperties_typ.TextVar3);
 
Zuviel Werbung?
-> Hier kostenlos registrieren
e.g memcpy().
Independant of the programming tool (AnsiC oder IEC or..). Be aware that you have quite often an alignment to consider (which might possibily introduce some filling bytes. String as Datatype in IEC has an alignment of 1 so no fill bytes.

Guga
 
If "STRING" is equivalent to "char" then you can use sprintf (from stdio.h) to create the string, simular to this:

StringVar[0] = '\0';
sprintf(StringVar, "%s%s%s", statusProperties_typ.TextVar1, statusProperties_typ.TextVar2, statusProperties_typ.TextVar3);

or with string concatenation (from string.h):

strcpy(StringVar, statusProperties_typ.TextVar1);
strcat(StringVar, statusProperties_typ.TextVar2);
strcat(StringVar, statusProperties_typ.TextVar3);
HI,
Thanks for your answer,
but when the StringVar an array is? For Example:
StringVar=statusProperties_typ[0..2];
How can I write that in a FOR loop?

For example:
for(i=0; i<=10; i++)
strcpy(StringVar,statusProperties_typ.TextVar);

When I try to do that, my PLC gets in Service Mode :(

Any ideas?
 
HI,
Thanks for your answer,
but when the StringVar an array is? For Example:
StringVar=statusProperties_typ[0..2];
How can I write that in a FOR loop?

For example:
for(i=0; i<=10; i++)
strcpy(StringVar,statusProperties_typ.TextVar);

When I try to do that, my PLC gets in Service Mode :(

Any ideas?
strcpy(StringVar,statusProperties_typ.TextVar); --> Where is the index i?

statusProperties_typ[0..2] / for(i=0; i<=10; i++) --> Array 0..2, why are you counting 0..10?

As PLC I would put YOU in service mode ;)

By the way: If you use strcpy in the loop, you will only receive the last copy result. If you want to append, you need the strcat. Make sure before loop call the StringVar is empty.
 
Zuviel Werbung?
-> Hier kostenlos registrieren
strcpy(StringVar,statusProperties_typ.TextVar); --> Where is the index i?

statusProperties_typ[0..2] / for(i=0; i<=10; i++) --> Array 0..2, why are you counting 0..10?

As PLC I would put YOU in service mode ;)

By the way: If you use strcpy in the loop, you will only receive the last copy result. If you want to append, you need the strcat. Make sure before loop call the StringVar is empty.
Hi,
thanks for the tip...
Ok, with 2 and 10 I mixed a little bit, but only in here. In the PLC I have a Constant=2;
for(i=0; i<=Constant; i++) and statusProperties_typ[0..Constant]... My mistake :)
 
aaaaa it is not working as I want...Please help or I'll lose my mind!
I have:
VAR
StringVar : STRING[400];
Message : ARRAY[0..Const] of STRING [80];
Const = 1;
END_VAR

for(i=0; i<=Const; i++)
{
strcat(StringVar, Message(i) );
}
What I want to have as a result is: "StringVarMessage[0]Message[1]"
but instead I become: "StringVarMessage[0]Message[1]Message[1]Message[1]Message[1]Message[1]..."

Any tipps?
 
A C program would look like this:

C:
#include <stdio.h>
#include <string.h>

#define MSGCOUNT 2

int main()
{
    int i;
    char StringVar[400];
    char Message[MSGCOUNT][80];
    
    strcpy(Message[0], "Hello");
    strcpy(Message[1], "World");
    
    StringVar[0] = '\0';
    
    for (i = 0; i < MSGCOUNT; i++) {
        strcat(StringVar, Message[i]);
    }
    printf("%s", StringVar);

    return 0;
}
 
Zurück
Oben