drivers/can_anagate_win32/can_anagate_win32.c
author greg
Tue, 08 Jul 2008 15:14:48 +0200
changeset 494 aa36efce6e78
child 631 08b6b903f84a
permissions -rw-r--r--
add anagate device support in CanFestival
494
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     1
/*
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     3
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     5
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     6
See COPYING file for copyrights details.
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     7
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    12
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    16
Lesser General Public License for more details.
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    17
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    21
*/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    22
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    23
#if defined(WIN32) && !defined(__CYGWIN__)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    24
#define usleep(micro) Sleep(micro%1000 ? (micro/1000) + 1 : (micro/1000))
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    25
#define _CRT_SECURE_NO_DEPRECATE
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    26
#else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    27
#include <stdio.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    28
#include <string.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    29
#include <errno.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    30
#include <fcntl.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    31
#endif
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    32
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    33
#include "can_driver.h"
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    34
#include "AnaGateDllCan.h"
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    35
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    36
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    37
typedef struct SAnaGatePort
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    38
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    39
int		hHandle;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    40
HANDLE	hAnaRecEvent;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    41
HANDLE	hFesticalRecAcknowledge;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    42
Message sMsgBuffer;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    43
struct SAnaGatePort *pNext;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    44
struct SAnaGatePort *pPrev;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    45
} SAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    46
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    47
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    48
SAnaGatePort *pFirstAnaGatePort=NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    49
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    50
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    51
/********* AnaGate API CAN receive callback Funciton  ****************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    52
void WINAPI AnaGateReceiveCallBack (int nIdentifier, const char* pcBuffer, int nBufferLen, int nFlags, int hHandle)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    53
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    54
	SAnaGatePort *pAnaGatePort = pFirstAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    55
   int           i;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    56
	
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    57
	while (pAnaGatePort->hHandle != hHandle )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    58
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    59
		pAnaGatePort = pAnaGatePort->pNext;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    60
		if (pAnaGatePort == pFirstAnaGatePort )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    61
		{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    62
			pAnaGatePort = NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    63
			printf("AnaGateReceiveCallBack (AnaGate_Win32): ERROR: Can't find AnaGatePort-Objekt to the Received Handle %d\n",hHandle);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    64
			return;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    65
		}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    66
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    67
	
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    68
	pAnaGatePort->sMsgBuffer.cob_id =   nIdentifier;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    69
	pAnaGatePort->sMsgBuffer.len= nBufferLen;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    70
	if (nFlags == 2)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    71
	 pAnaGatePort->sMsgBuffer.rtr = 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    72
	else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    73
	 pAnaGatePort->sMsgBuffer.rtr = 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    74
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    75
	for (i = 0 ; i < nBufferLen; i++)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    76
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    77
		pAnaGatePort->sMsgBuffer.data[i] = pcBuffer[i];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    78
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    79
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    80
	SetEvent(pAnaGatePort->hAnaRecEvent);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    81
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    82
	WaitForSingleObject(pAnaGatePort->hFesticalRecAcknowledge,INFINITE);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    83
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    84
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    85
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    86
/*********functions which permit to communicate with the board****************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    87
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    88
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    89
	SAnaGatePort*  pAnaGatePort = (SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    90
   int            i;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    91
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    92
	WaitForSingleObject(			// Wait for receive event
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    93
		pAnaGatePort->hAnaRecEvent, // event handle
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    94
		INFINITE);				    // indefinite wait
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    95
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    96
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    97
	m->cob_id = pAnaGatePort->sMsgBuffer.cob_id;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    98
	m->len	  = pAnaGatePort->sMsgBuffer.len;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    99
	m->rtr	  = pAnaGatePort->sMsgBuffer.rtr;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   100
	for (i = 0 ; i < pAnaGatePort->sMsgBuffer.len; i++)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   101
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   102
	  m->data[i] = pAnaGatePort->sMsgBuffer.data[i];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   103
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   104
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   105
	SetEvent(pAnaGatePort->hFesticalRecAcknowledge);	//Set Acknollede event
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   106
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   107
	return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   108
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   109
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   110
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   111
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   112
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   113
	SAnaGatePort*  pAnaCanPort = (SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   114
	char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   115
	int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   116
	int nMsgTyp;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   117
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   118
	if (m->rtr == 0)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   119
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   120
	  nMsgTyp = 0; //Normal;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   121
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   122
	else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   123
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   124
	  nMsgTyp = 2; //Remote frame;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   125
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   126
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   127
	if ( (nRetCode = CANWrite(pAnaCanPort->hHandle , m->cob_id,(const char*) m->data, m->len, nMsgTyp) ) )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   128
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   129
	  CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   130
	  fprintf(stderr,"canSend_driver (AnaGate_Win32) %s \n",nRetCode);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   131
	  //printf("canSend_driver (AnaGate_Win32) %s \n",nRetCode);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   132
	  return 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   133
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   134
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   135
	return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   136
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   137
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   138
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   139
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   140
int TranslateBaudeRate(char* optarg){
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   141
	if(!strcmp( optarg, "1M"))   return 1000000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   142
    if(!strcmp( optarg, "800K")) return  800000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   143
    if(!strcmp( optarg, "500K")) return  500000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   144
    if(!strcmp( optarg, "250K")) return  250000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   145
    if(!strcmp( optarg, "125K")) return  125000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   146
	if(!strcmp( optarg, "100K")) return  100000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   147
	if(!strcmp( optarg, "50K"))  return   50000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   148
	if(!strcmp( optarg, "20K"))  return   20000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   149
	if(!strcmp( optarg, "10K"))  return   10000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   150
 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   151
	return 0x0000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   152
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   153
/****************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   154
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   155
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   156
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   157
   int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   158
   char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   159
   struct SAnaGatePort*  pAnaGatePort = (struct SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   160
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   161
   if (nRetCode = CANSetGlobals (pAnaGatePort->hHandle, TranslateBaudeRate(baud), 0, 0, 1) ) 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   162
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   163
      CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   164
      fprintf(stderr, "canChangeBaudRate_drive (AnaGate_Win32): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   165
      printf("canChangeBaudRate_drive (AnaGate_Win32): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   166
      return 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   167
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   168
   return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   169
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   170
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   171
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   172
/* To open a connection to AnaGate CAN the s_BOARD board->busname must be 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   173
 the AnaGate IP-Adresse followed from the CAN-Port (A or B) you want to use 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   174
 For example "192.168.1.254:A"
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   175
*/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   176
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   177
CAN_HANDLE canOpen_driver(s_BOARD *board)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   178
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   179
	int PortNr;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   180
	char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   181
	int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   182
	char sIPAddress[16];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   183
	struct SAnaGatePort *pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   184
    char bBusnameValid = TRUE;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   185
	unsigned int nBusnameLen; 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   186
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   187
   ///////////////////////////////////////////
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   188
   // Do some checkings concerning the busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   189
   // format should be IP-Adress:Port
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   190
   // e.g. 192.168.1.254:A
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   191
   ///////////////////////////////////////////
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   192
   nBusnameLen = strlen(board->busname);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   193
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   194
   if ( nBusnameLen < strlen( "1.2.3.4:A" ) )         bBusnameValid = FALSE;  // check minimum length of busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   195
   if ( nBusnameLen > strlen( "123.234.345.456:A" ) ) bBusnameValid = FALSE;  // check maximum length of busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   196
   if ( bBusnameValid )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   197
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   198
      switch (board->busname[nBusnameLen-1])                                 // check Portname of busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   199
	   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   200
		   case ('A'): PortNr = 0; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   201
		   case ('B'): PortNr = 1; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   202
		   case ('C'): PortNr = 2; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   203
		   case ('D'): PortNr = 3; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   204
		   default :   bBusnameValid = FALSE; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   205
	   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   206
      if (board->busname[nBusnameLen-2] != ':' )    bBusnameValid = FALSE;   // check Colon before Portname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   207
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   208
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   209
   if ( ! bBusnameValid )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   210
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   211
	   fprintf(stderr, "canOpen_driver (AnaGate_Win32): busname (\"%s\") has a wrong format. Use IPAddr:CANPort for example \"192.168.1.254:A\"\n",board->busname);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   212
	   return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   213
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   214
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   215
   board->busname[nBusnameLen-2] = 0; // NULL Terminator for IP Address string
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   216
	strcpy (sIPAddress, board->busname);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   217
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   218
	pNewAnaGatePort = (SAnaGatePort*) malloc(sizeof (struct SAnaGatePort));
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   219
	if (pFirstAnaGatePort == NULL)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   220
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   221
		pFirstAnaGatePort = pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   222
		pNewAnaGatePort->pNext =  pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   223
		pNewAnaGatePort->pPrev =  pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   224
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   225
	else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   226
	{   pNewAnaGatePort->pNext =  pFirstAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   227
		pNewAnaGatePort->pPrev =  pFirstAnaGatePort->pPrev;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   228
		pFirstAnaGatePort->pPrev->pNext = pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   229
		pFirstAnaGatePort->pPrev = pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   230
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   231
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   232
	// Connect to AnaGate
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   233
	if ( nRetCode = CANOpenDevice (&pNewAnaGatePort->hHandle, 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   234
		 						0,		/*confirmation*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   235
								1,		/*Monitor*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   236
								PortNr,
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   237
								sIPAddress, 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   238
								1000	/*TimeOut*/ )  )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   239
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   240
		CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   241
		fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   242
		//printf( "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   243
		return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   244
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   245
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   246
	// Inizial Baudrate
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   247
	if (nRetCode = CANSetGlobals (pNewAnaGatePort->hHandle, 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   248
							   TranslateBaudeRate(board->baudrate), 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   249
							   0,/*OperatingMode = normal*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   250
							   0,/*CAN-Termination = off*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   251
							   1 /*HighSpeedMode = on*/) ) 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   252
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   253
		CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   254
		fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   255
		//printf("canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   256
		return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   257
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   258
	
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   259
	// Creat receive and receive-acknoledge event 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   260
	pNewAnaGatePort->hAnaRecEvent = CreateEvent(	
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   261
												NULL,  // default security attributes
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   262
												FALSE, // manual-reset event
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   263
												FALSE, // initial state is nonsignaled
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   264
												NULL  // object name
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   265
												); 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   266
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   267
	pNewAnaGatePort->hFesticalRecAcknowledge = CreateEvent( 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   268
												NULL,  // default security attributes
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   269
												FALSE, // manual-reset event
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   270
												FALSE, // initial state is nonsignaled
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   271
												NULL   // object name
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   272
												); 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   273
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   274
	// Install receive callback funktion
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   275
	if (nRetCode = CANSetCallback(pNewAnaGatePort->hHandle,  AnaGateReceiveCallBack) ) 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   276
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   277
		canClose_driver (pNewAnaGatePort);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   278
		CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   279
		fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   280
		//printf("canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   281
		return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   282
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   283
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   284
  return (CAN_HANDLE)pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   285
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   286
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   287
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   288
int canClose_driver(CAN_HANDLE fd0)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   289
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   290
	SAnaGatePort*  pAnaCanPort = (SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   291
	char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   292
	int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   293
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   294
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   295
	SetEvent	(pAnaCanPort->hAnaRecEvent);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   296
	CloseHandle (pAnaCanPort->hAnaRecEvent);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   297
	CloseHandle (pAnaCanPort->hFesticalRecAcknowledge);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   298
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   299
	if ( nRetCode = CANCloseDevice(pAnaCanPort->hHandle) )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   300
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   301
	  CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   302
	  fprintf(stderr, "canClose_driver (AnaGate_Linux): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   303
	  //printf("canClose_driver (AnaGate_Linux): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   304
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   305
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   306
	if (pAnaCanPort->pNext == pAnaCanPort)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   307
	{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   308
		 free (pAnaCanPort);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   309
		 pFirstAnaGatePort=NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   310
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   311
	else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   312
	{   pAnaCanPort->pNext->pPrev = pAnaCanPort->pPrev;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   313
		pAnaCanPort->pPrev->pNext = pAnaCanPort->pNext;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   314
		free (pAnaCanPort);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   315
	}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   316
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   317
	return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   318
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   319
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   320
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   321
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   322
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   323