CAN Message Data Problem

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time

CAN Message Data Problem

Post by gritt »

Can anyone help me with this?

I am using Microchip's PICDEM CAN-LIN 2 with 2 x 18F4680 using internal CAN.
I have written two files; a send node and a receive node. The send node increments a value "CAN_Message" by 1 every 500ms, ouputs it to LEDs on port D and using the CAN settxdata macro, sets it as the first byte of data (d0).
The recieve node is set to accept all messages, and using the get CAN getrxdata macro takes byte zero and outputs it to leds on port D.
Both nodes are set to use the internal CAN. The system works fine over Vnet simulation however not when put on the hardware. The send node works fine (the leds change at the correct rate) but the recieve node does not work and just shows zero or some other constant number on the leds.
I have proved that the hardware is capable of actually sending CAN messages by setting the message length to the incremented value in the send node. The receive node gets the message length using getrxdata count and outports it to port d. This works fine.
So why can't the receive node get the data from the message?

Any ideas greatly appreciated.
Attachments
CAN receive demo board v1.fcf
(20.23 KiB) Downloaded 263 times
CAN send demo board v1.fcf
(11 KiB) Downloaded 255 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: CAN Message Data Problem

Post by Benj »

Hello,

Thats very odd that the data count is being collected but the actual data is not getting through.

Can you test if the checkRx function ever returns true?

I tested the internal CAN code using a 18F2680 so this should be 100% compatible with your devices.

You could try the following C code instead of the CheckRX function as this should highlight if there is any problems with the CheckRX function.

Code: Select all

char buffer = 0;
char retval = 0;
if (buffer == 0)
{
	cancon = cancon & 0xE0;
	//cancon = cancon | 0x0C;		//Rx Buffer 0
}
else
{
	cancon = cancon & 0xE0;
	cancon = cancon | 0x0A; 	//Rx Buffer 1
}

if (ts_bit(rxb0con, RXFUL))
{
	CAN_MSG_DLC = rxb0dlc & 0b00001111;	// Retrieve message length.
	CAN_MSG_ID[0] = rxb0sidh;
	CAN_MSG_ID[1] = rxb0sidl;
	if(CAN_MSG_DLC > 0)
	{
		CAN_MSG_DATA[0] = rxb0d0;
		if(CAN_MSG_DLC > 1)
		{
			CAN_MSG_DATA[1] = rxb0d1;
			if(CAN_MSG_DLC > 2)
			{
				CAN_MSG_DATA[2] = rxb0d2;
				if(CAN_MSG_DLC > 3)
				{
					CAN_MSG_DATA[3] = rxb0d3;
					if(CAN_MSG_DLC > 4)
					{
						CAN_MSG_DATA[4] = rxb0d4;
						if(CAN_MSG_DLC > 5)
						{
							CAN_MSG_DATA[5] = rxb0d5;
							if(CAN_MSG_DLC > 6)
							{
								CAN_MSG_DATA[6] = rxb0d6;
								if(CAN_MSG_DLC > 7)
								{
									CAN_MSG_DATA[7] = rxb0d7;
								}
							}
						}
					}
				}
			}
		}
	}
	cr_bit(rxb0con, RXFUL);
}
else
	retval 0;

retval = 1;

gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time

Re: CAN Message Data Problem

Post by gritt »

Hi Ben,

thanks for your help. I wrote a program for the receive node that turned on led d0 every time that the checkRx function returned a non zero value. The send node was set to send messages at a rate of 0.5Hz, and the receive node led flashed at this rate, indicating that the checkRx macro was working. Still no luck with getting data from the message using getrxdata macro though. What else might the problem be?

James

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: CAN Message Data Problem

Post by Benj »

Hello James,

I may be onto something here. I have customized the set_tx_data function and hopefully if you recompile and program the transmitter you should now be able to receive data.

Let me know how you get on with this and if it works then I will look at a way to update the component C file.

Otherwise I'll see if there's anything else I can try.
Attachments
CAN send demo board v1.1.fcf
(14.73 KiB) Downloaded 248 times

gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time

Re: CAN Message Data Problem

Post by gritt »

Hi Ben,

I'm afraid it still doesn't work. Port D on the rx shows a steady 8, or if I swap tx and rx between pics it shows a steady 25, which is confusing.
I'm working on some new hardware to try it on but it won't be ready until next week.
Please let me know if you have any other ideas.

Thanks

James

gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time

Re: CAN Message Data Problem

Post by gritt »

Hi Ben,

sorry for the long delay, I have finally got round to making the new hardware to test this application and it still does not work.
I know that the tx node is sending the correct data at the correct speed as I have connected it to a pc using an NI can interface and all is ok there.
The rx node still does not show the received data, I know that it's receiving the message as I have set an LED to flash every time CheckRx returns a non zero value. This LED flashes at the correct rate. I checked the code for the GetRxData macro and I'm not sure if it is correct. Can you confirm what should be in there, all I have is:

