Automotive and Embedded Networks

cancel
Showing results for 
Search instead for 
Did you mean: 

I need to now the byteslength in ncRead

 
I use nationals PCI CAN and had created a Read and Write Frame API in Visual c++. When I read the Can bus I got the hole message and everything is fine. The problem is that the can bus I read from send message with different byte lengths and I need to now how many bytes there is in every message? I now that this information is in the messages Frame but I can´t get it. Please help me!
 
 
//Olssoninc
 
0 Kudos
Message 1 of 11
(6,333 Views)

Hi,

See the attached example for how to get the number of bytes.

DirkW

0 Kudos
Message 2 of 11
(6,320 Views)
Thank you. but your example was for ncReadMult! It must be any way to read the nr. of bytes with ncRead? A use the example objekt to objekt in the ni examples files.
 
//Anders
0 Kudos
Message 3 of 11
(6,312 Views)

Hi Anders,

You can find details in the "NI-CAN Hardware and Software Manual", Chapter 11: "Frame API for C":

1. If you use a CAN Object, "Data always contains Data Length valid bytes, where Data Length was configured using ncConfig."(p.11-81)

2. If you use a CAN Network Interface Object, the field 'DataLength' in the structure NCTYPE_CAN_STRUCT "Returns the number of data bytes received in the frame. This specifies the number of valid data bytes in Data." (p.11-74).

So you seem to have to use a CAN Network Interface Object instead of a CAN Object.

Yuri

Message Edited by YuGen on 11-01-2006 12:11 PM

0 Kudos
Message 4 of 11
(6,310 Views)

OK. I have trying to convert my receive objekt from a CAN Objekt to a CAN Network Interface Objekt but it still dosent work. I got the struct for Network objekt but the only information in that struct is the same as the CAN objekt (time and data). So wath is wrong when I don´t get the data length information?

 

//Olssoninc

0 Kudos
Message 5 of 11
(6,284 Views)

Anders,

> I got the struct for Network objekt but the only information in that struct is the same as ...

With a CAN object you used this structure:

typedef  struct {
   NCTYPE_ABS_TIME        Timestamp;
   NCTYPE_UINT8           Data[8];
} NCTYPE_CAN_DATA_TIMED;

With a  CAN Network Interface Object you should use this structure:

typedef  struct {
   NCTYPE_ABS_TIME         Timestamp;
   NCTYPE_CAN_ARBID        ArbitrationId;
   NCTYPE_UINT8            FrameType;
   NCTYPE_UINT8            DataLength;
   NCTYPE_UINT8            Data[8];
} NCTYPE_CAN_STRUCT;

 

0 Kudos
Message 6 of 11
(6,273 Views)

Hello!

I have try to use that struct but I don´t ArbitrationId, Frametype and Datalength dosent get any information just some bytes from the data array. It is like the compiler use CAN objekt even when a use the network Interface frame struct.

//Olssoninc

0 Kudos
Message 7 of 11
(6,262 Views)
It  is hard to discuss the code I didn't see...
 
> ArbitrationId, Frametype and Datalength dosent get any information just
> some bytes from the data array.
 
If so, you seem to use a CAN Object, not a CAN Network Interface Object.
 
Did you really open a Network Object? Did you use an object name "CANx" instead of "CANx::STDy"?.
 
 
0 Kudos
Message 8 of 11
(6,256 Views)

Here is my CAN read/write function!

 

 

