CanPeakSys.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 <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 //-----------------------------------------------
27 
28 const int CanPeakSys::c_iInterrupt = 7;
29 const int CanPeakSys::c_iPort = 0x378;
30 
31 //-----------------------------------------------
32 CanPeakSys::CanPeakSys(const char* cIniFile)
33 {
34  m_bInitialized = false;
35 
36  // read IniFile
37  m_IniFile.SetFileName(cIniFile, "CanPeakSys.cpp");
38  init();
39 }
40 
41 //-----------------------------------------------
43 {
44  if (m_bInitialized)
45  {
46  CAN_Close(m_handle);
47  }
48 }
49 
50 //-----------------------------------------------
52 {
53  // Not implemented yet
54  return false;
55 }
56 
57 //-----------------------------------------------
59 {
60  std::string sCanDevice;
61  if( m_IniFile.GetKeyString( "TypeCan", "DevicePath", &sCanDevice, false) != 0) {
62  sCanDevice = "/dev/pcan32";
63  } else std::cout << "CAN-device path read from ini-File: " << sCanDevice << std::endl;
64  m_handle = LINUX_CAN_Open(sCanDevice.c_str(), O_RDWR);
65 
66 
67  if (! m_handle)
68  {
69  // Fatal error
70  std::cout << "Cannot open CAN-dongle on parallel port: " << strerror(errno) << std::endl;
71  sleep(3);
72  exit(0);
73  }
74 
75 
76  int ret = CAN_ERR_OK;
77  int iBaudrateVal = 0;
78  m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true);
79 
80  switch(iBaudrateVal)
81  {
82  case CANITFBAUD_1M:
83  ret = CAN_Init(m_handle, CAN_BAUD_1M, CAN_INIT_TYPE_ST);
84  break;
85  case CANITFBAUD_500K:
86  ret = CAN_Init(m_handle, CAN_BAUD_500K, CAN_INIT_TYPE_ST);
87  break;
88  case CANITFBAUD_250K:
89  ret = CAN_Init(m_handle, CAN_BAUD_250K, CAN_INIT_TYPE_ST);
90  break;
91  case CANITFBAUD_125K:
92  ret = CAN_Init(m_handle, CAN_BAUD_125K, CAN_INIT_TYPE_ST);
93  break;
94  case CANITFBAUD_50K:
95  ret = CAN_Init(m_handle, CAN_BAUD_50K, CAN_INIT_TYPE_ST);
96  break;
97  case CANITFBAUD_20K:
98  ret = CAN_Init(m_handle, CAN_BAUD_20K, CAN_INIT_TYPE_ST);
99  break;
100  case CANITFBAUD_10K:
101  ret = CAN_Init(m_handle, CAN_BAUD_10K, CAN_INIT_TYPE_ST);
102  break;
103  }
104 
105  if(ret)
106  {
107  std::cout << "CanPeakSys::CanPeakSys(), error in init" << std::endl;
108  }
109  else
110  {
111  std::cout << "CanPeakSys::CanpeakSys(), init ok" << std::endl;
112  m_bInitialized = true;
113  }
114 }
115 
116 //-------------------------------------------
117 bool CanPeakSys::transmitMsg(CanMsg CMsg, bool bBlocking)
118 {
119  TPCANMsg TPCMsg;
120  bool bRet = true;
121 
122  if (m_bInitialized == false) return false;
123 
124  // copy CMsg to TPCmsg
125  TPCMsg.LEN = CMsg.m_iLen;
126  TPCMsg.ID = CMsg.m_iID;
127  TPCMsg.MSGTYPE = CMsg.m_iType;
128  for(int i=0; i<8; i++)
129  TPCMsg.DATA[i] = CMsg.getAt(i);
130 
131  // write msg
132  int iRet;
133  iRet = CAN_Write(m_handle, &TPCMsg);
134  iRet = CAN_Status(m_handle);
135 
136  if(iRet < 0)
137  {
138  std::cout << "CanPeakSys::transmitMsg, errorcode= " << nGetLastError() << std::endl;
139  bRet = false;
140  }
141 
142 
143  return bRet;
144 }
145 
146 //-------------------------------------------
148 {
149  TPCANRdMsg TPCMsg;
150  TPCMsg.Msg.LEN = 8;
151  TPCMsg.Msg.MSGTYPE = 0;
152  TPCMsg.Msg.ID = 0;
153 
154  int iRet = CAN_ERR_OK;
155  bool bRet = false;
156 
157 
158  if (m_bInitialized == false) return false;
159 
160  iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0);
161 
162  if (iRet == CAN_ERR_OK)
163  {
164  pCMsg->m_iID = TPCMsg.Msg.ID;
165  pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
166  TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
167  bRet = true;
168  }
169  else if (CAN_Status(m_handle) != CAN_ERR_QRCVEMPTY)
170  {
171  std::cout << "CanPeakSys::receiveMsg ERROR: iRet = " << iRet << std::endl;
172  pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
173  }
174  else
175  {
176  // make sure there's never an undefined state (even when can drivers fail)
177  pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
178  }
179 
180  return bRet;
181 }
182 
183 //-------------------------------------------
184 bool CanPeakSys::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
185 {
186  int i, iRet;
187 
188  TPCANRdMsg TPCMsg;
189  TPCMsg.Msg.LEN = 8;
190  TPCMsg.Msg.MSGTYPE = 0;
191  TPCMsg.Msg.ID = 0;
192 
193  if (m_bInitialized == false) return false;
194 
195  // wait until msg in buffer
196  bool bRet = true;
197  iRet = CAN_ERR_OK;
198  i=0;
199  do
200  {
201  iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0);
202 
203  if(iRet == CAN_ERR_OK)
204  break;
205 
206  i++;
207  usleep(100000);
208  }
209  while(i < iNrOfRetry);
210 
211  // eval return value
212  if(iRet != CAN_ERR_OK)
213  {
214  std::cout << "CanPeakSys::receiveMsgRetry: " << strerror(errno) << std::endl;
215  pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
216  bRet = false;
217  }
218  else
219  {
220  pCMsg->m_iID = TPCMsg.Msg.ID;
221  pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
222  TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
223  }
224 
225  return bRet;
226 }
227 
228 //-------------------------------------------
229 bool CanPeakSys::receiveMsgTimeout(CanMsg* pCMsg, int nMicroSecTimeout)
230 {
231  int iRet = CAN_ERR_OK;
232 
233  TPCANRdMsg TPCMsg;
234  TPCMsg.Msg.LEN = 8;
235  TPCMsg.Msg.MSGTYPE = 0;
236  TPCMsg.Msg.ID = 0;
237 
238  if (m_bInitialized == false) return false;
239 
240  bool bRet = true;
241 
242  iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, nMicroSecTimeout);
243 
244  // eval return value
245  if(iRet != CAN_ERR_OK)
246  {
247  std::cout << "CANPeakSysUSB::receiveMsgRetry, errorcode= " << nGetLastError() << std::endl;
248  pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
249  bRet = false;
250  }
251  else
252  {
253  pCMsg->setID(TPCMsg.Msg.ID);
254  pCMsg->setLength(TPCMsg.Msg.LEN);
255  pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
256  TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
257  }
258 
259  return bRet;
260 }
bool receiveMsg(CanMsg *pCMsg)
Definition: CanPeakSys.cpp:147
void setLength(int len)
Definition: CanMsg.h:182
bool transmitMsg(CanMsg CMsg, bool bBlocking=true)
Definition: CanPeakSys.cpp:117
#define CANITFBAUD_500K
Definition: CanItf.h:33
#define CANITFBAUD_50K
Definition: CanItf.h:36
void init()
Definition: CanPeakSys.cpp:58
static const int c_iPort
Definition: CanPeakSys.h:50
bool init_ret()
Definition: CanPeakSys.cpp:51
Definition: CanMsg.h:28
bool m_bInitialized
Definition: CanPeakSys.h:45
bool receiveMsgTimeout(CanMsg *pCMsg, int nSecTimeout)
Definition: CanPeakSys.cpp:229
int m_iLen
Definition: CanMsg.h:36
#define CANITFBAUD_1M
Definition: CanItf.h:32
#define CANITFBAUD_250K
Definition: CanItf.h:34
static const int c_iInterrupt
Definition: CanPeakSys.h:49
int GetKeyInt(const char *pSect, const char *pKey, int *pValue, bool bWarnIfNotfound=true)
bool receiveMsgRetry(CanMsg *pCMsg, int iNrOfRetry)
Definition: CanPeakSys.cpp:184
void setID(int id)
Definition: CanMsg.h:163
int SetFileName(std::string fileName, std::string strIniFileUsedBy="", bool bCreate=false)
#define CANITFBAUD_20K
Definition: CanItf.h:37
HANDLE m_handle
Definition: CanPeakSys.h:43
int m_iType
Definition: CanMsg.h:38
int m_iID
Definition: CanMsg.h:34
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
int GetKeyString(const char *pSect, const char *pKey, std::string *pStrToRead, bool bWarnIfNotfound=true)
#define CANITFBAUD_10K
Definition: CanItf.h:38
#define CANITFBAUD_125K
Definition: CanItf.h:35
IniFile m_IniFile
Definition: CanPeakSys.h:46
int getAt(int iNr)
Definition: CanMsg.h:99
CanPeakSys(const char *cIniFile)
Definition: CanPeakSys.cpp:32


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