/*
* NI-CAN送受信処理
*/
#include "NiCANDrv.h"
//=============================================================================
// Static variables
//=============================================================================
static nxSessionRef_t m_SessionRefTx = 0;
static nxSessionRef_t m_SessionRefRx = 0;
/**
* デバイスのオープン処理.
*
* @param [in] pSelectedInterface デバイスのインタフェース
* @param [in] BaudRate ボーレート
* @retval NI_CAN_DRV_SUCCESS 成功
* @retval NI_CAN_DRV_ERROR 失敗
*/
int WINAPI Open(char *pSelectedInterface, u64 BaudRate)
{
char *l_pSelectedDatabase = ":memory:";
char *l_pSelectedCluster = "";
char *l_pSelectedFrameList = "";
nxStatus_t l_Status = 0;
// Create an XNET session in FrameOutStream mode
l_Status = nxCreateSession(l_pSelectedDatabase, l_pSelectedCluster,
l_pSelectedFrameList, pSelectedInterface, nxMode_FrameOutStream,
&m_SessionRefTx);
if (nxSuccess != l_Status)
{
Close();
return NI_CAN_DRV_ERROR;
}
l_Status = nxCreateSession(l_pSelectedDatabase, l_pSelectedCluster,
l_pSelectedFrameList, pSelectedInterface, nxMode_FrameInStream,
&m_SessionRefRx);
if (nxSuccess != l_Status)
{
Close();
return NI_CAN_DRV_ERROR;
}
// We are not using a predefined database, so we need to set the Baud Rate
l_Status = nxSetProperty(m_SessionRefTx, nxPropSession_IntfBaudRate64,
sizeof(u64), &BaudRate);
if (nxSuccess != l_Status)
{
Close();
return NI_CAN_DRV_ERROR;
}
// We are not using a predefined database, so we need to set the Baud Rate
l_Status = nxSetProperty(m_SessionRefRx, nxPropSession_IntfBaudRate64,
sizeof(u64), &BaudRate);
if (nxSuccess != l_Status)
{
Close();
return NI_CAN_DRV_ERROR;
}
return NI_CAN_DRV_SUCCESS;
}
/**
* フレーム書き込み処理.
*
* @param [in] id 送信フレームID
* @param [in] length 送信データ長さ
* @param [in] data 送信データ
* @retval NI_CAN_DRV_SUCCESS 成功
* @retval NI_CAN_DRV_ERROR 失敗
*/
int WINAPI WriteFrame(u32 id, u8 length, char *data)
{
unsigned int i = 0;
u8 l_Buffer[sizeof(nxFrameCAN_t)];
nxFrameVar_t *l_pFrame;
nxStatus_t l_Status;
// Check length
if (length > 8)
{
return NI_CAN_DRV_ERROR;
}
// Get a pointer to the first frame's memory
l_pFrame = (nxFrameVar_t *)l_Buffer;
// Set the frame's parameters (skipping the payload for now).
l_pFrame->Timestamp = 0;
l_pFrame->Flags = 0;
l_pFrame->Identifier = id;
l_pFrame->Type = nxFrameType_CAN_Data;
l_pFrame->PayloadLength = length;
if (l_pFrame->Identifier > 0x7FF)
{
l_pFrame->Identifier |= nxFrameId_CAN_IsExtended;
}
// Set the payload
for (i = 0; i < length; i++)
{
l_pFrame->Payload[i] = data[i];
}
// Write the frame buffer
l_Status = nxWriteFrame(m_SessionRefTx, l_Buffer, sizeof(l_Buffer), 10.0);
if (nxSuccess != l_Status)
{
return NI_CAN_DRV_ERROR;
}
return NI_CAN_DRV_SUCCESS;
}
/**
* フレーム読み出し処理.
*
* @param [out] id 受信フレームID
* @param [out] length 受信データ長さ
* @param [out] data 受信データ
* @retval NI_CAN_DRV_SUCCESS 成功
* @retval NI_CAN_DRV_ERROR 失敗
*/
int WINAPI ReadFrame(u32 *id, u8 *length, char *data)
{
unsigned int i = 0;
u8 l_Buffer[sizeof(nxFrameCAN_t)];
nxFrameVar_t *l_pFrame;
nxStatus_t l_Status;
u32 l_NumBytes = 0;
// Read the received frames into the buffer
l_Status = nxReadFrame(m_SessionRefRx, l_Buffer, sizeof(l_Buffer),
nxTimeout_None, &l_NumBytes);
if (nxSuccess != l_Status)
{
return NI_CAN_DRV_ERROR;
}
if (l_NumBytes < sizeof(l_Buffer))
{
return NI_CAN_DRV_ERROR;
}
l_pFrame = (nxFrameVar_t *)l_Buffer;
// Get ID and length
*id = l_pFrame->Identifier;
*length = l_pFrame->PayloadLength;
// Get the payload
for (i = 0; i < l_pFrame->PayloadLength; i++)
{
data[i] = l_pFrame->Payload[i];
}
return NI_CAN_DRV_SUCCESS;
}
/**
* デバイスのクローズ処理.
*
* @param なし
* @retval NI_CAN_DRV_SUCCESS 成功
* @retval NI_CAN_DRV_ERROR 失敗
*/
int WINAPI Close()
{
nxStatus_t l_Status = 0;
// Clear the XNET session
l_Status = nxClear(m_SessionRefTx);
if (nxSuccess != l_Status)
{
return NI_CAN_DRV_ERROR;
}
return NI_CAN_DRV_SUCCESS;
}
- 1
- 2
前往页