Bonjour,

je rencontre quelques difficultés à utiliser la librairie SocketCAN. J'ai tout d'abord développé mon programme en utilisant le socket can en mode raw et tout allait bien. Mais comme j'ai décidé d'envoyer des trames CAN de façon cyclique, je me suis dit que ce serait une bonne idée de passer en mode BCM. Mais la c'est une autre affaire.

J'arrive a créer mon socket et à écrire la configuration pour la réception de trames mais je ne lis jamais rien et ça que la lecture soit bloquante ou pas. J'ai beau vérifier mon code avec ce aue je trouve sur les divers tuto, mais rien a faire. Si je reviens en mode raw, cela fonctionne a nouveau. Ca fait 2 jours que je bataille avec ça... Voici le dernier bout de code que j'ai fait pour tester:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
int CAN_init_can (can_t* CanStruct)
 {
	int s, flags, iNbytes;
	struct sockaddr_can addr;
    struct ifreq ifr;
 
 
	//if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
    if((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0)
	{
		perror("Error while opening socket\n");
		return -1;
	}
 
	strcpy(ifr.ifr_name, CanStruct->sCanPort );
 
    if(ioctl(s, SIOCGIFINDEX, &ifr) < 0)
	{
		printf("ioctl error: %s\n", strerror(errno));
		perror(strerror(errno));
		return -1;
	}
 
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
 
    /* Set socket to non-blocking */
    flags = fcntl(s, F_GETFL, 0);
    if (flags < 0)
    {
    	perror("fcntl: F_GETFL");
        return errno;
    }
 
    if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
    {
    	perror("fcntl: F_SETFL");
    	return errno;
    }
 
#ifdef DEBUG
	printf("Can ID: %s at index %d\n", CanStruct->sCanPort, ifr.ifr_ifindex);
#endif
 
	//if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
	if(connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
	{
		printf("Error in socket bind");
		return -1;
	}
 
	struct can_msg_BCM CanBcmFrameRx;
 
	/*CanStruct->*/CanBcmFrameRx.msg_head.opcode 	= RX_SETUP;
	/*CanStruct->*/CanBcmFrameRx.msg_head.can_id 	= 0x18FEF100;
	/*CanStruct->*/CanBcmFrameRx.msg_head.flags 	= 0;
	/*CanStruct->*/CanBcmFrameRx.msg_head.nframes 	= 0;
	iNbytes = write(s, &/*CanStruct->*/CanBcmFrameRx, sizeof(struct can_msg_BCM));
	if( iNbytes < 0)
	{
		printf("write error init: RX_SETUP");
		//return errno;
	}
	iNbytes = read(s, &/*CanStruct->*/CanBcmFrameRx, sizeof(struct can_msg_BCM));
        if (iNbytes < 0)
		printf ("read error %d", errno);
 
 
/*	CanStruct->CanBcmFrameTx.msg_head.opcode 	= TX_SETUP;
	CanStruct->CanBcmFrameTx.msg_head.can_id 	= 0x124;
	CanStruct->CanBcmFrameTx.msg_head.flags 	= 0;
	CanStruct->CanBcmFrameTx.msg_head.nframes 	= 0;
	CanStruct->CanBcmFrameTx.canframe.can_id	 	= 0x18fef999;
	CanStruct->CanBcmFrameTx.canframe.can_dlc		= 2;
	CanStruct->CanBcmFrameTx.canframe.data[0]		= 0xFF;
	CanStruct->CanBcmFrameTx.canframe.data[1]		= 0xFF;
 
	if( write(s, &CanStruct->CanBcmFrameTx, sizeof(struct can_msg_BCM)) < 0)
	{
		printf("write error init: TX_SETUP %d", errno);
		//return errno;
	}
	iNbytes = read(s, &CanStruct->CanBcmFrameTx, sizeof(struct can_msg_BCM));
*/
	return s;
 }
Le code erreur de la lecture est 11 ce qui correspond à:
EAGAIN: The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block.

Dernier detail, je developpe dans une VM, sous linux Ubuntu avec Eclipse (on sais jamais si l'info peur etre utile...)

Merci d'avance pour votre aide.