//wdmcan.cpp - версия 1.04


//#include "stdafx.h"
#include<stdio.h>
#include<windows.h>
#include "wdmcan.h"
#pragma hdrstop

HANDLE CANhandle[MAX_CAN_NUMBER+1]={0},_CANhandle=0,_CANoldhandle=0;
USHORT SubTmk;
HANDLE _ahVCAN4Event[MAX_CAN_NUMBER+1];


unsigned short  CANOpen (int number)
{
	char canName[50];
	char cc = 0x5c;
	sprintf (canName,"%c%c.%cCAN200PCDevice%d",cc,cc,cc,number);
	if(number>MAX_CAN_NUMBER) return 1;

     CANhandle[number] = CreateFile(canName,
                   GENERIC_READ | GENERIC_WRITE,
                   0,
                   NULL,
                   OPEN_EXISTING,
                   FILE_ATTRIBUTE_NORMAL,
                   NULL
                   );
     if (CANhandle[number] != INVALID_HANDLE_VALUE)
		{
		SelectCAN(number);
		return 0;
		}
     else
		{
		return 1;
		}
}

void            CANClose (int number)
{
      CloseHandle(CANhandle[number]);
	  CANhandle[number]=0;
}

void  chBaseAddress(unsigned int Base)
{
		switch (Base)
        {
              case 1: SubTmk = 1;
			  break;
              case 2: SubTmk = 2;
			  break;
        }
}
unsigned short   SetWorkMode(unsigned short Mode)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Mode;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetWorkMode, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
unsigned short   GetWorkMode(void)
{
  WORD _awCANInBuf[1], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_GetWorkMode, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
void            HardReset(void)
{
  WORD _awCANInBuf[1], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle,IOCTL_CAN200PC_HardReset, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
}
void            SetDriverMode(unsigned short Mode)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Mode;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetDriverMode, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
}
unsigned short   SetCANSpeed(unsigned int Speed)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Speed;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetCANSpeed, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
unsigned short   GetStatus(void)
{
  WORD _awCANInBuf[1], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_GetStatus, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
//unsigned short   GetInterruptSource(void);
void            SetInterruptSource(unsigned short Source)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Source;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetInterruptSource, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
}
unsigned short   SetTxBuffer(pRX_TX_Buffer Buffer)
{
  WORD _awCANInBuf[20], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  memcpy(&_awCANInBuf[1],Buffer,sizeof(RX_TX_Buffer));
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetTxBuffer, _awCANInBuf, sizeof(RX_TX_Buffer)+2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
unsigned short   GetRxBuffer(pRX_TX_Buffer Buffer)
{
  WORD _awCANInBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_GetRxBuffer, _awCANInBuf, 2, (LPVOID)Buffer, sizeof(RX_TX_Buffer), &_cbCANRet, NULL);
  return 0;
}
void SetCommand(unsigned short Command)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Command;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetCommand, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
}
unsigned short   B_SetInputFilter(unsigned short Filter1,unsigned short Filter2)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[2] = Filter1;
  _awCANInBuf[1] = Filter2;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_B_SetInputFilter, _awCANInBuf, 6, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
unsigned short   P_SetRxErrorCounter(unsigned short Counter)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Counter;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_SetRxErrorCounter, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
int  P_GetRxErrorCounter(void)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_GetRxErrorCounter, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  (short)(_awCANOutBuf[0]);
}
unsigned short   P_SetTxErrorCounter(unsigned short Counter)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Counter;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_SetTxErrorCounter, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
int  P_GetTxErrorCounter(void)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_GetTxErrorCounter, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  (short)(_awCANOutBuf[0]);
}
unsigned short   P_SetErrorWarningLimit(unsigned short Limit)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = Limit;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_SetErrorWarningLimit, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}
int  P_GetErrorWarningLimit(void)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_GetErrorWarningLimit, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  (short)(_awCANOutBuf[0]);
}
int  P_GetArbitrationLostCapture(void)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_GetArbitrationLostCapture, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  (short)(_awCANOutBuf[0]);
}
int  P_GetRxMessageCounter(void)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_GetRxMessageCounter, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  (short)(_awCANOutBuf[0]);
}
int  P_GetErrorCode(void)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_GetErrorCode, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  (short)(_awCANOutBuf[0]);
}
unsigned short  P_SetInputFilter(unsigned short A1, unsigned short A2, unsigned short A3, unsigned short A4, unsigned short A5, unsigned short A6, unsigned short A7, unsigned short A8, unsigned short A9)
{
  WORD _awCANInBuf[10], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = A1;
  _awCANInBuf[2] = A2;
  _awCANInBuf[3] = A3;
  _awCANInBuf[4] = A4;
  _awCANInBuf[5] = A5;
  _awCANInBuf[6] = A6;
  _awCANInBuf[7] = A7;
  _awCANInBuf[8] = A8;
  _awCANInBuf[9] = A9;

  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_P_SetInputFilter, _awCANInBuf, 20, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}


