drivers/can_tcp_win32/can_tcp_win32.cpp
changeset 774 91d708a2cb4e
parent 771 4c74be252095
child 777 bbbfd27c1bd1
equal deleted inserted replaced
773:c10def4bfbde 774:91d708a2cb4e
    26 
    26 
    27 using namespace std;
    27 using namespace std;
    28 
    28 
    29 extern "C" {
    29 extern "C" {
    30 #include "can_driver.h"
    30 #include "can_driver.h"
    31 }
       
    32 
    31 
    33 //------------------------------------------------------------------------
    32 //------------------------------------------------------------------------
    34 extern "C"
    33 UNS8 LIBAPI canReceive_driver(CAN_HANDLE fd0, Message *m)
    35    UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
    34 {
    36    {
       
    37 
    35 
    38       string l = reinterpret_cast<SocketClient*>(fd0)->ReceiveLine();
    36     string l = reinterpret_cast<SocketClient*>(fd0)->ReceiveLine();
    39 
    37 
    40       int res = sscanf(l.c_str(),"{0x%3hx,%1hhd,%1hhd,{0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx}}",
    38     int res = sscanf(l.c_str(),
    41                 &m->cob_id,
    39             "{0x%3hx,%1hhd,%1hhd,{0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx}}",
    42                 &m->rtr,
    40             &m->cob_id,
    43                 &m->len,
    41             &m->rtr,
    44                 &m->data[0],
    42             &m->len,
    45                 &m->data[1],
    43             &m->data[0],
    46                 &m->data[2],
    44             &m->data[1],
    47                 &m->data[3],
    45             &m->data[2],
    48                 &m->data[4],
    46             &m->data[3],
    49                 &m->data[5],
    47             &m->data[4],
    50                 &m->data[6],
    48             &m->data[5],
    51                 &m->data[7]
    49             &m->data[6],
    52                 );
    50             &m->data[7]
       
    51             );
    53 
    52 
    54 
    53 
    55 #if defined DEBUG_MSG_CONSOLE_ON
    54 #if defined DEBUG_MSG_CONSOLE_ON
    56   printf("in : ");
    55     printf("in : ");
    57   print_message(m);
    56     print_message(m);
    58 #endif
    57 #endif
    59    
       
    60       return res==11 ? 0 : 1 ;
       
    61    }
       
    62 
    58 
    63 extern "C"
    59   return res==11 ? 0 : 1 ;
    64    UNS8 canSend_driver(CAN_HANDLE fd0, Message const *m)
    60 }
    65    {
    61 
    66         char s[1024];        
    62 UNS8 LIBAPI canSend_driver(CAN_HANDLE fd0, Message const *m)
    67         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}}",
    63 {
    68                 m->cob_id,
    64     char s[1024];        
    69                 m->rtr,
    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}}",
    70                 m->len,
    66             m->cob_id,
    71                 m->data[0],
    67             m->rtr,
    72                 m->data[1],
    68             m->len,
    73                 m->data[2],
    69             m->data[0],
    74                 m->data[3],
    70             m->data[1],
    75                 m->data[4],
    71             m->data[2],
    76                 m->data[5],
    72             m->data[3],
    77                 m->data[6],
    73             m->data[4],
    78                 m->data[7]
    74             m->data[5],
    79                 );
    75             m->data[6],
    80     
    76             m->data[7]
    81         reinterpret_cast<SocketClient*>(fd0)->SendLine(s);
    77             );
       
    78 
       
    79     reinterpret_cast<SocketClient*>(fd0)->SendLine(s);
    82 #if defined DEBUG_MSG_CONSOLE_ON
    80 #if defined DEBUG_MSG_CONSOLE_ON
    83   printf("out : ");
    81     printf("out : ");
    84   print_message(m);
    82     print_message(m);
    85 #endif
    83 #endif
    86         return 0;
    84     return 0;
    87    }
    85 }
    88 
    86 
    89 extern "C"
    87 CAN_HANDLE LIBAPI canOpen_driver(s_BOARD *board)
    90    CAN_HANDLE canOpen_driver(s_BOARD *board)
    88 {
    91    {
    89     Socket* s;
    92       Socket* s;
    90     try {
    93       try {
    91       char *dst = "127.0.0.1";
    94         char *dst = "127.0.0.1";
    92       if(!strlen(board->busname)){
    95         if(strlen(board->busname)){
    93           dst=board->busname;
    96             dst=board->busname;
       
    97         }
       
    98         s = new SocketClient(dst, 11898);
       
    99       } 
       
   100       catch (const char* _s) {
       
   101         cerr << _s << endl;
       
   102         return NULL;
       
   103       } 
       
   104       catch (std::string _s) {
       
   105         cerr << _s << endl;
       
   106         return NULL;
       
   107       } 
       
   108       catch (...) {
       
   109         cerr << "unhandled exception\n";
       
   110         return NULL;
       
   111       }
    94       }
   112       return (CAN_HANDLE) s;
    95       s = new SocketClient(dst, 11898);
   113    }
    96     } 
       
    97     catch (const char* _s) {
       
    98       cerr << "can_tcp_win32 exception :\n";
       
    99       cerr << _s << endl;
       
   100       return NULL;
       
   101     } 
       
   102     catch (std::string _s) {
       
   103       cerr << "can_tcp_win32 exception :\n";
       
   104       cerr << _s << endl;
       
   105       return NULL;
       
   106     } 
       
   107     catch (...) {
       
   108       cerr << "can_tcp_win32 exception :\n";
       
   109       cerr << "unhandled exception\n";
       
   110       return NULL;
       
   111     }
       
   112     return (CAN_HANDLE) s;
       
   113 }
   114 
   114 
   115 extern "C"
   115 int LIBAPI canClose_driver(CAN_HANDLE inst)
   116    int canClose_driver(CAN_HANDLE inst)
   116 {
   117    {
   117     delete reinterpret_cast<SocketClient*>(inst);
   118    delete reinterpret_cast<SocketClient*>(inst);
   118     return 1;
   119    return 1;
   119 }
   120    }
   120 
   121 extern "C"
   121 UNS8 LIBAPI canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
   122 	UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
   122 {
   123 	{
   123     cerr << "canChangeBaudRate not yet supported by this driver\n";
   124 	cerr << "canChangeBaudRate not yet supported by this driver\n";
   124     return 0;
   125 	return 0;
   125 }
   126 	}
   126 }