OpcUaCanOpen
CANopen OPC-UA server
pkcan.cpp
Go to the documentation of this file.
1 // This is the main DLL file.
2 
3 #include "pkcan.h"
4 
6 // kvCann.cpp: implementation of the KVCanScan class.
8 //#include "CCanCallback.h"
9 #include <time.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 bool initLibarary = false;
14 #ifdef WIN32
15  extern "C" __declspec(dllexport) CCanAccess *getCanbusAccess()
16 #else
18 #endif
19  {
20  CCanAccess *cc;
21  cc = new PKCanScan;
22 
23  return cc;
24  }
25 
26 #include "gettimeofday.h"
27 
29 {
30  return ;
31 }
32 
34 {
35  run_can = false;
36  CloseHandle(m_ReadEvent);
37  CAN_Uninitialize(canObjHandler);
38 }
39 
41 {
42 // DWORD waitStatus;
43  TPCANStatus Status;
44  TPCANMsg msg;
45  TPCANTimestamp time;
46  TPCANHandle xHandler;
47  CanMsgStruct cmt;
48 // struct timezone tm;
49 
50  PKCanScan *ppcs = reinterpret_cast<PKCanScan *>(pCanScan);
51  xHandler = ppcs->canObjHandler;
52  while ( ppcs->run_can ) {
53 // waitStatus = WaitForSingleObject(ppcs->m_ReadEvent,INFINITE);
54 // if (waitStatus == WAIT_OBJECT_0) {
55  Status = CAN_Read(xHandler,&msg,&time);
56  if (Status == PCAN_ERROR_OK) {
57  for(int i=0; i < 8; i++) cmt.c_data[i] = msg.DATA[i];
58  cmt.c_dlc = msg.LEN;
59  cmt.c_id = msg.ID;
60  cmt.c_ff = msg.MSGTYPE;
61 
62  unsigned long long mmsec = 0xFFFFFFFFUL;
63  mmsec = mmsec * 1000;
64  mmsec = mmsec * time.millis_overflow;
65  mmsec = mmsec + time.micros;
66  mmsec = mmsec + 1000 * time.millis;
67 
68  cmt.c_time.tv_usec = mmsec % 1000000UL;
69  cmt.c_time.tv_sec = mmsec / 1000000UL;
70  ppcs->canMessageCame(cmt);
71  }
72  else {
73  if (Status & PCAN_ERROR_QRCVEMPTY) {
74  continue;
75  }
76  ppcs->sendErrorCode(Status);
77  if (Status | PCAN_ERROR_ANYBUSERR) {
78  CAN_Initialize(xHandler,ppcs->BaudRate);
79  Sleep(100);
80  }
81  }
82 // }
83 // else break;
84  }
85 
86  ExitThread(0);
87  return 0;
88 }
89 
90 bool PKCanScan::configureCanboard(const char *name,const char *parameters)
91 {
92  TPCANStatus Status = PCAN_ERROR_OK;
93 
94  unsigned int tseg1 = 0;
95  unsigned int tseg2 = 0;
96  unsigned int sjw = 0;
97  unsigned int noSamp = 0;
98  unsigned int syncmode = 0;
99  BaudRate = PCAN_BAUD_125K;
100  long br;
101  int numPar;
102 
103  numPar = sscanf_s(parameters,"%d %d %d %d %d %d",&br,&tseg1,&tseg2,&sjw,&noSamp,&syncmode);
104 
105  /* Set baud rate to 125 Kbits/second. */
107 
108  if (numPar == 1) {
109  switch(br) {
110  case 50000: BaudRate = PCAN_BAUD_50K; break;
111  case 100000: BaudRate = PCAN_BAUD_100K; break;
112  case 125000: BaudRate = PCAN_BAUD_125K; break;
113  case 250000: BaudRate = PCAN_BAUD_250K; break;
114  case 500000: BaudRate = PCAN_BAUD_500K; break;
115  case 1000000: BaudRate = PCAN_BAUD_1M; break;
116  default: BaudRate = br;
117  }
118  }
119  else { if (numPar != 0) BaudRate = br; }
120  Status = CAN_Initialize(canObjHandler, BaudRate,256,3);
121  if (Status == PCAN_ERROR_OK) {
122  CAN_FilterMessages(canObjHandler,0,0x7FF,PCAN_MESSAGE_STANDARD);
123 
124  m_hCanScanThread = CreateThread(NULL, 0, CanScanControlThread,
125  this, 0, &m_idCanScanThread);
126  if (NULL == m_hCanScanThread) {
127  DebugBreak();
128  return false;
129  }
130  return true;
131  }
132 
133  return false;
134 }
135 
136 
137 bool PKCanScan::sendErrorCode(long status)
138 {
139  timeval ftTimeStamp;
140  if (status != bus_status) {
141  gettimeofday(&ftTimeStamp,0);
142  char *errMess;
143  getErrorMessage(status,&errMess);
144  canMessageError(status,errMess,ftTimeStamp );
145  bus_status = (TPCANStatus)status;
146 
147  }
148  if (status != PCAN_ERROR_OK) {
149  status = CAN_Reset(canObjHandler);
150  return false;
151  }
152  return true;
153 }
154 
155 bool PKCanScan::sendMessage(short cobID, unsigned char len, unsigned char *message)
156 {
157  TPCANStatus Status;
158  unsigned char *buf = message;
159  TPCANMsg pkmsg;
160  pkmsg.ID = cobID;
161  pkmsg.MSGTYPE = PCAN_MESSAGE_STANDARD;
162 
163  int l, l1;
164  l = len;
165  do {
166  if (l > 8) {
167  l1 = 8; l = l - 8;
168  }
169  else l1 = l;
170  pkmsg.LEN = l1;
171  memcpy(pkmsg.DATA,buf,l1);
172  Status = CAN_Write(canObjHandler, &pkmsg);
173  if (Status != PCAN_ERROR_OK) {
174  return sendErrorCode(Status);
175  }
176  buf = buf + l1;
177  }
178  while (l > 8);
179  return sendErrorCode(Status);
180 }
181 
183 {
184  TPCANStatus Status;
185 
186  TPCANMsg pkmsg;
187  pkmsg.ID = cobID;
188  pkmsg.MSGTYPE = PCAN_MESSAGE_RTR;
189  Status = CAN_Write(canObjHandler, &pkmsg);
190  return sendErrorCode(Status);
191 }
192 
193 bool PKCanScan::createBUS(const char *name ,const char *parameters )
194 {
195 
196  if (!configureCanboard(name,parameters))
197  return false;
198  // Initialization for Scan
199  return true;
200 }
201 
202 bool PKCanScan::getErrorMessage(long error, char **message)
203 {
204  char tmp[300];
205 
206  CAN_GetErrorText((TPCANStatus)error,0, tmp);
207  *message = new char[strlen(tmp)+1];
208  strcpy(*message,tmp);
209  return true;
210 }
211 
212 TPCANHandle PKCanScan::getHandle(const char *name)
213 {
214  char tmpName[256];
215  const char *chname[] = { "ISA1","ISA2","ISA3","ISA4","ISA5","ISA6","ISA7","ISA8",
216  "DNG1",
217  "PCI1","PCI2","PCI3","PCI4","PCI5","PCI6","PCI7","PCI8",
218  "USB1","USB2","USB3","USB4","USB5","USB6","USB7","USB8",
219  "PCC1","PCC2" };
220 
221  const TPCANHandle han[] = { 0x21, // PCAN-ISA interface, channel 1
222  0x22, // PCAN-ISA interface, channel 2
223  0x23, // PCAN-ISA interface, channel 3
224  0x24, // PCAN-ISA interface, channel 4
225  0x25, // PCAN-ISA interface, channel 5
226  0x26, // PCAN-ISA interface, channel 6
227  0x27, // PCAN-ISA interface, channel 7
228  0x28, // PCAN-ISA interface, channel 8
229 
230  0x31, // PCAN-Dongle/LPT interface, channel 1
231 
232  0x41, // PCAN-PCI interface, channel 1
233  0x42, // PCAN-PCI interface, channel 2
234  0x43, // PCAN-PCI interface, channel 3
235  0x44, // PCAN-PCI interface, channel 4
236  0x45, // PCAN-PCI interface, channel 5
237  0x46, // PCAN-PCI interface, channel 6
238  0x47, // PCAN-PCI interface, channel 7
239  0x48, // PCAN-PCI interface, channel 8
240 
241  0x51, // PCAN-USB interface, channel 1
242  0x52, // PCAN-USB interface, channel 2
243  0x53, // PCAN-USB interface, channel 3
244  0x54, // PCAN-USB interface, channel 4
245  0x55, // PCAN-USB interface, channel 5
246  0x56, // PCAN-USB interface, channel 6
247  0x57, // PCAN-USB interface, channel 7
248  0x58, // PCAN-USB interface, channel 8
249 
250  0x61, // PCAN-PC Card interface, channel 1
251  0x62 }; // PCAN-PC Card interface, channel 2
252 
253  int chn = sizeof(chname)/sizeof(chname[0]);
254 
255  for (unsigned int j=0; j < strlen(name); j++)
256  tmpName[j] = toupper(name[j]);
257 
258  for (int i = 0; i < chn; i++)
259  {
260  if (strcmp(chname[i],tmpName) >= 0)
261  return han[i];
262  }
263 
264  return 0;
265 }