【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
can test code
tfl_can.c

#include "tfl_can.h" // int can_init( int* sock ) { struct sockaddr_can addr; struct ifreq ifr; const char* ifrname = "can1"; // open socket if ((*sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("Error open socket...\n"); return 1; } strcpy(ifr.ifr_name, ifrname); // ioctl(*sock, SIOCGIFINDEX, &ifr); ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name); // printf("%s \n", ifr.ifr_name); if (!ifr.ifr_ifindex) { perror("if_nametoindex error...\n"); return 1; } memset(&addr, 0, sizeof(addr)); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; setsockopt(*sock, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); //disable filter. // bind socket if (bind(*sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Error in socket bind...\n"); return 1; } return 0; } void can_tfl(int s, TFL* data) { char buf[5][8] = {0}; buf[0][7] = (data->total)<<2; for(int i=0; i<5; i++) { buf[i][0] = ((data->type)<<4) + (data->color); buf[i][1] = (data->xpos)>>4; buf[i][2] = ((data->xpos)<<8) + ((data->level)<<2); data++; buf[i][4] = ((data->type)<<4) + (data->color); buf[i][5] = (data->xpos)>>4; buf[i][6] = ((data->xpos)<<8) + ((data->level)<<2); buf[i][7] = buf[i][7] + (data->rlcnt); if(i<4) data++; cansend_tfl(s, i, buf[i]); usleep(2000); // us } printf("**********can_tfl once*******\n"); return; } void cansend_tfl(int s, int i, char* buffer) { // send CAN frame struct can_frame frame; if(i==0) frame.can_id = 0x461; else if(i==1) frame.can_id = 0x462; else if(i==2) frame.can_id = 0x463; else if(i==3) frame.can_id = 0x464; else if(i==4) frame.can_id = 0x465; frame.can_dlc = 8; frame.data[0] = *buffer; frame.data[1] = *(buffer+1); frame.data[2] = *(buffer+2); frame.data[3] = *(buffer+3); frame.data[4] = *(buffer+4); frame.data[5] = *(buffer+5); frame.data[6] = *(buffer+6); frame.data[7] = *(buffer+7); printf("frame: %x %x %x %x %x %x %x %x\n", *buffer, *(buffer+1), *(buffer+2), *(buffer+3), *(buffer+4), *(buffer+5), *(buffer+6), *(buffer+7)); // int nbyte = write(s, &frame, sizeof(struct can_frame)); // printf("nbyte: %d, CAN_MTU: %d\n", nbyte, CAN_MTU); if(write(s, &frame, sizeof(struct can_frame)) != CAN_MTU) { perror("write frame error.\n"); return; } return; } int main() { for (int k=0; k<100; k++) { TFL objs[10] = {{1, 1, 640, 3, 2, 0}, {3, 1, 456, 2, 2, 0}, {0}}; for(int i=0; i<10; i++) objs[i].rlcnt = 1; TFL* data = objs; int s; can_init(&s); can_tfl(s, data); close(s); printf("loop: %d\n", k); } } int main_data() { int s; can_init(&s); struct can_frame frame; frame.can_id = 0x461; frame.can_dlc = 8; printf("ID=%#x data length=%d\n", frame.can_id, frame.can_dlc); /* prepare data for sending: 0x11,0x22...0x88 */ for (int i=0; i<8; i++) { frame.data[i] = ((i+1)<<4) | (i+1); printf("%#x ", frame.data[i]); } printf("Sent out\n"); /* Sending data */ if(write(s, &frame, sizeof(frame)) < 0) { perror("Send failed"); return 1; } close(s); return 0; } int main_init() { int s; struct sockaddr_can addr; struct ifreq ifr; const char* ifrname = "can1"; // open socket if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("Error open socket...\n"); return 1; } strcpy(ifr.ifr_name, ifrname); // ioctl(*sock, SIOCGIFINDEX, &ifr); ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name); printf("%s \n", ifr.ifr_name); if (!ifr.ifr_ifindex) { perror("if_nametoindex error...\n"); return 1; } memset(&addr, 0, sizeof(addr)); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); //disable filter. // bind socket if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Error in socket bind...\n"); return 1; } struct can_frame frame; frame.can_id = 0x461; frame.can_dlc = 8; printf("ID=%#x data length=%d\n", frame.can_id, frame.can_dlc); /* prepare data for sending: 0x11,0x22...0x88 */ for (int i=0; i<8; i++) { frame.data[i] = ((i+1)<<4) | (i+1); printf("%#x ", frame.data[i]); } printf("Sent out\n"); /* Sending data */ if(write(s, &frame, sizeof(frame)) < 0) { perror("Send failed"); return 1; } close(s); return 0; }
tfl_can.h
#ifndef TFL_CAN_H #define TFL_CAN_H #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> typedef struct{ unsigned int color; // 0-unknown, 1-green, 2-yellow, 3-red, 4-off; unsigned int type; // 0-unknown, 1-round, 2-left, 3-right, 4-Uturn, 5-bicycle, 6-pedestrain; unsigned int xpos; // 0-1280, image width; unsigned int level; // 0-3, 0-lowest, 3-highest; unsigned int total; // total num of object. unsigned int rlcnt; // Rolling Count, 0<->3. } TFL; int can_init(int*); void can_tfl(int s, TFL* data); void cansend_tfl(int s, int i, char* buffer); #endif
CMakeLists.txt
cmake_minimum_required(VERSION 3.3) project(cantest LANGUAGES C) set(CMAKE_BUILD_TYPE Release) set(INCLUDE_DIRS include) include_directories (${INCLUDE_DIRS}) set(LIBS_DIRS) link_directories(${LIBS_DIRS}) set(LIBS) link_libraries(${LIBS}) aux_source_directory(src SRC_DIRS) add_executable(${PROJECT_NAME} ${SRC_DIRS}) target_link_libraries(${PROJECT_NAME} ${LIBS}) # install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) # add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_SOURCE_DIR}/cmake/make_uninstall.cmake")
参考
1. 解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token;
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
分类:
c/c++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】