风火轮科技开发一款rk3588s的主板youyeetoo R1,该主板提供40pin扩展管脚,其中提供1路can管脚使用。管脚分布如下图所示:
如果要在Android系统里面使用shell命令,需要通过ADB进入Android的命令行窗口。ADB的使用教程请参考 adb调试 一章。通过adb打开Android系统命令行后,
adb root
adb shell
ifconfig can0 down
ip link set can0 up type can bitrate 100000
ip -details link show can0
ifconfig can0 up
Android系统的权限管理是十分严格的,使用c++或者java操作Android根文件系统里的文件或资源需要对应的权限。这里编写的ndk程序是基于 system app的作为模板进行编写。system app请参考创建system app这一章。
#include <jni.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#define can_up "ifconfig can0 up"//打开CAN0
#define can_down "ifconfig can0 down"//关闭CAN0
static void setCan0Bitrate(int nBitRate)
{
char anCanCommand[128] = {0,};
printf("- can0 down \n");
system(can_down);
printf("- can0 config bitrate %d\n", nBitRate);
sprintf(&anCanCommand[0], "ip link set can0 type can bitrate %d", nBitRate);
system(anCanCommand);
printf("- can0 up \n");
system(can_up);//关闭CAN设备,设置波特率后,重新打开CAN设备
}
int can0Init(int nBitRate)
{
int nCanFd;
struct sockaddr_can addr;
struct ifreq ifr;
setCan0Bitrate(nBitRate); //配置can0波特率
nCanFd = socket(PF_CAN, SOCK_RAW, CAN_RAW);//创建套接字
printf("- can0 socket \n");
strcpy(ifr.ifr_name, "can0" );
ioctl(nCanFd, SIOCGIFINDEX, &ifr); //指定 can0 设备
printf("- can0 ioctl \n");
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(nCanFd, (struct sockaddr *)&addr, sizeof(addr));//将套接字与 can0 绑定
printf("- can0 binded \n");
return nCanFd;
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_youyeetoo_r1_1i2cdemo_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
int nCanFd, nbytes;
struct can_frame frame[2] = {{0}};
nCanFd = can0Init(1000000); //创建can实例化对象
frame[0].can_id = 0x666;
frame[0].can_dlc = 8;
frame[0].data[0] = 0x40;
frame[0].data[1] = 0x20;
frame[0].data[2] = 0x10;
frame[0].data[3] = 0x00;
frame[0].data[4] = 0x03;
frame[0].data[5] = 0x04;
frame[0].data[6] = 0x05;
frame[0].data[7] = 0x06;
for(int i=0;i<10;i++)
{
printf("- for enter: %d \n", i);
frame[0].data[7]++;
printf("send frames: 0x40 0x20 0x10 0x00 0x03 0x04 0x05 0x06 \n");
nbytes = write(nCanFd, &frame[0], sizeof(frame[0])); //发送 frame[0]
if(nbytes != sizeof(frame[0]))
{
printf("Send Error frame[0]\n!");
}
nbytes = read(nCanFd, &frame[1], sizeof(frame[1]));//接收总线上的报文保存在frame[1]中
printf("the nbytes:%d\n", nbytes);
printf("length:%d \n", sizeof(frame[1]));
printf("ID=0x%X DLC=%d\n", frame[1].can_id, frame[1].can_dlc);
printf("data0=0x%02x\n",frame[1].data[0]);
printf("data1=0x%02x\n",frame[1].data[1]);
printf("data2=0x%02x\n",frame[1].data[2]);
printf("data3=0x%02x\n",frame[1].data[3]);
printf("data4=0x%02x\n",frame[1].data[4]);
printf("data5=0x%02x\n",frame[1].data[5]);
printf("data6=0x%02x\n",frame[1].data[6]);
printf("data7=0x%02x\n",frame[1].data[7]);
sleep(1);
}
close(nCanFd);
return env->NewStringUTF(show_buf);
}