void  SetCANReg(unsigned short A1, unsigned short A2)
{
  WORD _awCANInBuf[3], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = A1;
  _awCANInBuf[2] = A2;
  
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_SetCANReg, _awCANInBuf, 6, _awCANOutBuf, 2, &_cbCANRet, NULL);
}

unsigned short GetCANReg(unsigned short A1)
{
  WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = A1;
  
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_GetCANReg, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}


void ResetRequest(unsigned short A1)
{
WORD _awCANInBuf[2], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1] = A1;
  
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_ResetRequest, _awCANInBuf, 4, _awCANOutBuf, 2, &_cbCANRet, NULL);
}

unsigned short	TestResetRequest(void)
{
WORD _awCANInBuf[1], _awCANOutBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_TestResetRequest, _awCANInBuf, 2, _awCANOutBuf, 2, &_cbCANRet, NULL);
  return  _awCANOutBuf[0];
}



void DefEvent( HANDLE hEvent, BOOL fEventSet )
{
  int CANNum;	
  WORD _awCANInBuf[5], _awCANOutBuf[1];
  DWORD _cbCANRet;
  DWORD hVxDEvent;
  
  if (hEvent != 0)
  {
    for (CANNum = 0; CANNum <= MAX_CAN_NUMBER; ++CANNum)
    {
      if (hEvent == _ahVCAN4Event[CANNum])
        break;
    }
    if (CANNum > MAX_CAN_NUMBER)
    {
      hVxDEvent = (DWORD)hEvent;
      _ahVCAN4Event[SubTmk-1] = hEvent;
    }
  }
  else
  {
    hVxDEvent = 0;
    _ahVCAN4Event[SubTmk-1] = 0;
  }
  _awCANInBuf[0] = SubTmk;
  _awCANInBuf[1]=LOWORD(hVxDEvent);
  _awCANInBuf[2]=HIWORD(hVxDEvent);
  _awCANInBuf[3]=LOWORD(fEventSet);
  _awCANInBuf[4]=HIWORD(fEventSet);
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_DefEvent, _awCANInBuf, 10, _awCANOutBuf, 0, &_cbCANRet, NULL);
}


unsigned short   GetEventData(pTEventData Buffer)
{
  WORD _awCANInBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_GetEventData, _awCANInBuf, 2, (LPVOID)Buffer, sizeof(TEventData), &_cbCANRet, NULL);
  return 0;
}


unsigned short   GetConfig(pTCAN_VPDData Buffer)
{
  WORD _awCANInBuf[1];
  DWORD _cbCANRet;
  _awCANInBuf[0] = SubTmk;
  DeviceIoControl(_CANhandle, IOCTL_CAN200PC_GetConfig, _awCANInBuf, 2, (LPVOID)Buffer, sizeof(TCAN_VPDData), &_cbCANRet, NULL);
  return 0;
}

int SelectCAN(int number)
{
if ((CANhandle[number] !=0)&&(CANhandle[number] != INVALID_HANDLE_VALUE))
	{
	if((_CANhandle!= INVALID_HANDLE_VALUE)&&(_CANhandle!=0))
		_CANoldhandle = _CANhandle;
	_CANhandle = CANhandle[number];
	return 0;
	}
else
	return 1;
}

