drivers/can_peak_win32/can_peak_win32.c
author Christian Taedcke <hacking@taedcke.com>
Thu, 22 Dec 2011 11:43:08 +0100
changeset 678 c1b9455929e6
parent 650 58b07a7e695b
child 696 381af6bb6155
permissions -rwxr-xr-x
FIXED: - adapted paths for current vscom driver
- removed chmod +x vs_can_api.dll;
- added option -o to unzip command to overwrite existing files
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
     1
/*
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     3
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     5
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     6
See COPYING file for copyrights details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     7
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     8
This library is free software; you can redistribute it and/or
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    10
License as published by the Free Software Foundation; either
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    12
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    13
This library is distributed in the hope that it will be useful,
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    16
Lesser General Public License for more details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    17
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    19
License along with this library; if not, write to the Free Software
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
    21
*/
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
    22
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    23
#if defined(WIN32) && !defined(__CYGWIN__)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    24
#define usleep(micro) Sleep(micro%1000 ? (micro/1000) + 1 : (micro/1000))
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    25
#else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    26
#include <stdio.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    27
#include <string.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    28
#include <errno.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    29
#include <fcntl.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    30
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    31
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    32
#include "cancfg.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    33
#include "can_driver.h"
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
    34
#include "def.h"
41
e70439f50539 First working cygwin/pcan_light commit. can_peak_win32.c is obfuscated.
etisserant
parents: 38
diff changeset
    35
#ifndef extra_PCAN_init_params
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    36
	#define extra_PCAN_init_params /**/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    37
#else
574
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    38
	long int print_getenv(const char* pcanparam)
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    39
	{
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    40
		char* param=NULL;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    41
		long int res=0;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    42
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    43
		param = getenv(pcanparam);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    44
		if(param != NULL){
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    45
			res = strtol(param,NULL,0);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    46
		}
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    47
		else
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    48
			printf("Environment variable %s not defined !\n", pcanparam);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    49
		printf("Found environment variable %s : %ld\n", pcanparam ,res);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    50
		return res;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    51
	}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    52
	#define extra_PCAN_init_params\
