Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Understanding non blocking mode in SocketCAN.


๐Ÿ“š Understanding non blocking mode in SocketCAN.


๐Ÿ’ก Newskategorie: Linux Tipps
๐Ÿ”— Quelle: reddit.com

Hi all,

I am currently working with the socketCAN implementation for my project.

The issue i am facing is that in the non blocking mode, while trying to receive the data using the read() file descriptor, I am not able to distinguish whether I have received data or not in my buffer.

It is continuously returning -1 irrespective of the data available or not, any help with the issue would be appreciated.

The code I am using to check is as follows:

include <stdio.h>

include <stdlib.h>

include <string.h>

include <unistd.h>

include <fcntl.h>

include <sys/socket.h>

include <linux/can.h>

include <linux/can/raw.h>

include <net/if.h>

include <sys/ioctl.h>

include <errno.h>

// Function to send a CAN frame
int send_can_frame(int sock, struct can_frame *frame) {
ssize_t nbytes;

nbytes = write(sock, frame, sizeof(struct can_frame));
if (nbytes != sizeof(struct can_frame)) {
perror("write");
return -1;
}

printf("Sent CAN frame: ID=%X, DLC=%d, Data=", frame->can_id, frame->can_dlc);
for (int i = 0; i < frame->can_dlc; ++i) {
printf("%02X ", frame->data[i]);
}
printf("\n");

return 0;
}

int main() {
int sock;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
struct can_frame frame1;

// Create a CAN socket
sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (sock < 0) {
perror("socket");
return 1;
}

// Set the socket to non-blocking mode
int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);

// Specify the CAN interface (change can1 to your interface name)
strcpy(ifr.ifr_name, "can0");
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
perror("SIOCGIFINDEX");
close(sock);
return 1;
}

// Bind the socket to the CAN interface
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
close(sock);
return 1;
}

printf("Socket bound to CAN interface %s\n", ifr.ifr_name);

// Example: Prepare a CAN frame to send
frame.can_id = 0x123;
frame.can_dlc = 2;
frame.data[0] = 0xAB;
frame.data[1] = 0xCD;

while(1)

{

// Send the CAN frame
if (send_can_frame(sock, &frame) < 0) {
close(sock);
return 1;
}

ssize_t nbytes = read(sock, &frame1, sizeof(struct can_frame));
if (nbytes < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
// No data available, continue loop or other actions
printf("1.No data available\n");
printf("\nCAN DLC: %d",frame1.can_dlc);
continue;
} else {
perror("read");
break;
}
} else if (nbytes > 0) {
printf("Received CAN frame: ID=%X, DLC=%d, Data=", frame.can_id, frame.can_dlc);
for (int i = 0; i < frame1.can_dlc; ++i) {
printf("%02X ", frame1.data[i]);
}
printf("\n");
}

close(sock);
return 0;
}

submitted by /u/CompetitiveLow5109
[link] [comments] ...



๐Ÿ“Œ Understanding non blocking mode in SocketCAN.


๐Ÿ“ˆ 69.83 Punkte

๐Ÿ“Œ How to use SocketCAN with the Command-Line in Linux


๐Ÿ“ˆ 32.43 Punkte

๐Ÿ“Œ Blocking Bad: The Importance of Blocking and Virtual Patching


๐Ÿ“ˆ 24.57 Punkte

๐Ÿ“Œ CVE-2014-125093 | Ad Blocking Detector Plugin up to 1.2.1 on WordPress ad-blocking-detector.php information disclosure


๐Ÿ“ˆ 24.57 Punkte

๐Ÿ“Œ Navigation Blocking in Navigation Blocking in React in Other Locations: Preventing Unsaved Changes


๐Ÿ“ˆ 24.57 Punkte

๐Ÿ“Œ HP will pay customers for blocking non-HP ink cartridges in EU


๐Ÿ“ˆ 22.29 Punkte

๐Ÿ“Œ Understanding Ad Blocker: The Ultimate Guide to Blocking Ads Online


๐Ÿ“ˆ 21.09 Punkte

๐Ÿ“Œ Is there a single new device with good non-android non-chrooted linux distro supporti


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Can you recommend me a Non-Google, Non-Apple Smartphone?


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Lutris non GOG/non STEAM


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Lutris non GOG/non STEAM


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Vulnerabilitร  non corrette: il vettore di attacco ransomware che non perdona


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Mark Zuckerberg Denies Knowledge of Non-Consensual Shadow Profiles Facebook Has Been Building of Non-Users For Years


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Consider moving to KDE if you value non-blurry text on your non-hidpi screen


๐Ÿ“ˆ 20.02 Punkte

๐Ÿ“Œ Enable Drak Mode in Windows 10 | Dark Mode | Windows Dark Mode


๐Ÿ“ˆ 18.9 Punkte

๐Ÿ“Œ Discovering JavaScript's Hidden Secrets: Understanding Graphs as a Non Linear Data Structure.


๐Ÿ“ˆ 18.82 Punkte

๐Ÿ“Œ Discovering JavaScript's Hidden Secrets: Understanding Trees as a Non Linear Data Structure.


๐Ÿ“ˆ 18.82 Punkte

๐Ÿ“Œ Understanding Token Contracts in Blockchain For Non-Techie


๐Ÿ“ˆ 18.82 Punkte

๐Ÿ“Œ Googleโ€™s working on stopping sites from blocking Incognito mode


๐Ÿ“ˆ 18.58 Punkte

๐Ÿ“Œ Googleโ€™s working on stopping sites from blocking Incognito mode


๐Ÿ“ˆ 18.58 Punkte

๐Ÿ“Œ YouTube on Restricted Mode Blocking LGBTQ+ Content: Our System Is Flawed


๐Ÿ“ˆ 18.58 Punkte

๐Ÿ“Œ Mozilla Firefox Will Begin Blocking Extensions in Private Mode


๐Ÿ“ˆ 18.58 Punkte

๐Ÿ“Œ CVE-2016-5238 | QEMU Non-DMA Mode hw/scsi/esp.c get_cmd memory corruption (USN-3047-1 / Nessus ID 94758)


๐Ÿ“ˆ 16.31 Punkte

๐Ÿ“Œ QEMU Non-DMA Mode hw/scsi/esp.c get_cmd Denial of Service


๐Ÿ“ˆ 16.31 Punkte

๐Ÿ“Œ QEMU Non-DMA Mode hw/scsi/esp.c get_cmd Denial of Service


๐Ÿ“ˆ 16.31 Punkte

๐Ÿ“Œ Chrome Will Start Marking HTTP Sites In Incognito Mode As Non-Secure In October


๐Ÿ“ˆ 16.31 Punkte

๐Ÿ“Œ DEF CON Safe Mode Aerospace Village - Understanding Space Through a Cybersecurity Lens


๐Ÿ“ˆ 15.11 Punkte

๐Ÿ“Œ Gemini 1.5 Pro Now Available in 180+ Countries; With Native Audio Understanding, System Instructions, JSON Mode and More


๐Ÿ“ˆ 15.11 Punkte

๐Ÿ“Œ Gemini 1.5 Pro Now Available in 180+ Countries; with Native Audio Understanding, System Instructions, JSON Mode and more


๐Ÿ“ˆ 15.11 Punkte

๐Ÿ“Œ Understanding Google Chrome Lite Mode


๐Ÿ“ˆ 15.11 Punkte











matomo