SocketCan.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
19 #include <stdlib.h>
20 #include <cerrno>
21 #include <cstring>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 
28 SocketCan::SocketCan(const char* device, int baudrate)
29 {
30  m_bInitialized = false;
31 
32  p_cDevice = device;
34 }
35 
36 SocketCan::SocketCan(const char* device)
37 {
38  m_bInitialized = false;
39 
40  p_cDevice = device;
42 }
43 
44 //-----------------------------------------------
46 {
47  if (m_bInitialized)
48  {
49  m_handle->shutdown();
50  }
51 }
52 
53 //-----------------------------------------------
55 {
56  bool ret = true;
57  if (!m_handle->init(p_cDevice, false))
58  {
59  print_error(m_handle->getState());
60  ret = false;
61  }
62  else
63  {
65  m_bInitialized = true;
66  bool bRet = true;
67  ret = true;
68  }
69  return ret;
70 }
71 
72 //-----------------------------------------------
74 {
75  if (!init_ret())
76  {
77  sleep(3);
78  exit(0);
79  }
80 }
81 
82 
83 //-------------------------------------------
84 bool SocketCan::transmitMsg(CanMsg CMsg, bool bBlocking)
85 {
86  can::Header header(CMsg.getID(), false, false, false);
87  can::Frame message(header, CMsg.getLength());
88  for (int i = 0; i < CMsg.getLength(); i++)
89  {
90  message.data[i] = CMsg.getAt(i);
91  }
92  return m_handle->send(message);
93 }
94 
95 //-------------------------------------------
97 {
98  if (!m_bInitialized)
99  {
100  return false;
101  }
102 
103  bool bRet = false;
104  can::Frame frame;
105 
106  if (m_reader.read(&frame, boost::chrono::seconds(1)))
107  {
108  pCMsg->setID(frame.id);
109  pCMsg->setLength(frame.dlc);
110  pCMsg->set(frame.data[0], frame.data[1], frame.data[2], frame.data[3],
111  frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
112  bRet = true;
113  }
114  return bRet;
115 }
116 
117 //-------------------------------------------
118 bool SocketCan::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
119 {
120  if (!m_bInitialized)
121  {
122  return false;
123  }
124 
125  can::Frame frame;
126  bool bRet = false;
127  int i = 0;
128 
129  do
130  {
131  if (m_reader.read(&frame, boost::chrono::milliseconds(10)))
132  {
133  pCMsg->setID(frame.id);
134  pCMsg->setLength(frame.dlc);
135  pCMsg->set(frame.data[0], frame.data[1], frame.data[2], frame.data[3],
136  frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
137  bRet = true;
138  break;
139  }
140  i++;
141  }
142  while ((i < iNrOfRetry && bRet != true));
143  return bRet;
144 }
145 
146 //-------------------------------------------
147 bool SocketCan::receiveMsgTimeout(CanMsg* pCMsg, int nMicroSecTimeout)
148 {
149  if (!m_bInitialized)
150  {
151  return false;
152  }
153 
154  bool bRet = false;
155  can::Frame frame;
156 
157  if (m_reader.read(&frame, boost::chrono::microseconds(nMicroSecTimeout)))
158  {
159  pCMsg->setID(frame.id);
160  pCMsg->setLength(frame.dlc);
161  pCMsg->set(frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
162  bRet = true;
163  }
164  return bRet;
165 }
166 
168 {
169  std::string err;
170  std::cout << "ERROR: state=" << std::endl;
171  m_handle->translateError(state.internal_error, err);
172  std::cout << "ERROR: state=" << state.driver_state << " internal_error=" << state.internal_error << "('" << err << "') asio: " << state.error_code << std::endl;
173 }
void setLength(int len)
Definition: CanMsg.h:182
void listen(CommInterfaceSharedPtr interface)
bool m_bInitialized
Definition: SocketCan.h:51
bool transmitMsg(CanMsg CMsg, bool bBlocking=true)
Definition: SocketCan.cpp:84
int getID()
Definition: CanMsg.h:153
Definition: CanMsg.h:28
boost::array< value_type, 8 > data
boost::shared_ptr< CommInterface > CommInterfaceSharedPtr
bool receiveMsgRetry(CanMsg *pCMsg, int iNrOfRetry)
Definition: SocketCan.cpp:118
can::BufferedReader m_reader
Definition: SocketCan.h:49
bool read(can::Frame *msg, const DurationType &duration)
bool receiveMsgTimeout(CanMsg *pCMsg, int nMicroSecTimeout)
Definition: SocketCan.cpp:147
bool init_ret()
Definition: SocketCan.cpp:54
const char * p_cDevice
Definition: SocketCan.h:52
enum can::State::DriverState driver_state
void init()
Definition: SocketCan.cpp:73
bool receiveMsg(CanMsg *pCMsg)
Definition: SocketCan.cpp:96
void setID(int id)
Definition: CanMsg.h:163
SocketCan(const char *device, int baudrate)
Definition: SocketCan.cpp:28
void set(BYTE Data0=0, BYTE Data1=0, BYTE Data2=0, BYTE Data3=0, BYTE Data4=0, BYTE Data5=0, BYTE Data6=0, BYTE Data7=0)
Definition: CanMsg.h:60
can::ThreadedSocketCANInterfaceSharedPtr m_handle
Definition: SocketCan.h:48
unsigned int internal_error
unsigned char dlc
int getLength()
Definition: CanMsg.h:173
unsigned int id
int getAt(int iNr)
Definition: CanMsg.h:99
void print_error(const can::State &state)
Definition: SocketCan.cpp:167
boost::system::error_code error_code


cob_generic_can
Author(s): Christian Connette
autogenerated on Wed Apr 7 2021 02:11:52