if (index > 7)
(
return (0);
)
return (CAN_MSG_DATA(index);

Is it worth trying some custon c code instead of CheckRx and GetRxData?
For reference I'm using the embedded can so I'm not sure if that will affect this.

Let me know what you think

regards

James

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: CAN Message Data Problem

Post by Benj »

Hi James,

It might be worth using the custom code feature on the CheckRX macro. This macro is responsible for detecting the incoming message and then stripping out the data.

I would edit the code to look something like follows.

Code: Select all

	//returns 1 if the buffer is full, 0 otherwise
	char rx_status;
	char status_mask;
	char retval = 0;

	if (buffer > CAN_MAX_RX_BUF)
	{
	   return (0);
	}

#ifdef USE_INT_CAN

	if (buffer == 0)
	{
		cancon = cancon & 0xE0;
		//cancon = cancon | 0x0C;		//Rx Buffer 0
	}
	else
	{
		cancon = cancon & 0xE0;
		cancon = cancon | 0x0A; 	//Rx Buffer 1
	}

	if (ts_bit(rxb0con, RXFUL))
	{
		CAN_MSG_DLC = rxb0dlc & 0b00001111;	// Retrieve message length.
		CAN_MSG_ID[0] = rxb0sidh;
		CAN_MSG_ID[1] = rxb0sidl;

                             //BR added ******************************
                             trisa = 0x00;
                             porta = CAN_MSG_DLC ;

		if(CAN_MSG_DLC > 0)
		{
			CAN_MSG_DATA[0] = rxb0d0;
			if(CAN_MSG_DLC > 1)
			{
				CAN_MSG_DATA[1] = rxb0d1;
				if(CAN_MSG_DLC > 2)
				{
					CAN_MSG_DATA[2] = rxb0d2;
					if(CAN_MSG_DLC > 3)
					{
						CAN_MSG_DATA[3] = rxb0d3;
						if(CAN_MSG_DLC > 4)
						{
							CAN_MSG_DATA[4] = rxb0d4;
							if(CAN_MSG_DLC > 5)
							{
								CAN_MSG_DATA[5] = rxb0d5;
								if(CAN_MSG_DLC > 6)
								{
									CAN_MSG_DATA[6] = rxb0d6;
									if(CAN_MSG_DLC > 7)
									{
										CAN_MSG_DATA[7] = rxb0d7;
									}
								}
							}
						}
					}
				}
			}
		}
		cr_bit(rxb0con, RXFUL);
	}
	else
		return 0;

	retval = 1;

#else

	if (buffer == 1)
	{
	   //status_mask = 0x02;       //if using CAN_ReadStatus
	   status_mask = 0x80;         //if using CAN_ReadRXStatus
	} else {
	   //status_mask = 0x01;       //if using CAN_ReadStatus
	   status_mask = 0x40;         //if using CAN_ReadRXStatus
	}

	//rx_status = CAN_ReadStatus();    //if using CAN_ReadStatus
	rx_status = CAN_ReadRXStatus();    //if using CAN_ReadRXStatus

	if ((rx_status & status_mask) != 0)
	{
	   retval = 1;

	   //now read in the id and data info
	   CAN_ReadRX_ID_and_Data(buffer);
	}

#endif

	return (retval);
You can then see how many data bytes are being received. If the data length is being received correctly then you may want to start moving the porta write around to test that the data buffers are being populated correctly.

gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time

Re: CAN Message Data Problem

Post by gritt »

Hi Ben,

I tried adding the code you suggested throughout the macro code and got the following results.
Data count is set as 8 in the send node. All 8 sent bytes are set as the same value which increments by 1 at 0.5hz up to a maximum of 8, at which point it resets to zero. This value is displayed on port D of the send node so that it is obvious to see if the two nodes are in sync and the correct data is being received.

CAN_MSG_DLC shows the value 8, which is good.
All buffers give the correct value, which is also good.

However, I tried using the GetRxData macro again and it gives the value 65.

I was confused by the GetRxData macro which calls for the buffer and index but only uses the index in the code. Is that correct?
What else do you think I should try, maybe writing a custom macro to get data from the buffers?

Any help is greatly appreciated

Thanks

James

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: CAN Message Data Problem

Post by Benj »

Hello James,

Thats really strange that the CheckRX function has the right data but the GetRXData is returning incorrect data. The GetRXData is simply reading from an array that is created by the CheckRX function when it detects a valid incoming message. I know I have had this working great on an internal CAN system so its really strange that its not working for you. You could use the custom code feature of Flowcode to create your own macro to check for incoming messages and then return the data based on the current macros and see if this gets you anywhere.

Post Reply