574
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    53
		,print_getenv("PCANHwType")\
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    54
		,print_getenv("PCANIO_Port")\
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    55
		,print_getenv("PCANInterupt")
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    56
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    57
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    58
static s_BOARD *first_board = NULL;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    59
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    60
//Create the Event for the first board
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    61
HANDLE hEvent1 = NULL;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    62
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    63
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    64
#ifdef PCAN2_HEADER_
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    65
	static s_BOARD *second_board = NULL;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    66
	HANDLE hEvent2 = NULL;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    67
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    68
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    69
// Define for rtr CAN message
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    70
#define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    71
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    72
/***************************************************************************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    73
int TranslateBaudeRate(char* optarg){
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    74
	if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    75
	if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    76
	if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    77
	if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    78
	if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    79
	if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    80
	if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    81
	if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    82
	if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    83
	if(!strcmp( optarg, "none")) return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    84
	return 0x0000;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    85
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    86
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    87
UNS8 canInit (s_BOARD *board)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    88
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    89
	int baudrate;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    90
	int ret = 0;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    91
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
    92
#ifdef PCAN2_HEADER_
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    93
	// if not the first handler
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    94
	if(second_board == (s_BOARD *)board) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    95
		if(baudrate = TranslateBaudeRate(board->baudrate))
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    96
		{
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    97
			ret = CAN2_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    98
			if(ret != CAN_ERR_OK)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    99
				return 0;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   100
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   101
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   102
		//Create the Event for the first board
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   103
		if(hEvent2 != NULL){
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   104
			hEvent2 = CreateEvent(NULL, // lpEventAttributes
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   105
			                      FALSE,  // bManualReset
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   106
			                      FALSE,  // bInitialState
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   107
			                      "");    // lpName
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   108
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   109
		//Set Event Handle for CANReadExt
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   110
		CAN2_SetRcvEvent(hEvent2);
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   111
	}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   112
	else
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   113
#endif
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   114
	if(first_board == (s_BOARD *)board) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   115
		if(baudrate = TranslateBaudeRate(board->baudrate))
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   116
		{
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   117
			ret = CAN_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   118
			if(ret != CAN_ERR_OK)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   119
				return 0;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   120
		}
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   121
		//Create the Event for the first board
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   122
		if(hEvent1 != NULL){
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   123
			hEvent1 = CreateEvent(NULL, // lpEventAttributes
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   124
			                      FALSE,  // bManualReset
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   125
			                      FALSE,  // bInitialState
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   126
			                      "");    // lpName
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   127
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   128
		//Set Event Handle for CANReadExt
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   129
		CAN_SetRcvEvent(hEvent1);
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   130
	}
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   131
	return 1;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   132
}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   133
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   134
/********* functions which permit to communicate with the board ****************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   135
UNS8 __stdcall canReceive_driver (CAN_HANDLE fd0, Message * m)
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   136
{
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   137
	int ret=0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   138
	UNS8 data;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   139
	TPCANMsg peakMsg;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   140
	TPCANTimestamp peakRcvTime;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   141
	DWORD Res;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   142
	DWORD result;
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   143
	// loop until valid message or fatal error
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   144
	do{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   145
#ifdef PCAN2_HEADER_
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   146
		// if not the first handler
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   147
		if(second_board == (s_BOARD *)fd0) {
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   148
			//wait for CAN msg...
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   149
			result = WaitForSingleObject(hEvent2, INFINITE);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   150
			if (result == WAIT_OBJECT_0)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   151
				Res = CAN2_ReadEx(&peakMsg, &peakRcvTime);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   152
				// Exit receive thread when handle is no more valid
650
58b07a7e695b FIXED: - Renamed macro for peak win32 driver, patch from mailinglist (post from Christian Boepple, 2010-09-08 14:38)
Christian Taedcke
parents: 633
diff changeset
   153
				if(Res & CAN_ERR_ILLHANDLE)
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   154
					return 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   155
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   156
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   157
#endif
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   158
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   159
		// We read the queue looking for messages.
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   160
		if(first_board == (s_BOARD *)fd0) {
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   161
			result = WaitForSingleObject(hEvent1, INFINITE);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   162
			if (result == WAIT_OBJECT_0)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   163
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   164
				Res = CAN_ReadEx(&peakMsg, &peakRcvTime);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   165
				// Exit receive thread when handle is no more valid
650
58b07a7e695b FIXED: - Renamed macro for peak win32 driver, patch from mailinglist (post from Christian Boepple, 2010-09-08 14:38)
Christian Taedcke
parents: 633
diff changeset
   166
				if(Res & CAN_ERR_ILLHANDLE)
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   167
					return 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   168
			}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   169
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   170
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   171
			Res = CAN_ERR_BUSOFF;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   172
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   173
		// A message was received : we process the message(s)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   174
		if (Res == CAN_ERR_OK)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   175
		{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   176
			// if something different that 11bit or rtr... problem
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   177
			if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR))
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   178
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   179
				if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   180
				{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   181
					printf ("!!! Peak board read : re-init\n");
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   182
					canInit((s_BOARD*) fd0);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   183
					usleep (10000);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   184
				}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   185
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   186
				// If status, return status if 29bit, return overrun
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   187
				return peakMsg.MSGTYPE ==
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   188
					MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   189
			}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   190
			m->cob_id = peakMsg.ID;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   191
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   192
			if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)	/* bits of MSGTYPE_ */
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   193
				m->rtr = 0;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   194
			else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   195
				m->rtr = 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   196
			m->len = peakMsg.LEN;	/* count of data bytes (0..8) */
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   197
			for (data = 0; data < peakMsg.LEN; data++)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   198
				m->data[data] = peakMsg.DATA[data];	/* data bytes, up to 8 */
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   199
#if defined DEBUG_MSG_CONSOLE_ON
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   200
			MSG("in : ");
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   201
			print_message(m);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   202
