Please hear me out on this. Let sysA
be a system variable (struct member) of type Uint32
used to store a unique hexadecimal ID four bytes long.
In my CAPL script, I'd like to perform a check between this UUID and an integer I re-define by composition of four bytes coming from a message. I went with this approach:
variables
{
dword uuid;
}
on message myMsg
{
uuid = (this.byte(0)<<24) | (this.byte(1)<<16)| (this.byte(2)<<8) | this.byte(3);
if (@sysA == uuid)
{
// do something
}
}
this code triggers error, because of incompatible types dword
and Uint32
. I then tried casting sysA
to dword
: invalid type cast
. I'm not sure why this would be the case in the CAPL language. According to the docs:
byte (unsigned, 1 Byte)
word (unsigned, 2 Byte)
dword (unsigned, 4 Byte)
int (signed, 2 Byte)
long (signed, 4 Byte)
int64 (signed, 8 Byte)
qword (unsigned, 8 Byte)
I cannot type cast Uint32 system variables to int
: int i = 32768
is out of range and int i = 32768LL
isn't valid. But dword
, in this context, is strictly unsigned int
of 4 bytes.
What should I correctly use as type for my uuid, in this scenario, and why?
I tried your code snippet too. You should have a try with defining the proper namespace also when you're trying to use system variables.
variables
{
dword uuid;
}
on message *
{
uuid = ((this.byte(0) << 24) | (this.byte(1) << 16) | (this.byte(2) << 8) | (this.byte(3)));
if ((dword)@namespace::sysA == uuid)
{
write ("Hello World!");
}
}