socketcan的测试例程

使用的是autoware源码中的socketcan程序驱动

  • 发送例程
#include <socketcan/socket_can_receiver.hpp>
#include <socketcan/socket_can_sender.hpp>

#include <chrono>
#include <memory>
#include<iostream>
using autoware::drivers::socketcan::SocketCanReceiver;
using autoware::drivers::socketcan::SocketCanSender;

using autoware::drivers::socketcan::CanId;
using autoware::drivers::socketcan::StandardFrame;
using autoware::drivers::socketcan::ExtendedFrame;
using autoware::drivers::socketcan::FrameType;

std::chrono::milliseconds send_timeout_{1LL};
CanId send_id{0x123};
int main(int argc,char*argv[])
{
//shilihua sender can0
SocketCanSender socket_s("can0",send_id);
//fasongshuju
union data{
    char ch[8];
    uint64_t total;
};
union data to_send;
to_send.ch[0] = 0x41;
to_send.ch[1] = 0x42;
to_send.ch[2] = 0x43;
to_send.ch[3] = 0x44;
to_send.ch[4] = 0x45;
to_send.ch[5] = 0x46;
to_send.ch[6] = 0x47;
to_send.ch[7] = 0x48;
//uint64_t send_data = 0x5A'5A'5A'5A'U;

//fasong
socket_s.send(&to_send.total,sizeof(to_send),send_id,send_timeout_);

//xianshifasong
printf("send data : ");
for(int i =0; i <8;++i)
printf(" 0x%x",to_send.ch[i]);
std::cout<<std::endl;

return 0;
}

输出结果:

send data :  0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48
  • 接收例程
#include <socketcan/socket_can_receiver.hpp>
#include <socketcan/socket_can_sender.hpp>

#include <chrono>
#include <memory>
#include<iostream>
using drivers::socketcan::SocketCanReceiver;
using drivers::socketcan::SocketCanSender;

using drivers::socketcan::CanId;
using drivers::socketcan::StandardFrame;
using drivers::socketcan::ExtendedFrame;
using drivers::socketcan::FrameType;

std::chrono::milliseconds receive_timeout_{10LL};
CanId send_id{0x123};
CanId canid_recv;
int main(int argc,char*argv[])
{
//shilihua receiver can0
SocketCanReceiver socket_r("can0");

//jieshoushuju
uint64_t recv_data;

//shujudayin
union data{
    unsigned char ch[8];
    uint64_t total;
};
union data recv;

canid_recv = socket_r.receive(recv_data,receive_timeout_);

recv.total = recv_data;

printf("canid_recv id : 0x%x ,length : %d \n",canid_recv.get(),canid_recv.length());

printf("receive data : ");
for(int i =0; i <8;++i)
printf(" 0x%x",recv.ch[i]);
//std::cout<<" "<< recv.ch[i];
std::cout<<std::endl;

return 0;
}

输出结果:

canid_recv id : 0x123 ,length : 8 
receive data :  0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48
  • cmakelists.txt
cmake_minimum_required(VERSION 3.5)
project(socketcan)
# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
#find_package(autoware_auto_cmake REQUIRED)
find_package(ament_cmake_auto REQUIRED)
#ament_auto_find_build_dependencies()

ament_auto_add_library(${PROJECT_NAME}
  src/socket_can_common.hpp
  src/socket_can_common.cpp
  src/socket_can_id.cpp
  src/socket_can_receiver.cpp
  src/socket_can_sender.cpp)
#autoware_set_compile_options(${PROJECT_NAME})
add_executable(socketcan_send test/socketcan_send.cpp)
target_link_libraries(socketcan_send ${PROJECT_NAME})
#
add_executable(socketcan_recv test/socketcan_recv.cpp)
target_link_libraries(socketcan_recv ${PROJECT_NAME})

ament_auto_package()
  • packages.xml 使用的源码中test里的xml文件



  socketcan
  0.0.2
  Simple wrapper around SocketCAN
  Apex.AI, Inc.
  Apache License 2.0

  ament_cmake_auto
  autoware_auto_cmake

  ament_cmake_gtest

  ament_lint_auto
  ament_lint_common
  ament_cmake_pclint

  
    ament_cmake
  

你可能感兴趣的:(网络协议)