void CCaNREADERView::ReadCAN(unsigned char *byte,char *cReadId,char *cWriteId)
{
 char csCANStdWrite[14]="CAN1::STD";
 strcat_s(csCANStdWrite,14,cWriteId);
 char csCANStdRead[14]="CAN1::STD";
 strcat_s(csCANStdRead,14,cReadId);
 NCTYPE_STATUS           Status;
 NCTYPE_STATE            State;
 NCTYPE_CAN_DATA         Transmit;
 NCTYPE_CAN_STRUCT Receive;
 NCTYPE_ATTRID           AttrIdList[8];
 NCTYPE_UINT32           AttrValueList[8];


   AttrIdList[0] =         NC_ATTR_BAUD_RATE;  
   AttrValueList[0] =      50000;

   AttrIdList[1] =         NC_ATTR_START_ON_OPEN;
   AttrValueList[1] =      NC_TRUE;

   AttrIdList[2] =         NC_ATTR_READ_Q_LEN;
   AttrValueList[2] =      0;
   AttrIdList[3] =         NC_ATTR_WRITE_Q_LEN;
   AttrValueList[3] =      0;

   AttrIdList[4] =         NC_ATTR_CAN_COMP_STD;
   AttrValueList[4] =    NC_CAN_ARBID_NONE;
   AttrIdList[5] =         NC_ATTR_CAN_MASK_STD;
   AttrValueList[5] =      NC_CAN_MASK_STD_DONTCARE;
   AttrIdList[6] =         NC_ATTR_CAN_COMP_XTD;
   AttrValueList[6] =      NC_CAN_ARBID_NONE;
   AttrIdList[7] =         NC_ATTR_CAN_MASK_XTD;
   AttrValueList[7] =      NC_CAN_MASK_XTD_DONTCARE;
 
   Status = ncConfig("CAN0", 8, AttrIdList, AttrValueList);
   PrintStat(Status, "ncConfig CAN0"); 
 
   Status = ncConfig("CAN1", 8, AttrIdList, AttrValueList);
   PrintStat(Status, "ncConfig CAN1"); 

   AttrIdList[0] =         NC_ATTR_COMM_TYPE;
   AttrValueList[0] =      NC_CAN_COMM_RX_PERIODIC;//NC_CAN_COMM_RX_UNSOL;

   AttrIdList[1] =         NC_ATTR_PERIOD;
   AttrValueList[1] =      NC_DURATION_10SEC;

 AttrIdList[2] =         NC_ATTR_CAN_DATA_LENGTH;
 AttrValueList[2] =      3;

   AttrIdList[3] =         NC_ATTR_CAN_TX_RESPONSE;  
   AttrValueList[3] =      NC_FALSE;

   AttrIdList[4] =         NC_ATTR_RX_CHANGES_ONLY;
   AttrValueList[4] =      NC_FALSE;

   AttrIdList[5] =         NC_ATTR_READ_Q_LEN;
   AttrValueList[5] =      0;
   AttrIdList[6] =         NC_ATTR_WRITE_Q_LEN;
   AttrValueList[6] =      0;

   Status = ncConfig(csCANStdRead, 7, AttrIdList, AttrValueList);
   PrintStat(Status, "ncConfig CAN0::1224 (receiving)"); 

   AttrIdList[0] =         NC_ATTR_COMM_TYPE;
   AttrValueList[0] =      NC_CAN_COMM_TX_PERIODIC;

   AttrIdList[1] =         NC_ATTR_BKD_PERIOD;
   AttrValueList[1] =      NC_DURATION_1SEC;

   AttrIdList[2] =         NC_ATTR_CAN_DATA_LENGTH;
   AttrValueList[2] =     7;  //Length of Transmitting mes

   AttrIdList[3] =         NC_ATTR_CAN_TX_RESPONSE;  
   AttrValueList[3] =      NC_FALSE;

   AttrIdList[4] =   NC_ATTR_RX_CHANGES_ONLY;
   AttrValueList[4] =  NC_FALSE;

 AttrIdList[5] =         NC_ATTR_READ_Q_LEN;
 AttrValueList[5] =      0;

  
 AttrIdList[6] =         NC_ATTR_WRITE_Q_LEN;
 AttrValueList[6] =      0;
 
 
 Status = ncConfig(csCANStdWrite, 7, AttrIdList, AttrValueList);
 PrintStat(Status, "ncConfig CAN1::1224 (transmitting)"); 

 Status = ncOpenObject(csCANStdRead, &RxHandle);
 PrintStat(Status, "ncOpenObject CAN0::1224 (receiving)");
  
 
 Status = ncOpenObject(csCANStdWrite, &TxHandle);
 PrintStat(Status, "ncOpenObject CAN1::STD1224 (transmitting)");
   
 Transmit.Data[0] = 0x00;
 Transmit.Data[1] = 0x00;
 Transmit.Data[2] = 0x00;
 Transmit.Data[3] = 0x00;
 Transmit.Data[4] = 0x00;
 Transmit.Data[5] = 0x00;
 Transmit.Data[6] = 0x00;
 Transmit.Data[7] = 0x00;

 Status= ncWrite(TxHandle, sizeof(Transmit), &Transmit);
 PrintStat(Status, "ncWrite");
 

     Status = ncWaitForState(RxHandle,
               (NC_ST_READ_AVAIL | NC_ST_ERROR),
               (NC_DURATION_10SEC * 2), &State);
 PrintStat(Status, "ncWaitForState");

 Status= ncRead(RxHandle, sizeof(Receive), &Receive);
 
 for(int i=0;i<8;i++)
  byte[i]=Receive.Data[i];

 Status = ncCloseObject(TxHandle);
 PrintStat(Status, "ncCloseObject CAN1::STD5 (transmitting object)");

 Status = ncCloseObject(RxHandle);
 PrintStat(Status, "ncCloseObject CAN0::STD5 (receiving object)");
}

0 Kudos
Message 9 of 11
(6,252 Views)

Ok I use CANx::STDy is it wrong? where did they write about that?

 

//Anders

0 Kudos
Message 10 of 11
(6,255 Views)