简体   繁体   English

需要将数据从CAPL写入C#代码并读回CAPL脚本

[英]Need to Write Data from CAPL to C# code and read back to CAPL script

I am using CANoe tool for CAPL scripting. 我正在使用CANoe工具进行CAPL脚本编写。

And I have referred this link for help given by Vector: https://vector.com/portal/medien/cmc/application_notes/AN-IND-1-011_Using_CANoe_NET_API.pdf 我也参考了此链接,以获取Vector提供的帮助: https : //vector.com/portal/medien/cmc/application_notes/AN-IND-1-011_Using_CANoe_NET_API.pdf

Now I am facing issue at step or section: 2.7 Calling CAPL Functions. 现在,我在步骤或部分面临问题:2.7调用CAPL函数。 There are no syntax errors, but I think, the code is not able to retrieve the function from the CAPL file as it is giving this error when I pass values to the function from C# code. 没有语法错误,但是我认为代码无法从CAPL文件中检索函数,因为当我从C#代码中将值传递给函数时,它给出了此错误。

Error statement: 错误陈述:

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code System.Core.dll中发生类型'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'的异常,但未在用户代码中处理

Additional information: Cannot convert null to 'int' because it is a non-nullable value type 附加信息:无法将null转换为'int',因为它是不可为null的值类型

If there is a handler for this exception, the program may be safely continued. 如果有用于此异常的处理程序,则可以安全地继续执行该程序。

Can some one tell me what exactly the issue is? 有人可以告诉我问题出在哪里吗?

Code: 码:

public void Init()
{
 CANoe.Application myApp;
 myApp = new CANoe.Application();
 CANoe.Measurement mymeasure;
 mymeasure = (CANoe.Measurement)myApp.Measurement;
 myApp.Open(@"D:\Planter CAPl\CANoeCAPLdll.cfg", true, true);
 CANoe.OpenConfigurationResult ocresult = myApp.Configuration.OpenConfigurationResult;

 if (ocresult.result == 0)
   {
     CANoe.CAPL CANoeCAPL = (CANoe.CAPL)myApp.CAPL;
     CANoeCAPL.Compile(null);
     CANoe.CAPLFunction mydata;
     mydata = (CANoe.CAPLFunction)CANoeCAPL.GetFunction("Data");//CANoeCAPL.GetFunction("DataShare");

      mymeasure.Start();
      Thread.Sleep(2000);
      int Result = (int)mydata.Call(10,20,30);// Exeception error is coming at this point*

       if (mymeasure.Running)
         {
           mymeasure.Stop();
         }
   }
}

I think the error message is relatively self-explanatory, you're trying to convert a null value to an int type. 我认为错误消息是相对不言自明的,您正在尝试将null值转换为int类型。 Since int is a value type, it cannot be null , therefore, you get an exception. 由于int是一个值类型,因此不能为null ,因此会出现异常。

In the line 在行中

int Result = (int)mydata.Call(10,20,30);

The value returned by mydata.Call(10,20,30) is null . mydata.Call(10,20,30)返回的mydata.Call(10,20,30) null You should check if that value is null and do something. 您应该检查该值是否为null并执行某些操作。

var myDataResult = mydata.Call(10,20,30);
if(myDataResult == null)
   // do something
else
   int result = (int)myDataResult;

As to why the call to the Call method returns null , you'll have to check the API you're using. 至于为什么对Call方法的Call返回null ,则必须检查所使用的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM