Moxa V2426A-C2-W7E [36/88] Epm 3112 programming guide

Moxa V2426A-C2 [36/88] Epm 3112 programming guide
V2400A Series Expansion Modules Software Installation and Programming Guide
4-16
EPM-3112 Programming Guide
The following code is a working example of the SocketCAN API, which sends packets using the raw interface.
It is based on the notes documented in the Linux Kernel
(https://www.kernel.org/doc/Documentation/networking/can.txt
).
Folder and file: /examples/ExpansionCard/LX/EPM3112/can_read_write/can_write.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int
main(void)
{
int s;
int nbytes;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr;
char *ifname = "can1";
if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Error while opening socket");
return -1;
}
strcpy(ifr.ifr_name, ifname);
ioctl(s, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
printf("%s at index %d\n", ifname, ifr.ifr_ifindex);
if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Error in socket bind");
return -2;
}
frame.can_id = 0x123;
frame.can_dlc = 2;
frame.data[0] = 0x11;
frame.data[1] = 0x22;

Содержание

Похожие устройства

Скачать