CanESD.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 
18 // general includes
19 
20 // Headers provided by other cob-packages
21 #include <cob_generic_can/CanESD.h>
22 
23 // Headers provided by other cob-packages which should be avoided/removed
24 
25 
26 //-----------------------------------------------
27 CanESD::CanESD(const char* cIniFile, bool bObjectMode)
28 {
29  m_bObjectMode = bObjectMode;
30  m_bIsTXError = false;
31  m_IniFile.SetFileName(cIniFile, "CanESD.cpp");
32  initIntern();
33 }
34 
35 //-----------------------------------------------
41 {
42  std::cout << "Closing CAN handle" << std::endl;
44 }
45 
46 //-----------------------------------------------
48 {
49  // Not implemented yet
50  return false;
51 }
52 
53 //-----------------------------------------------
55 {
56  int ret=0;
57  ret = 0;
58  int iCanNet = 1;
59  m_IniFile.GetKeyInt( "CanCtrl", "NetESD", &iCanNet, true);
60 
61  int iBaudrateVal = 2;
62  m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true);
63 
64  std::cout << "Initializing CAN network with id =" << iCanNet << ", baudrate=" << iBaudrateVal << std::endl;
65 
66  int iRet;
67  if( m_bObjectMode )
68  iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 1000, 0, &m_Handle);
69  else
70  iRet = canOpen(iCanNet, 0, 10000, 10000, 1000, 0, &m_Handle);
71  Sleep(300);
72 
73  if(iRet == NTCAN_SUCCESS)
74  std::cout << "CanESD::CanESD(), init ok" << std::endl;
75  else
76  std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
77 
78  iRet = canSetBaudrate(m_Handle, iBaudrateVal);
79  if(iRet == NTCAN_SUCCESS)
80  std::cout << "CanESD::CanESD(), canSetBaudrate ok" << std::endl;
81  else
82  std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
83  Sleep(300);
84 
85  //long lArg;
87 
88  // MMB/24.02.2006: Add all 11-bit identifiers as there is no loss in performance.
89  for( int i=0; i<=0x7FF; ++i ) {
90  iRet = canIdAdd( m_Handle, i );
91  if(iRet != NTCAN_SUCCESS)
92  std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
93  }
94 
95 
96  Sleep(300);
97 
98  m_LastID = -1;
99 }
100 
101 //-----------------------------------------------
108 bool CanESD::transmitMsg(CanMsg CMsg, bool bBlocking)
109 {
110  CMSG NTCANMsg;
111  NTCANMsg.id = CMsg.m_iID;
112  NTCANMsg.len = CMsg.m_iLen;
113 
114  for(int i=0; i<8; i++)
115  NTCANMsg.data[i] = CMsg.getAt(i);
116 
117  int ret;
118  int32_t len;
119  bool bRet = true;
120 
121  len = 1;
122 
123  if (bBlocking)
124  ret = canWrite(m_Handle, &NTCANMsg, &len, NULL);
125  else
126  ret = canSend(m_Handle, &NTCANMsg, &len);
127 
128  if( ret != NTCAN_SUCCESS)
129  {
130  std::cout << "error in CANESD::transmitMsg: " << GetErrorStr(ret) << std::endl;
131  bRet = false;
132  }
133 
134  m_LastID = (int)NTCANMsg.data[0];
135 
136  m_bIsTXError = !bRet;
137  return bRet;
138 }
139 
140 //-----------------------------------------------
141 bool CanESD::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
142 {
143  //int id = pCMsg->m_iID;
144  CMSG NTCANMsg;
145  NTCANMsg.len = 8;
146 
147  int32_t len;
148  int i, ret;
149  bool bRet = true;
150 
151  i=0;
152 
153  len = 1;
154 
155  do
156  {
157  len = 1;
158  ret = canTake(m_Handle, &NTCANMsg, &len);
159  i++;
160  Sleep(10);
161  }
162 
163  while((len == 0) && (i < iNrOfRetry));
164 
165  if(i == iNrOfRetry)
166  {
167  if( ret != NTCAN_SUCCESS )
168  std::cout << "error in CANESD::receiveMsgRetry: " << GetErrorStr(ret) << std::endl;
169 
170  bRet = false;
171  }
172  else
173  {
174  pCMsg->m_iID = NTCANMsg.id;
175  pCMsg->m_iLen = NTCANMsg.len;
176  pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
177  NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
178  }
179 
180  return bRet;
181 }
182 
183 //-----------------------------------------------
185 {
186  CMSG NTCANMsg;
187  NTCANMsg.len = 8;
188 
189  int ret;
190  int32_t len;
191  bool bRet = true;
192 
193  len = 1;
194 
195  // Debug valgrind
196  NTCANMsg.data[0] = 0;
197  NTCANMsg.data[1] = 0;
198  NTCANMsg.data[2] = 0;
199  NTCANMsg.data[3] = 0;
200  NTCANMsg.data[4] = 0;
201  NTCANMsg.data[5] = 0;
202  NTCANMsg.data[6] = 0;
203  NTCANMsg.data[7] = 0;
204  NTCANMsg.msg_lost = 0;
205  NTCANMsg.id = 0;
206  NTCANMsg.len = 0;
207 
208  pCMsg->set(0,0,0,0,0,0,0,0);
209 
210 
211  if( !isObjectMode() ) {
212  pCMsg->m_iID = 0;
213  } else {
214  NTCANMsg.id = pCMsg->m_iID;
215  }
216 
217  ret = canTake(m_Handle, &NTCANMsg, &len);
218 
219  if( !isObjectMode() ) {
220  if( (len == 1) && (ret == NTCAN_SUCCESS) )
221  {
222  // message received
223  pCMsg->m_iID = NTCANMsg.id;
224  pCMsg->m_iLen = NTCANMsg.len;
225  pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
226  NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
227  bRet = true;
228  }
229  else
230  {
231  // no message
232  if( ret != NTCAN_SUCCESS)
233  {
234  // error
235  std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(ret) << std::endl;
236  }
237 
238  pCMsg->m_iID = NTCANMsg.id;
239  pCMsg->set(0,0,0,0,0,0,0,0);
240 
241  bRet = false;
242  }
243  } else {
244  if( len == 16 ) {
245  // No message was received yet.
246  pCMsg->m_iID = NTCANMsg.id;
247  pCMsg->set(0,0,0,0,0,0,0,0);
248  bRet = false;
249  } else {
250  pCMsg->m_iID = NTCANMsg.id;
251  pCMsg->m_iLen = NTCANMsg.len;
252  pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
253  NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
254  bRet = true;
255  }
256  }
257 
258  if( NTCANMsg.msg_lost != 0 )
259  std::cout << (int)(NTCANMsg.msg_lost) << " messages lost!" << std::endl;
260 
261  return bRet;
262 }
263 
264 
265 //-----------------------------------------------
266 bool CanESD::receiveMsgTimeout(CanMsg* pCMsg, int nMicroSecTimeout)
267 {
268  // Not implemented yet
269  return false;
270 }
271 
281 {
282  int result = NTCAN_SUCCESS;
283  int i = 0;
284  int iRes = 0;
285  int cmd_id = invert(id);
286 
287  for( i=0; i<8; ++i) {
288  iRes = canIdAdd(m_Handle, cmd_id+i);
289 
290  if( iRes != NTCAN_SUCCESS ) {
291  std::cout << "Adding CAN ID " << cmd_id+i << " failed with errorcode: " << iRes << " " << GetErrorStr(iRes) << std::endl;
292  result = iRes;
293  }
294  }
295 
296  return result;
297 }
298 
299 //-----------------------------------------------
300 std::string CanESD::GetErrorStr(int ntstatus) const
301 {
302  switch (ntstatus)
303  {
304  case NTCAN_SUCCESS: return "NTCAN_SUCCESS";
305  case NTCAN_RX_TIMEOUT: return "NTCAN_RX_TIMEOUT";
306  case NTCAN_TX_TIMEOUT: return "NTCAN_TX_TIMEOUT";
307  case NTCAN_TX_ERROR: return "NTCAN_TX_ERROR";
308  case NTCAN_CONTR_OFF_BUS: return "NTCAN_CONTR_OFF_BUS";
309  case NTCAN_CONTR_BUSY: return "NTCAN_CONTR_BUSY";
310  case NTCAN_CONTR_WARN: return "NTCAN_CONTR_WARN";
311  case NTCAN_NO_ID_ENABLED: return "NTCAN_NO_ID_ENABLED";
312  case NTCAN_ID_ALREADY_ENABLED: return "NTCAN_ID_ALREADY_ENABLED";
313  case NTCAN_ID_NOT_ENABLED: return "NTCAN_ID_NOT_ENABLED";
314 
315  case NTCAN_INVALID_FIRMWARE: return "NTCAN_INVALID_FIRMWARE";
316  case NTCAN_MESSAGE_LOST: return "NTCAN_MESSAGE_LOST";
317  case NTCAN_INVALID_HARDWARE: return "NTCAN_INVALID_HARDWARE";
318 
319  case NTCAN_PENDING_WRITE: return "NTCAN_PENDING_WRITE";
320  case NTCAN_PENDING_READ: return "NTCAN_PENDING_READ";
321  case NTCAN_INVALID_DRIVER: return "NTCAN_INVALID_DRIVER";
322 
323  case NTCAN_INVALID_PARAMETER: return "NTCAN_INVALID_PARAMETER";
324  case NTCAN_INVALID_HANDLE: return "NTCAN_INVALID_HANDLE";
325  case NTCAN_NET_NOT_FOUND: return "NTCAN_NET_NOT_FOUND";
326  case NTCAN_INSUFFICIENT_RESOURCES: return "NTCAN_INSUFFICIENT_RESOURCES";
327 
328  case NTCAN_OPERATION_ABORTED: return "NTCAN_OPERATION_ABORTED";
329  }
330  char msg[100];
331  sprintf(msg, "unknown error code %d", ntstatus);
332  return msg;
333 }
334 
345 {
346  EVMSG evmsg;
347  int iRet = 0;
348  int ret;
349 
350  ret = canReadEvent(m_Handle, &evmsg, NULL);
351 
352  if(ret == NTCAN_SUCCESS)
353  {
354  if( (int)evmsg.evid == NTCAN_EV_CAN_ERROR ) {
355  switch( evmsg.evdata.s[0] ) {
356  case 0x00:
357  iRet = 0;
358  break;
359  case 0xC0:
360  iRet = -6;
361  std::cout << "BUS OFF" << std::endl;
362  break;
363  case 0x40:
364  iRet = -7;
365  std::cout << "ERROR PASSIVE" << std::endl;
366  break;
367  }
368  if( evmsg.evdata.s[3] != 0 ) {
369  iRet = -3;
370  std::cout << "Lost " << (int)evmsg.evdata.s[3] << " messages" << std::endl;
371  } else if( evmsg.evdata.s[5] != 0 ) {
372  iRet = -5;
373  std::cout << "Lost " << (int)evmsg.evdata.s[5] << " messages from fifo" << std::endl;
374  }
375  }
376  }
377  return iRet;
378 }
379 
bool receiveMsgRetry(CanMsg *pCMsg, int iNrOfRetry)
Definition: CanESD.cpp:141
#define NTCAN_MESSAGE_LOST
#define NTCAN_INVALID_FIRMWARE
#define NTCAN_ID_NOT_ENABLED
NTCAN_HANDLE m_Handle
Definition: CanESD.h:46
CanESD(const char *cIniFile, bool bObjectMode=false)
Definition: CanESD.cpp:27
uint8_t len
uint16_t s[4]
int32_t id
#define NTCAN_SUCCESS
EXPORT NTCAN_RESULT CALLTYPE canWrite(NTCAN_HANDLE handle, CMSG *cmsg, int32_t *len, OVERLAPPED *ovrlppd)
EXPORT NTCAN_RESULT CALLTYPE canIoctl(NTCAN_HANDLE handle, uint32_t ulCmd, void *pArg)
EXPORT NTCAN_RESULT CALLTYPE canTake(NTCAN_HANDLE handle, CMSG *cmsg, int32_t *len)
#define NTCAN_CONTR_BUSY
EXPORT NTCAN_RESULT CALLTYPE canReadEvent(NTCAN_HANDLE handle, EVMSG *evmsg, OVERLAPPED *ovrlppd)
bool isObjectMode()
Definition: CanESD.h:65
Definition: CanMsg.h:28
bool receiveMsg(CanMsg *pCMsg)
Definition: CanESD.cpp:184
union EVMSG::@0 evdata
bool transmitMsg(CanMsg CMsg, bool bBlocking=true)
Definition: CanESD.cpp:108
int m_iLen
Definition: CanMsg.h:36
int32_t evid
#define NTCAN_CONTR_OFF_BUS
#define NTCAN_INVALID_PARAMETER
#define NTCAN_NO_ID_ENABLED
~CanESD()
Definition: CanESD.cpp:40
int32_t NTCAN_HANDLE
void initIntern()
Definition: CanESD.cpp:54
#define NTCAN_ID_ALREADY_ENABLED
#define NTCAN_EV_CAN_ERROR
int canIdAddGroup(NTCAN_HANDLE handle, int id)
Definition: CanESD.cpp:280
#define NTCAN_TX_TIMEOUT
int GetKeyInt(const char *pSect, const char *pKey, int *pValue, bool bWarnIfNotfound=true)
int invert(int id)
Definition: CanESD.h:79
EXPORT NTCAN_RESULT CALLTYPE canOpen(int32_t net, uint32_t flags, int32_t txqueuesize, int32_t rxqueuesize, int32_t txtimeout, int32_t rxtimeout, NTCAN_HANDLE *handle)
#define NTCAN_TX_ERROR
#define NTCAN_INVALID_HARDWARE
#define NTCAN_OPERATION_ABORTED
#define NTCAN_MODE_OBJECT
EXPORT NTCAN_RESULT CALLTYPE canClose(NTCAN_HANDLE handle)
bool init_ret()
Definition: CanESD.cpp:47
#define NTCAN_INSUFFICIENT_RESOURCES
uint8_t data[8]
#define NTCAN_IOCTL_FLUSH_RX_FIFO
#define NTCAN_INVALID_DRIVER
int SetFileName(std::string fileName, std::string strIniFileUsedBy="", bool bCreate=false)
IniFile m_IniFile
Definition: CanESD.h:52
uint8_t msg_lost
bool receiveMsgTimeout(CanMsg *pCMsg, int nMicroSeconds)
Definition: CanESD.cpp:266
int m_LastID
Definition: CanESD.h:47
std::string GetErrorStr(int ntstatus) const
Definition: CanESD.cpp:300
int m_iID
Definition: CanMsg.h:34
#define NTCAN_PENDING_READ
#define NTCAN_INVALID_HANDLE
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
bool m_bIsTXError
Definition: CanESD.h:49
EXPORT NTCAN_RESULT CALLTYPE canSend(NTCAN_HANDLE handle, CMSG *cmsg, int32_t *len)
int readEvent()
Definition: CanESD.cpp:344
#define NTCAN_CONTR_WARN
#define NTCAN_PENDING_WRITE
bool m_bObjectMode
Definition: CanESD.h:48
EXPORT NTCAN_RESULT CALLTYPE canIdAdd(NTCAN_HANDLE handle, int32_t id)
EXPORT NTCAN_RESULT CALLTYPE canSetBaudrate(NTCAN_HANDLE handle, uint32_t baud)
#define NTCAN_RX_TIMEOUT
int getAt(int iNr)
Definition: CanMsg.h:99
void Sleep(long dwMilliseconds)
#define NTCAN_NET_NOT_FOUND


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