【问题标题】:CAPL invalid type cast of uint32 to dworduint32 到 dword 的 CAPL 无效类型转换
【发布时间】:2020-03-11 12:31:11
【问题描述】:

请听我说完。让sysA 成为Uint32 类型的系统变量(结构成员),用于存储一个唯一的十六进制ID,长度为四个字节。 在我的 CAPL 脚本中,我想在此 UUID 和我通过来自消息的四个字节的组合重新定义的整数之间执行检查。我采用了这种方法:

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
    }
}

此代码触发错误,因为 dwordUint32 类型不兼容。然后我尝试将sysA 转换为dwordinvalid type cast。我不知道为什么在 CAPL 语言中会出现这种情况。根据文档:

字节(无符号,1字节)

字(无符号,2字节)

双字(无符号,4字节)

int(有符号,2字节)

长(有符号,4字节)

int64(有符号,8 字节)

qword(无符号,8 字节)

我无法将 Uint32 系统变量类型转换为 intint i = 32768 超出范围,int i = 32768LL 无效。但是dword,在这种情况下,严格来说是4字节的unsigned int

在这种情况下,我应该正确地使用什么作为我的 uuid 的类型,为什么?

【问题讨论】:

  • 我认为dword 作为uuid 的类型很好。您能否显示您尝试将 @sysA 显式转换为 dword 的代码?

标签: capl


【解决方案1】:

我也试过你的代码 sn-p。当您尝试使用系统变量时,您也应该尝试定义正确的命名空间。

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!");
  }
}

【讨论】:

  • 您好 vakesz,感谢您的反馈。是的,很可能我忘记在 sn-p 中报告命名空间,但是您无法在 CANoe 中创建没有命名空间的变量。你试过你的代码sn-p吗?它可以正常工作吗?
  • 嗨,我确实尝试编译我发送的代码。它正在编译而没有警告。但是我没有在真实的模拟中尝试过。我使用的是 CANalyzer 10。命名空间是我为系统变量定义的命名空间的名称。您可以通过检查变量来检查它,您将其定义为命名空间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 2012-11-19
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多