資源簡介
樹莓派CAN通訊(c語言)
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
int?main()
{
????int?ret;
????int?s?nbytes;
????struct?sockaddr_can?addr;
????struct?ifreq?ifr;
????struct?can_frame?frame;
????memset(&frame?0?sizeof(struct?can_frame));
????system(“sudo?ip?link?set?can0?type?can?bitrate?100000“);
????system(“sudo?ifconfig?can0?up“);
????printf(“this?is?a?can?send?demo\r\n“);
????//1.Create?socket?<創建接口>
????s?=?socket(PF_CAN?SOCK_RAW?CAN_RAW);
????if?(s?0)?{
????????perror(“socket?PF_CAN?failed“);
????????return?1;
????}
????//2.Specify?can0?device?<指定can0設備>
????strcpy(ifr.ifr_name?“can0“);
????ret?=?ioctl(s?SIOCGIFINDEX?&ifr);
????if?(ret?0)?{
????????perror(“ioctl?failed“);
????????return?1;
????}
????//3.Bind?the?socket?to?can0?<將接口綁定到can0>
????addr.can_family?=?AF_CAN;
????addr.can_ifindex?=?ifr.i
評論
共有 條評論