drivers/can_tcp_win32/can_tcp_win32.cpp
author bmakuc <blaz.makuc@smarteh.si>
Fri, 24 Mar 2017 10:52:38 +0100
changeset 796 1c87f7a8cb8a
parent 777 bbbfd27c1bd1
permissions -rwxr-xr-x
Send timeout is set to 10 ms. Without timeout PLC can be blocked by CAN driver: if CAN bus is not connected to controller CAN driver never returns and therfore PLC application halts. This is a temporary solution.
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     1
/*
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     3
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     5
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     7
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    12
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    17
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    21
*/
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    22
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    23
#include "Socket.h"
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    24
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    25
#include <iostream>
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    26
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    27
using namespace std;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    28
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    29
extern "C" {
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    30
#include "can_driver.h"
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    31
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    32
//------------------------------------------------------------------------
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    33
UNS8 LIBAPI canReceive_driver(CAN_HANDLE fd0, Message *m)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    34
{
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    35
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    36
    string l = reinterpret_cast<SocketClient*>(fd0)->ReceiveLine();
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    37
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    38
    int res = sscanf(l.c_str(),
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    39
            "{0x%3hx,%1hhd,%1hhd,{0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx}}",
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    40
            &m->cob_id,
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    41
            &m->rtr,
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    42
            &m->len,
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    43
            &m->data[0],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    44
            &m->data[1],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    45
            &m->data[2],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    46
            &m->data[3],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    47
            &m->data[4],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    48
            &m->data[5],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    49
            &m->data[6],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    50
            &m->data[7]
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    51
            );
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    52
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    53
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    54
#if defined DEBUG_MSG_CONSOLE_ON
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    55
    printf("in : ");
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    56
    print_message(m);
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    57
#endif
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    58
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    59
  return res==11 ? 0 : 1 ;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    60
}
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    61
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    62
UNS8 LIBAPI canSend_driver(CAN_HANDLE fd0, Message const *m)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    63
{
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    64
    char s[1024];        
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    65
    sprintf(s,"{0x%3.3x,%1d,%1d,{0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x}}",
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    66
            m->cob_id,
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    67
            m->rtr,
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    68
            m->len,
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    69
            m->data[0],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    70
            m->data[1],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    71
            m->data[2],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    72
            m->data[3],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    73
            m->data[4],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    74
            m->data[5],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    75
            m->data[6],
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    76
            m->data[7]
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    77
            );
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    78
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    79
    reinterpret_cast<SocketClient*>(fd0)->SendLine(s);
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    80
#if defined DEBUG_MSG_CONSOLE_ON
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    81
    printf("out : ");
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    82
    print_message(m);
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    83
#endif
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    84
    return 0;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    85
}
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    86
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    87
CAN_HANDLE LIBAPI canOpen_driver(s_BOARD *board)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    88
{
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    89
    char *dst = "127.0.0.1";
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    90
    if(!strlen(board->busname)){
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    91
      dst=board->busname;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    92
    }
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    93
    try {
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    94
      CAN_HANDLE res = (CAN_HANDLE) new SocketClient(dst, 11898);
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    95
      return res;
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    96
    } 
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    97
    catch (...) {
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
    98
      cerr << "can_tcp_win32: couldn't connect to" << dst << endl;
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    99
      return NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   100
    }
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   101
}
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
   102
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   103
int LIBAPI canClose_driver(CAN_HANDLE inst)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   104
{
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   105
    delete reinterpret_cast<SocketClient*>(inst);
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   106
    return 1;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   107
}
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   108
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   109
UNS8 LIBAPI canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   110
{
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   111
    cerr << "canChangeBaudRate not yet supported by this driver\n";
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   112
    return 0;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   113
}
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
   114
}