CANoe CAPL 接收以太网报文--UDP

工具:CANoe VN5610

软件:CANoe 11.0

调用函数:

on preStart
{}
on sysvar_update sysvar::Receiver::open
{} //打开udp端口函数

on sysvar_update sysvar::Receiver::close
{} //关闭udp端口函数

on preStop

{}

void OnUdpReceiveFrom( dword socket, long result, dword address, dword port, char buffer[], dword size)

{} //udp报文监测函数,将接收到的数据存在char buffer[]中

具体实现:

/*@!Encoding:1252*/

variables
{
  UdpSocket gSocket;
  char      gRxBuffer[1500];
}
on preStart
{
  @sysvar::Receiver::open = 1;
}
on sysvar_update sysvar::Receiver::open
{
  // on open button down...
  if (@this == 1)
  {
    // Open an UDP socket. As source address 0 is used, this means that
    // the configure address of the TCP/IP stack is used. See TCP/IP stack
    // configuration dialog in the simulation setup
    // On UDP port 40001 we want to receive UDP pacekts.
    gSocket = UdpSocket::Open(0, 0);
    
    if (IpGetLastError() != 0)
    {
      // if UdpSocket::Open fails, we print a message to the write window
      write( "<%BASE_FILE_NAME%> UdpSocket::Open failed with reauls %d", IpGetLastError() );
      return;
    }
    
    // To receive data with the created socket, we have to call ReceiveFrom.
    gSocket.ReceiveFrom( gRxBuffer, elcount(gRxBuffer) );
    
    // if ReceiveFrom does not immediatelly copy to to gRxBuffer, it returns 997 to
    // indicate it will call the callback function OnUdpReceiveFrom later.
    if ((gSocket.GetLastSocketError() != 0) && (gSocket.GetLastSocketError()  != 997))
    {
      char errorString[100];
      // if ReceiveFrom fails, we print a message to the write window
      gSocket.GetLastSocketErrorAsString( errorString, elcount(errorString) );
      write( "<%BASE_FILE_NAME%> ReceiveFrom failed with result %d (%s)", IpGetLastError(), errorString );
    }
    
    // update panel controls state
    EnableControl( "Receiver", "OpenButton", 0 );
    EnableControl( "Receiver", "CloseButton", 1 );
  }
}

on sysvar_update sysvar::Receiver::close
{
  // on open button down...
  if (@this == 1)
  {
    // Close socket
    gSocket.Close();

    // update panel controls state
    EnableControl( "Receiver", "OpenButton", 1 );
    EnableControl( "Receiver", "CloseButton", 0 );
  }
}

on preStop
{
  // Close socket on measurement stop
  gSocket.Close();
}

// Callback function, which is called if a UDP packet is received
void OnUdpReceiveFrom( dword socket, long result, dword address, dword port, char buffer[], dword size)
{
  if (result == 0)
  {
    char addressString[20];
    
    ipGetAddressAsString( address, addressString, elcount(addressString) );
    
    // set sysvars to display receive data in panels
    sysSetVariableString( sysvar::Receiver::Receiver_ip, addressString );
    sysSetVariableString( sysvar::Receiver::Receive_arr, buffer);
  }

  // To receive more data, we have to call ReceiveFrom again.
  gSocket.ReceiveFrom( gRxBuffer, elcount(gRxBuffer) );
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值