[讨论]
跪求SAM 9x25 can总线的一些问题的指教
如题目说的,该型号有2路can总线,我按照配套例程,只想使用can1,不想使用can0 ,因为使用can0 后,debug串口不能使用,也就无法使用板子的文件系统和终端。代码以给出截图,我只是将can0 改为can1 。但是显示出类似没有can1这个设备的问题提示,打开can1 失败。求指教。- nt main(char *argv[], int argc)
- {
- int s,res;
- pthread_t id;
- unsigned long nbytes;
- struct sockaddr_can addr;
- struct ifreq ifr;
- struct can_frame frame;
- char senddata[80] = { "test" };
- int len;
- int index;
- s = socket(PF_CAN,SOCK_RAW,CAN_RAW); // Create a socket with PF_CAN family, SOCK_RAW and CAN_RAW protocol
- strcpy((char *)(ifr.ifr_name),"can0"); // Use can0
- res = ioctl(s,SIOCGIFINDEX,&ifr); // Control device and get information
- printf("ioctl:%d\n", res);
- printf("can0 can_ifindex = %x\n",ifr.ifr_ifindex);
- int loopback = 0; /* 0 = disabled, 1 = enabled (default) */
- setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback)); // Setting lookback options
- printf("set can0 sock loopback unable:%d\n", res);
- int recv_own_msgs = 0; /* 0 = disabled (default), 1 = enabled */
- res = setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, // Setting receiver own massages options
- &recv_own_msgs, sizeof(recv_own_msgs));
- printf("set can0 do not receiver own massage:%d\n", res);
- addr.can_family = AF_CAN; // Use AF_CAN protocol family
- addr.can_ifindex = ifr.ifr_ifindex; // frame index
- res = bind(s,(struct sockaddr*)&addr,sizeof(addr)); // Binding sock
- printf("bind can0 sock:%d\n", res);
- /* Create a tread for can1 receiver and send message */
- // pthread_create(&id, NULL, (void*)can1_thread, NULL);
- frame.can_id = 0x123;
- len = strlen(senddata) - 1;
-
- while (1) {
- // memset(senddata, 0, sizeof(senddata));
- /* Get input from stdin */
- // fgets(senddata, sizeof(senddata), stdin);
- /* nbytes = recvfrom(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr, 16);
- if (nbytes != -1) {
- printf("Recv message: %d %s\n", nbytes, frame.data);
- strncpy(frame.data, senddata, 5);
- frame.can_dlc = strlen(frame.data);
- nbytes = sendto(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr, sizeof(addr));
- if (nbytes != -1) {
- printf("Send success:%d %s\n", nbytes, senddata);
- }
- }
-
- */
- strncpy(frame.data, senddata, 5);
- frame.can_dlc = strlen(frame.data);
- nbytes = sendto(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
- if (nbytes != -1) {
- printf("Send success: %d %s\n", nbytes, senddata);
- usleep(500000);
- nbytes = recvfrom(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr, &len);
- if (nbytes != -1) {
- printf("Recv message: %d %s\n", nbytes, frame.data);
- }
- }
- /* Send 8 bytes each time */
- /*for (index = 0; index < len / 7; ++index) {
- memset(frame.data, 0, 8);
- strncpy(frame.data, &senddata[index * 7], 7);
- frame.can_dlc = strlen(frame.data); // Set data length
-
- nbytes = sendto(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
- if (nbytes == -1) {
- perror("send error");
- } else {
- printf("send %d bytes to can0: %s\n", frame.can_dlc, frame.data);
- /* receiver feedback message */
- /* nbytes = recvfrom(s,&frame,sizeof(struct can_frame),0,(struct sockaddr *)&addr,&len);
-
- if (nbytes == -1) {
- perror("recv error");
- } else {
- printf("recv %d bytes from can0: %s\n", frame.can_dlc, frame.data);
- }
- }
- }
- if (len % 7 != 0) {
- memset(frame.data, 0, 8);
- strncpy(frame.data, &senddata[index * 7], len % 7);
- frame.can_dlc = strlen(frame.data);
- nbytes = sendto(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr*)&addr, sizeof(addr));
- if (nbytes == -1) {
- perror("send error");
- } else {
- printf("send %d bytes to can0: %s\n", nbytes, frame.data);
- /* receiver feedback message */
- /* nbytes = recvfrom(s,&frame,sizeof(struct can_frame),0,(struct sockaddr *)&addr,&len);
-
- if (nbytes == -1) {
- perror("recv error");
- } else {
- printf("recv %d bytes from can0: %s\n", frame.can_dlc, frame.data);
- }
- }
- }*/
- }
- return 0;
- }
暂无评论,赶紧抢沙发吧