drivers/win32/win32.c
changeset 710 e7a45c77d6ec
parent 697 24a2aec61731
child 723 c33da109a4c8
--- a/drivers/win32/win32.c	Mon Jan 23 21:40:59 2012 +0100
+++ b/drivers/win32/win32.c	Sat Feb 04 01:33:16 2012 +0100
@@ -3,6 +3,7 @@
 
 Copyright (C): Edouard TISSERANT and Francis DUPIN
 Copyright (C) Win32 Port Leonid Tochinski
+Modified by: Jaroslav Fojtik
 
 See COPYING file for copyrights details.
 
@@ -118,6 +119,17 @@
 	m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
 	m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
 	m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
+
+	if(m_canReceive==NULL || m_canSend==NULL || m_canOpen==NULL || m_canClose==NULL || m_canChangeBaudRate==NULL)
+	{
+	  m_canReceive = NULL;
+	  m_canSend = NULL;
+	  m_canOpen = NULL;
+	  m_canClose = NULL;
+	  m_canChangeBaudRate = NULL;
+	  FreeLibrary(handle);
+	  handle = NULL;
+	}
 #else
   //compiled in...
   handle = 1; //TODO: remove this hack
@@ -136,32 +148,39 @@
 /***************************************************************************/
 UNS8 canSend(CAN_PORT port, Message *m)
 {
-	UNS8 res = 1; //NOT OK
 	if (port && (m_canSend != NULL))
 	{
-		res = m_canSend(((CANPort*)port)->fd, m);
-	}
-	return res;
-}
-
-/***************************************************************************/
-void canReceiveLoop(CAN_PORT port)
+		return m_canSend(((CANPort*)port)->fd, m);
+	}
+	return 1; /* NOT OK */	
+}
+
+/***************************************************************************/
+DWORD canReceiveLoop(CAN_PORT port)
 {
 	Message m;
 	while(((CANPort*)port)->used)
 	{
-		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break;
+		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) continue;
 		EnterMutex();
 		canDispatch(((CANPort*)port)->d, &m);
 		LeaveMutex();
 	}
+	return 0;
 }
 
 /***************************************************************************/
 CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
 {
 	int i;
-    CAN_HANDLE fd0;
+	CAN_HANDLE fd0;
+
+
+	  /* Fix of multiple opening one data set, added by J.Fojtik. */
+	if(d->canHandle)
+	{
+	  canClose(d);
+	}
 
 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
 	{
@@ -197,28 +216,27 @@
 /***************************************************************************/
 int canClose(CO_Data * d)
 {
-	UNS8 res = 1;
+	UNS8 res;
 	CANPort* tmp;
 
-	if((CANPort*)d->canHandle)
-	{
-	  ((CANPort*)d->canHandle)->used = 0;
-	}
-
 	tmp = (CANPort*)d->canHandle;
 
 	if(tmp)
 	{
-	  // kill receiver task before port is closed and handle set to NULL
+	  d->canHandle = NULL;
+
+		// close CAN port
+	  res = m_canClose(tmp->fd);
+
+		// kill receiver task
 	  WaitReceiveTaskEnd(&tmp->receiveTask);
 
-	  // close CAN port
-	  res = m_canClose(tmp->fd);
-	}
-
-	d->canHandle = NULL;
-
-	return res;
+		// release used flag as a last step.
+	  tmp->used = 0;
+	}
+  else res = 255;
+
+return res;
 }
 
 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)