#endif
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   203
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   204
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   205
		{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   206
			// not benign error => fatal error
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   207
			if (!(Res & CAN_ERR_QRCVEMPTY
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   208
					|| Res & CAN_ERR_BUSLIGHT
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   209
					|| Res & CAN_ERR_BUSHEAVY))
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   210
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   211
				printf ("canReceive returned error (%d)\n", Res);
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   212
				return 1;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   213
			}
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   214
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   215
	}while(Res != CAN_ERR_OK);
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   216
	return 0;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   217
}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   218
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   219
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   220
UNS8 __stdcall canSend_driver (CAN_HANDLE fd0, Message const * m)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   221
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   222
	UNS8 data;
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   223
	DWORD localerrno;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   224
	TPCANMsg peakMsg;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 341
diff changeset
   225
	peakMsg.ID = m->cob_id;	/* 11/29 bit code */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   226
	if (m->rtr == 0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   227
		peakMsg.MSGTYPE = CAN_INIT_TYPE_ST;	/* bits of MSGTYPE_ */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   228
	else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   229
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   230
		peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR;	/* bits of MSGTYPE_ */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   231
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   232
	peakMsg.LEN = m->len;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   233
	/* count of data bytes (0..8) */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   234
	for (data = 0; data < m->len; data++)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   235
		peakMsg.DATA[data] = m->data[data];	/* data bytes, up to 8 */
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   236
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   237
	do
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   238
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   239
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   240
		// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   241
		if(second_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   242
		{
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   243
			errno = localerrno = CAN2_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   244
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   245
		else
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   246
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   247
		if(first_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   248
			{
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   249
				errno = localerrno = CAN_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   250
			}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   251
		else
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   252
			goto fail;
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   253
		if (localerrno)
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   254
		{
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   255
			if (localerrno == CAN_ERR_BUSOFF)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   256
			{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   257
				printf ("!!! Peak board write : re-init\n");
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   258
				canInit((s_BOARD*)fd0);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   259
				usleep (10000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   260
			}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   261
			usleep (1000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   262
		}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   263
	}
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   264
	while (localerrno != CAN_ERR_OK);
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   265
#if defined DEBUG_MSG_CONSOLE_ON
454
bc000083297a - add RTAI support
greg
parents: 384
diff changeset
   266
	MSG("out : ");
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   267
	print_message(m);
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   268
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   269
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   270
fail:
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   271
	return 1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   272
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   273
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   274
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   275
UNS8 __stdcall canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   276
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   277
	printf("canChangeBaudRate not yet supported by this driver\n");
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   278
	return 0;
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   279
}
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   280
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   281
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   282
CAN_HANDLE __stdcall canOpen_driver (s_BOARD * board)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   283
{
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   284
  char busname[64];
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   285
  char* pEnd;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   286
  int ret;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   287
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   288
  //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0));
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   289
  if (strtol(board->busname, &pEnd,0) == 0)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   290
  {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   291
      first_board = board;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   292
      //printf ("First Board selected\n");
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   293
      ret = canInit(board);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   294
      if(ret)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   295
    	  return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   296
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   297
  #ifdef PCAN2_HEADER_
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   298
  if (strtol(board->busname, &pEnd,0) == 1)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   299
  {
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   300
      second_board = board;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   301
      //printf ("Second Board selected\n");
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   302
      ret = canInit(board);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   303
      if(ret)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   304
    	  return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   305
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   306
  #endif
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   307
  return NULL;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   308
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   309
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   310
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   311
int __stdcall canClose_driver (CAN_HANDLE fd0)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   312
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   313
#ifdef PCAN2_HEADER_
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   314
      // if not the first handler
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   315
      if(second_board == (s_BOARD *)fd0)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   316
      {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   317
           CAN2_SetRcvEvent(NULL);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   318
           CAN2_Close ();
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   319
           if(hEvent2)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   320
           {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   321
             SetEvent(hEvent2);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   322
             CloseHandle(hEvent2);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   323
             hEvent2 = NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   324
           }
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   325
           second_board = (s_BOARD *)NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   326
      }else
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   327
#endif
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   328
      if(first_board == (s_BOARD *)fd0)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   329
      {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   330
           CAN_SetRcvEvent(NULL);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   331
           CAN_Close ();
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   332
           if(hEvent1)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   333
           {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   334
             SetEvent(hEvent1);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   335
             CloseHandle(hEvent1);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   336
             hEvent1 = NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   337
            }
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   338
           first_board = (s_BOARD *)NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   339
      }
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   340
      return 0;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   341
}