Hi,
There are two ways to transmit a Frame with NI-CAN. The first is using the Network Interface Object (Port ex: CAN0) directly as follows:
/* Configure and open the CAN Network Interface Object */
AttrIdList[0] = NC_ATTR_BAUD_RATE;
AttrValueList[0] = Baudrate;
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] = 1;
AttrIdList[4] = NC_ATTR_CAN_COMP_STD;
AttrValueList[4] = 0;
AttrIdList[5] = NC_ATTR_CAN_MASK_STD;
AttrValueList[5] = NC_CAN_MASK_STD_DONTCARE;
AttrIdList[6] = NC_ATTR_CAN_COMP_XTD;
AttrValueList[6] = 0;
AttrIdList[7] = NC_ATTR_CAN_MASK_XTD;
AttrValueList[7] = NC_CAN_MASK_XTD_DONTCARE;
ncConfig(Interface, 8, AttrIdList, AttrValueList);
ncOpenObject(Interface, &TxHandle);
/* Populate data array */
for (i=0; i<8; i++)
{
data[i] = i*2;
}
memcpy (Transmit.Data, data, data_length);
Transmit.DataLength = data_length;
Transmit.IsRemote = 0;
ncWrite(TxHandle, sizeof(Transmit), &Transmit);
The second way is using a CAN Object for a particular ID (ex: CAN0::STD5), in this case 11 bit ID 5 on Port CAN0 as follows:
/* Configure the CAN Network Interface Object */ no open
AttrIdList[0] = NC_ATTR_BAUD_RATE;
AttrValueList[0] = Baudrate;
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] = 0;
AttrIdList[5] = NC_ATTR_CAN_MASK_STD;
AttrValueList[5] = NC_CAN_MASK_STD_DONTCARE;
AttrIdList[6] = NC_ATTR_CAN_COMP_XTD;
AttrValueList[6] = 0;
AttrIdList[7] = NC_ATTR_CAN_MASK_XTD;
AttrValueList[7] = NC_CAN_MASK_XTD_DONTCARE;
ncConfig(Interface, 8, AttrIdList, AttrValueList);
//Configure and open CAN Object for periodic transmission;
AttrIdList[0] = NC_ATTR_COMM_TYPE;
AttrValueList[0] = NC_CAN_COMM_TX_PERIODIC;
AttrIdList[1] = NC_ATTR_BKD_PERIOD;
AttrValueList[1] = period;
AttrIdList[2] = NC_ATTR_CAN_DATA_LENGTH;
AttrValueList[2] = data_length;
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] = 1;
//Format the Interface and ID to a String (ObjectID) like CAN0::STD5
strcat(Interface,id_string);
strcpy(ObjectID,Interface);
ncConfig (ObjectID, 7, AttrIdList, AttrValueList);
ncOpenObject (ObjectID, &TxHandle);
/* Populate data array */
for (i=0; i<8; i++)
{
data[i] = 'D';
}
/* Copy the CAN Data Array with the specific length (data_length) to the DATA inside Transmit */
memcpy (Transmit.Data, data, data_length);
ncWrite(TxHandle, sizeof(Transmit), &Transmit);
In both cases you need to specify the data bytes and the data length.
The ID configures with the write on the Network Interface and with the configure on the Object.
For both ways there are examples shipping with NI-CAN. You can find them with the //National Instruments/NI-CAN folder.
Hope that helps.
DirkW