Unable to Rx and Tx on CAN bus using MangOH red board

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 :
image

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

Hi,

An update:

I am seeing the data on CAN bus when I have connected a sniffer in parallel on CAN lines. So, the write can_send function is working correctly and message is being written and a valid response message is also seen on sniffer. Just while using can_receive function that I have posted in the earlier post, I am not able to receive the data bytes.

I am now able to read the messages but facing below issues now :

1. Losing frames while receiving :

I have a CAN simulator with me which transmits 5 can frames when I send a particular can frame in request. Out of the five frames sent I am only able to receive 3 frames successfully on mangOH red board. I see the following statistics after reception is done.

Following is the snippet of my can_receive function:

2. Receiving the same message multiple times :

While reading I am getting the same can_frame thrice with the same timestamp when I call the recvfrom function. Refer the below logs:

3. Not able to download linux distribution source :

I wanted to add can-utils in my linux image. I am unable to download the linux distribution source from the sierra wireless website - https://source.sierrawireless.com/resources/airprime/software/wp76xx/wp76xx-firmware-release-13,-d-,1-components/#sthash.HVrsgnL0.dpbs

I am getting following error. I have tried this multiple times in the past few weeks. Getting Not found error :

Please help me. Me and my team have been stuck on this for quite some time now. We will have to switch to some other vendor’s 4G modem if this doesn’t work. Would really appreciate any help that guys can provide.

Regards,
Devanshu Agarwal

Hi @devanshuagarwal
I am working these days on the same things. I am in the same situation as you, ie can_send works fine, but receive does not. You come have you solved? Is it a problem in the software?

As for yocto, I can tell you that it is a fundamental step to install the canutils library. Fortunately I have the .cwe file that I downloaded before the soto crashed (in March). I hope you have solved this aspect.