Hi,
I am unable to write and read on CAN bus using mangOH red and TALON IOT daughter board. I have followed the steps given here: Running the MCP2515 CAN driver for mangOH Red · mangOH/mangOH Wiki · GitHub.
I am able to run the start_can.sh :
Can0 is listed when I do ip l :
o/p of ifconfig can0:
The can_connect, can_send and can_receive functions are as follows:
int can_connect(const char *interface)
{
struct sockaddr_can addr;
struct ifreq ifr;
int sock_buf_size;
sock_buf_size = 1000;
// if interface string is long we will cut it
if(strlen(interface)>INTERFACE_NAME_SIZE)
{
memcpy(name,interface,INTERFACE_NAME_SIZE);
name[INTERFACE_NAME_SIZE-1]='\0';
}
else
{
strcpy(name,interface);
}
// SOCKET FOR READING
if ((sdr = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
LE_INFO("SOCKET ERROR");
return SOCKET_ERROR;
}
addr.can_family = PF_CAN;
strcpy(ifr.ifr_name, name);
if (ioctl(sdr, SIOCGIFINDEX, &ifr) < 0)
{
LE_INFO("SOCK_IOCTL_ERROR");
return SOCK_IOCTL_ERROR;
}
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(sdr, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
LE_INFO("SOCK_BINDING_ERROR");
return SOCK_BINDING_ERROR;
}
// Filter
struct can_filter rfilter[7]; int i = 0;
rfilter[i].can_id = 0x7E8;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
// rfilter[i].can_id = CHG_TX_ACT;
// rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
// rfilter[i].can_id = BMS_CHRGR_CTL;
// rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
// rfilter[i].can_id = BMS_SOC_DATA;
// rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
// rfilter[i].can_id = BMS_BATTP_DATA;
// rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
// rfilter[i].can_id = BMS_BATP_DATA;
// rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
// rfilter[i].can_id = BMS_BATU_DATA;
// rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
setsockopt(sdr, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
// SET SOCKET BUFFER SIZE
setsockopt(sdr, SOL_CAN_RAW, SO_RCVBUF, (char *)&sock_buf_size, sizeof(sock_buf_size));
// SOCKET FOR WRITING
if ((sdw = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
LE_INFO("SOCKET ERROR");
return SOCKET_ERROR;
}
addr.can_family = PF_CAN;
strcpy(ifr.ifr_name, name);
if (ioctl(sdw, SIOCGIFINDEX, &ifr) < 0)
{
LE_INFO("SOCK_IOCTL_ERROR");
return SOCK_IOCTL_ERROR;
}
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(sdw, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
LE_INFO("SOCK_BINDING_ERROR");
return SOCK_BINDING_ERROR;
}
// SET SOCKET BUFFER SIZE
setsockopt(sdw, SOL_CAN_RAW, SO_SNDBUF, (char *)&sock_buf_size, sizeof(sock_buf_size));
//READ SOCKET NON-BLOCKING
int flags;
if (-1 == (flags = fcntl(sdr, F_GETFL, 0))) flags = 0;
fcntl(sdr, F_SETFL, flags | O_NONBLOCK);
return 0;
}
int can_send(void)
{
int nbytesWrite;
// USE SEND STANDARD FRAME
frameWrite.can_id = 0x7E0;
frameWrite.can_id &= CAN_SFF_MASK;
frameWrite.can_dlc = 8;
// strcpy((char *)frameWrite.data, "MGATE");
frameWrite.data[0] = 0x02;
frameWrite.data[1] = 0x01;
frameWrite.data[2] = 0x00;
frameWrite.data[3] = 0x00;
frameWrite.data[4] = 0x00;
frameWrite.data[5] = 0x00;
frameWrite.data[6] = 0x00;
frameWrite.data[7] = 0x00;
if ((nbytesWrite = write(sdw, &frameWrite, sizeof(frameWrite))) != sizeof(frameWrite))
{
canwrite = false;
LE_INFO ("Writing error, nbytesWrite = %d", nbytesWrite);
return SOCK_WRITING_ERROR;
}
canwrite = true;
return 0;
}
int can_receive(void)
{
nbytesReceive = 1;
while (nbytesReceive != 0)
{
if ((nbytesReceive = read(sdr, &frameRead, sizeof(frameRead))) != sizeof(frameRead))
{
LE_INFO(“Socket reading error. nbytesReceive = %d”, nbytesReceive);
return SOCK_READING_ERROR;
}
else
{
LE_INFO(“Receive message %X %X %X %X %X %X %X %X %X”, frameRead.can_id, frameRead.data[0], frameRead.data[1], frameRead.data[2], frameRead.data[3]
, frameRead.data[4], frameRead.data[5], frameRead.data[6], frameRead.data[7]);
}
}
return 0;
}
int can_disconnect(void)
{
close(sdw);
close(sdr);
return 0;
}
From component init I call the like this:
COMPONENT_INIT
{
can_connect(“can0”);
LE_INFO(“Sending CAN data”);
can_send();
LE_INFO(“Receiving CAN data”);
can_receive();
}
The messages are not being transmitted on CAN lines and while reading I am getting nbytesReceive as -1 i.e the o/p of read function.
I have been stuck on this key project for quite some time now. Would really appreciate any help.
@asyal @dfrey
Regards,
Devanshu Agarwal