Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
litesom:devicetree:can [2017/06/09 21:31]
filug [CAN sender]
litesom:devicetree:can [2017/06/09 22:14]
filug [C/C++ API]
Line 212: Line 212:
 canbusload can0@125000 -r -t -b -c canbusload can0@125000 -r -t -b -c
 </code> </code>
 +
 +===== C/C++ API =====
 +
 +In case when you want to develop your own application to access CAN bus via ''SocketCAN'' please check [[https://www.kernel.org/doc/Documentation/networking/can.txt|Section 4 in SocketCAN readme]] file.
 +
 +In most cases access to the CAN bus looks like access to the ''linux'' interface . Please check //pseudo-code// example presented below.
 +
 +<code>
 +int s;
 +struct sockaddr_can addr;
 +struct ifreq ifr;
 +
 +/*
 + * Open connection with CAN bus
 + */
 +s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
 +
 +strcpy(ifr.ifr_name, "can0" );
 +ioctl(s, SIOCGIFINDEX, &ifr);
 +
 +addr.can_family = AF_CAN;
 +addr.can_ifindex = ifr.ifr_ifindex;
 +
 +bind(s, (struct sockaddr *)&addr, sizeof(addr));
 +
 +/*
 + * build CAN data frame
 + */
 +struct can_frame frame;
 +frame.can_id = node_id | CAN_EFF_FLAG;  // node id + extended frame format
 +frame.can_dlc = 6;                      // data frame length 
 +frame.data[0] = 0x11;                   // data frame first byte
 +frame.data[1] = 0x22;
 +frame.data[2] = 0x33;
 +frame.data[3] = 0x44;
 +frame.data[4] = 0x55;
 +frame.data[5] = 0x66;                   // data frame last byte
 +
 +/*
 + * send data frame via CAN bus
 + */
 +write(s, &frame, sizeof(frame));
 +</code>
 +
 +===== Python API =====
 +
 +Via [[https://buildroot.org|Buildroot]] ''BR2_PACKAGE_PYTHON_CAN'' option located in
 +<code>
 +-> Target packages
 +  -> Interpreter languages and scripting
 +    -> External python modules
 +      -> python-can
 +</code>
 +you can build ''python-can'' Python3 package.
 +
 +Please read [[https://python-can.readthedocs.io/en/stable/|python-can documentation]] to check how to access CAN from python based application.
  • litesom/devicetree/can.txt
  • Last modified: 2021/05/12 10:08
  • by kateryna.kozakova