121 UNS8 data; |
121 UNS8 data; |
122 TPCANMsg peakMsg; |
122 TPCANMsg peakMsg; |
123 TPCANTimestamp peakRcvTime; |
123 TPCANTimestamp peakRcvTime; |
124 DWORD Res; |
124 DWORD Res; |
125 DWORD result; |
125 DWORD result; |
126 |
126 // loop until valid message or fatal error |
127 #ifdef PCAN2_HEADER_ |
127 do{ |
128 // if not the first handler |
128 #ifdef PCAN2_HEADER_ |
129 if(second_board == (s_BOARD *)fd0) { |
129 // if not the first handler |
130 //wait for CAN msg... |
130 if(second_board == (s_BOARD *)fd0) { |
131 result = WaitForSingleObject(hEvent2, INFINITE); |
131 //wait for CAN msg... |
132 if (result == WAIT_OBJECT_0) |
132 result = WaitForSingleObject(hEvent2, INFINITE); |
133 Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); |
133 if (result == WAIT_OBJECT_0) |
134 if(Res == (CAN_ERR_QRCVEMPTY | CAN_ERRMASK_ILLHANDLE)) |
134 Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); |
135 { |
135 // Exit receive thread when handle is no more valid |
136 ResetEvent(hEvent2); |
136 if(Res & CAN_ERRMASK_ILLHANDLE) |
|
137 return 1; |
|
138 } |
|
139 else |
|
140 #endif |
|
141 |
|
142 // We read the queue looking for messages. |
|
143 if(first_board == (s_BOARD *)fd0) { |
|
144 result = WaitForSingleObject(hEvent1, INFINITE); |
|
145 if (result == WAIT_OBJECT_0) |
|
146 { |
|
147 Res = CAN_ReadEx(&peakMsg, &peakRcvTime); |
|
148 // Exit receive thread when handle is no more valid |
|
149 if(Res & CAN_ERRMASK_ILLHANDLE) |
|
150 return 1; |
|
151 } |
|
152 } |
|
153 else |
|
154 Res = CAN_ERR_BUSOFF; |
|
155 |
|
156 // A message was received : we process the message(s) |
|
157 if (Res == CAN_ERR_OK) |
|
158 { |
|
159 // if something different that 11bit or rtr... problem |
|
160 if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) |
|
161 { |
|
162 if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) |
|
163 { |
|
164 printf ("!!! Peak board read : re-init\n"); |
|
165 canInit((s_BOARD*) fd0); |
|
166 usleep (10000); |
|
167 } |
|
168 |
|
169 // If status, return status if 29bit, return overrun |
|
170 return peakMsg.MSGTYPE == |
|
171 MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; |
|
172 } |
|
173 m->cob_id = peakMsg.ID; |
|
174 |
|
175 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ |
|
176 m->rtr = 0; |
|
177 else |
|
178 m->rtr = 1; |
|
179 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
|
180 for (data = 0; data < peakMsg.LEN; data++) |
|
181 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
|
182 #if defined DEBUG_MSG_CONSOLE_ON |
|
183 MSG("in : "); |
|
184 print_message(m); |
|
185 #endif |
|
186 } |
|
187 else |
|
188 { |
|
189 // not benign error => fatal error |
|
190 if (!(Res & CAN_ERR_QRCVEMPTY |
|
191 || Res & CAN_ERR_BUSLIGHT |
|
192 || Res & CAN_ERR_BUSHEAVY)) |
|
193 { |
|
194 printf ("canReceive returned error (%d)\n", Res); |
137 return 1; |
195 return 1; |
138 } |
196 } |
139 } |
197 } |
140 else |
198 }while(Res != CAN_ERR_OK); |
141 #endif |
|
142 |
|
143 // We read the queue looking for messages. |
|
144 if(first_board == (s_BOARD *)fd0) { |
|
145 result = WaitForSingleObject(hEvent1, INFINITE); |
|
146 if (result == WAIT_OBJECT_0) |
|
147 { |
|
148 Res = CAN_ReadEx(&peakMsg, &peakRcvTime); |
|
149 if(Res == (CAN_ERR_QRCVEMPTY | CAN_ERRMASK_ILLHANDLE)) |
|
150 { |
|
151 ResetEvent(hEvent1); |
|
152 return 1; |
|
153 } |
|
154 } |
|
155 } |
|
156 else |
|
157 Res = CAN_ERR_BUSOFF; |
|
158 |
|
159 // A message was received : we process the message(s) |
|
160 if (Res == CAN_ERR_OK) |
|
161 { |
|
162 // if something different that 11bit or rtr... problem |
|
163 if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) |
|
164 { |
|
165 if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) |
|
166 { |
|
167 printf ("!!! Peak board read : re-init\n"); |
|
168 canInit((s_BOARD*) fd0); |
|
169 usleep (10000); |
|
170 } |
|
171 |
|
172 // If status, return status if 29bit, return overrun |
|
173 return peakMsg.MSGTYPE == |
|
174 MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; |
|
175 } |
|
176 m->cob_id = peakMsg.ID; |
|
177 |
|
178 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ |
|
179 m->rtr = 0; |
|
180 else |
|
181 m->rtr = 1; |
|
182 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
|
183 for (data = 0; data < peakMsg.LEN; data++) |
|
184 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
|
185 #if defined DEBUG_MSG_CONSOLE_ON |
|
186 MSG("in : "); |
|
187 print_message(m); |
|
188 #endif |
|
189 } |
|
190 else |
|
191 { |
|
192 if (!(Res & CAN_ERR_QRCVEMPTY |
|
193 || Res & CAN_ERR_BUSLIGHT |
|
194 || Res & CAN_ERR_BUSHEAVY)) |
|
195 { |
|
196 printf ("canReceive returned error (%d)\n", Res); |
|
197 return 1; |
|
198 } |
|
199 } |
|
200 return 0; |
199 return 0; |
201 } |
200 } |
202 |
201 |
203 /***************************************************************************/ |
202 /***************************************************************************/ |
204 UNS8 canSend_driver (CAN_HANDLE fd0, Message * m) |
203 UNS8 canSend_driver (CAN_HANDLE fd0, Message * m) |