@@ -8,11 +8,14 @@ Description : This class manage the USBtin CAN gateway.
8
8
- Receiving CAN Frame and switching then to appropriate CAN Layer
9
9
- Sending CAN Frame with writeframe, writeframe is virtualized inside each CAN Layer
10
10
Supported Layer :
11
- * MultiblocV8 CAN : Scheiber spécific communication
11
+ * MultiblocV8 CAN : Scheiber spécific communication
12
12
13
13
History :
14
14
- 2017-10-01 : Creation by X.PONCET
15
15
16
+ - 2018-01-22 : Update :
17
+ # for feature add to MultiblocV8: manual creation up to 127 virtual switch, ability to learn eatch switch to any blocks output
18
+
16
19
*/
17
20
#include " stdafx.h"
18
21
#include " USBtin.h"
@@ -72,10 +75,10 @@ m_szSerialPort(devname)
72
75
{
73
76
m_HwdID = ID;
74
77
m_stoprequested=false ;
75
- m_retrycntr =USBTIN_RETRY_DELAY*5 ;
78
+ m_USBtinRetrycntr =USBTIN_RETRY_DELAY*5 ;
76
79
Bus_CANType = BusCanType;
77
- if ( DebugMode == 0 )BOOL_Debug = false ;
78
- else BOOL_Debug = true ;
80
+ if ( DebugMode == 0 )m_BOOL_USBtinDebug = false ;
81
+ else m_BOOL_USBtinDebug = true ;
79
82
Init ();
80
83
}
81
84
@@ -85,14 +88,14 @@ USBtin::~USBtin(void){
85
88
86
89
void USBtin::Init ()
87
90
{
88
- m_bufferpos = 0 ;
91
+ m_USBtinBufferpos = 0 ;
89
92
}
90
93
91
94
bool USBtin::StartHardware ()
92
95
{
93
96
m_stoprequested = false ;
94
- BelErrorCount = 0 ;
95
- m_retrycntr =USBTIN_RETRY_DELAY*5 ; // will force reconnect first thing
97
+ m_USBtinBelErrorCount = 0 ;
98
+ m_USBtinRetrycntr =USBTIN_RETRY_DELAY*5 ; // will force reconnect first thing
96
99
m_thread = boost::shared_ptr<boost::thread>(new boost::thread (boost::bind (&USBtin::Do_Work, this )));
97
100
return (m_thread!=NULL );
98
101
}
@@ -116,50 +119,50 @@ bool USBtin::StopHardware()
116
119
117
120
void USBtin::Do_Work ()
118
121
{
119
- int sec_counter = 0 ;
122
+ int m_V8secCounterBase = 0 ;
120
123
int msec_counter = 0 ;
121
- EtapeInitCan = 0 ;
124
+ m_EtapeInitCan = 0 ;
122
125
123
126
while (!m_stoprequested)
124
127
{
125
128
sleep_milliseconds (TIME_200ms);
126
129
127
130
if (m_stoprequested){
128
- EtapeInitCan = 0 ;
131
+ m_EtapeInitCan = 0 ;
129
132
break ;
130
133
}
131
134
132
135
msec_counter++;
133
136
if (msec_counter == 5 )
134
137
{
135
138
msec_counter = 0 ;
136
- sec_counter ++;
139
+ m_V8secCounterBase ++;
137
140
138
- if (sec_counter % 12 == 0 ) {
141
+ if (m_V8secCounterBase % 12 == 0 ) {
139
142
m_LastHeartbeat = mytime (NULL );
140
143
}
141
144
142
145
if (isOpen ()) // Serial port open, we can initiate the Can BUS :
143
146
{
144
- switch (EtapeInitCan ){
147
+ switch (m_EtapeInitCan ){
145
148
case 0 :
146
149
_log.Log (LOG_STATUS, " USBtin: Serial port is now open !" );
147
150
CloseCanPort (); // more cleaner to close in first, sometimes the gateway maybe already open...
148
- memset (&m_bufferUSBtin ,0 ,sizeof (m_bufferUSBtin ));
149
- BelErrorCount = 0 ;
150
- EtapeInitCan ++;
151
+ memset (&m_USBtinBuffer ,0 ,sizeof (m_USBtinBuffer ));
152
+ m_USBtinBelErrorCount = 0 ;
153
+ m_EtapeInitCan ++;
151
154
break ;
152
155
case 1 :
153
156
GetHWVersion ();
154
- EtapeInitCan ++;
157
+ m_EtapeInitCan ++;
155
158
break ;
156
159
case 2 :
157
160
GetFWVersion ();
158
- EtapeInitCan ++;
161
+ m_EtapeInitCan ++;
159
162
break ;
160
163
case 3 :
161
164
GetSerialNumber ();
162
- EtapeInitCan ++;
165
+ m_EtapeInitCan ++;
163
166
break ;
164
167
case 4 :
165
168
SetBaudRate250Kbd ();
@@ -168,13 +171,16 @@ void USBtin::Do_Work()
168
171
if ( (Bus_CANType&FreeCan) == FreeCan ) _log.Log (LOG_STATUS, " USBtin: FreeCAN is Selected !" );
169
172
170
173
if ( Bus_CANType == 0 ) _log.Log (LOG_ERROR, " USBtin: WARNING: No Can management Selected !" );
171
- EtapeInitCan ++;
174
+ m_EtapeInitCan ++;
172
175
break ;
173
176
case 5 : // openning can port :
174
177
// Activate the good CAN Layer :
175
- if ( (Bus_CANType&Multibloc_V8) == Multibloc_V8 ){ ManageThreadV8 (true );}
178
+ if ( (Bus_CANType&Multibloc_V8) == Multibloc_V8 ){
179
+ ManageThreadV8 (true );
180
+ switch_id_base = m_V8switch_id_base;
181
+ }
176
182
OpenCanPort ();
177
- EtapeInitCan ++;
183
+ m_EtapeInitCan ++;
178
184
break ;
179
185
180
186
case 6 :// All is good !
@@ -188,16 +194,16 @@ void USBtin::Do_Work()
188
194
189
195
if (!isOpen ()) // serial not open
190
196
{
191
- if (m_retrycntr ==0 )
197
+ if (m_USBtinRetrycntr ==0 )
192
198
{
193
199
_log.Log (LOG_STATUS," USBtin: serial retrying in %d seconds..." , USBTIN_RETRY_DELAY);
194
200
}
195
- m_retrycntr ++;
196
- if (m_retrycntr /5 >=USBTIN_RETRY_DELAY)
201
+ m_USBtinRetrycntr ++;
202
+ if (m_USBtinRetrycntr /5 >=USBTIN_RETRY_DELAY)
197
203
{
198
- m_retrycntr =0 ;
199
- m_bufferpos = 0 ;
200
- m_bufferUSBtin[m_bufferpos ] = 0 ;
204
+ m_USBtinRetrycntr =0 ;
205
+ m_USBtinBufferpos = 0 ;
206
+ m_USBtinBuffer[m_USBtinBufferpos ] = 0 ;
201
207
OpenSerialDevice ();
202
208
}
203
209
}
@@ -232,8 +238,8 @@ bool USBtin::OpenSerialDevice()
232
238
}
233
239
234
240
m_bIsStarted = true ;
235
- m_bufferpos = 0 ;
236
- memset (&m_bufferUSBtin ,0 ,sizeof (m_bufferUSBtin ));
241
+ m_USBtinBufferpos = 0 ;
242
+ memset (&m_USBtinBuffer ,0 ,sizeof (m_USBtinBuffer ));
237
243
setReadCallback (boost::bind (&USBtin::readCallback, this , _1, _2));
238
244
239
245
sOnConnected (this );
@@ -246,7 +252,7 @@ void USBtin::readCallback(const char *data, size_t len)
246
252
boost::lock_guard<boost::mutex> l (readQueueMutex);
247
253
if (!m_bEnableReceive)
248
254
return ; // receiving not enabled
249
- if (len > sizeof (m_bufferUSBtin )){
255
+ if (len > sizeof (m_USBtinBuffer )){
250
256
_log.Log (LOG_ERROR," USBtin: Warning Error buffer size reaches/ maybe Can is overrun..." );
251
257
return ;
252
258
}
@@ -257,58 +263,52 @@ void USBtin::ParseData(const char *pData, int Len)
257
263
{
258
264
char value[30 ] = " " ;
259
265
std::string vString;
260
- // unsigned long ulValue;
261
-
262
- // unsigned long IdValue;
263
- // unsigned int DlcValue;
264
- // unsigned int Data[8];
265
-
266
- // int i,j;
266
+
267
267
int ii = 0 ;
268
268
while (ii<Len)
269
269
{
270
- m_bufferUSBtin[m_bufferpos ] = pData[ii];
270
+ m_USBtinBuffer[m_USBtinBufferpos ] = pData[ii];
271
271
// BEL signal received : appears if the command is allready active or if errors occured on CAN side...
272
- if ( USBTIN_BELSIGNAL == m_bufferUSBtin[m_bufferpos ] ){
272
+ if ( USBTIN_BELSIGNAL == m_USBtinBuffer[m_USBtinBufferpos ] ){
273
273
// reset first char
274
- BelErrorCount ++;
275
- if ( BelErrorCount > 3 ){ // If more than 3 BEL receive : restart the Gateway !
274
+ m_USBtinBelErrorCount ++;
275
+ if ( m_USBtinBelErrorCount > 3 ){ // If more than 3 BEL receive : restart the Gateway !
276
276
_log.Log (LOG_ERROR," USBtin: 3x times BEL signal receive : restart gateway " );
277
277
Restart ();
278
278
}
279
279
else {
280
280
_log.Log (LOG_ERROR," USBtin: BEL signal (commande allready active or Gateway error) ! " );
281
281
}
282
- m_bufferpos = 0 ;
282
+ m_USBtinBufferpos = 0 ;
283
283
}
284
284
// else CR receive = good reception of a response :
285
- else if ( USBTIN_CR == m_bufferUSBtin[m_bufferpos ] )
285
+ else if ( USBTIN_CR == m_USBtinBuffer[m_USBtinBufferpos ] )
286
286
{
287
- BelErrorCount = 0 ;
288
- if ( m_bufferUSBtin [0 ] == USBTIN_HARDWARE_VERSION ){
289
- strncpy (value, (char *)&(m_bufferUSBtin [1 ]), 4 );
287
+ m_USBtinBelErrorCount = 0 ;
288
+ if ( m_USBtinBuffer [0 ] == USBTIN_HARDWARE_VERSION ){
289
+ strncpy (value, (char *)&(m_USBtinBuffer [1 ]), 4 );
290
290
_log.Log (LOG_STATUS," USBtin: Hardware Version: %s" , value);
291
291
292
292
}
293
- else if ( m_bufferUSBtin [0 ] == USBTIN_FIRMWARE_VERSION ){
294
- strncpy (value, (char *)&(m_bufferUSBtin [1 ]), 4 );
293
+ else if ( m_USBtinBuffer [0 ] == USBTIN_FIRMWARE_VERSION ){
294
+ strncpy (value, (char *)&(m_USBtinBuffer [1 ]), 4 );
295
295
_log.Log (LOG_STATUS," USBtin: Firware Version: %s" , value);
296
296
}
297
- else if ( m_bufferUSBtin [0 ] == USBTIN_SERIAL_NUMBER ){
298
- strncpy (value, (char *)&(m_bufferUSBtin [1 ]), 4 );
297
+ else if ( m_USBtinBuffer [0 ] == USBTIN_SERIAL_NUMBER ){
298
+ strncpy (value, (char *)&(m_USBtinBuffer [1 ]), 4 );
299
299
_log.Log (LOG_STATUS," USBtin: Serial Number: %s" , value);
300
300
}
301
- else if ( m_bufferUSBtin [0 ] == USBTIN_CR ){
301
+ else if ( m_USBtinBuffer [0 ] == USBTIN_CR ){
302
302
_log.Log (LOG_STATUS," USBtin: return OK :-)" );
303
303
}
304
- else if ( m_bufferUSBtin [0 ] == USBTIN_EXT_TRAME_RECEIVE ){ // Receive Extended Frame :
305
- strncpy (value, (char *)&(m_bufferUSBtin [1 ]), 8 ); // take the "Extended ID" CAN parts and paste it in the char table
304
+ else if ( m_USBtinBuffer [0 ] == USBTIN_EXT_TRAME_RECEIVE ){ // Receive Extended Frame :
305
+ strncpy (value, (char *)&(m_USBtinBuffer [1 ]), 8 ); // take the "Extended ID" CAN parts and paste it in the char table
306
306
int IDhexNumber;
307
307
sscanf (value, " %x" , &IDhexNumber); // IDhexNumber now contains the the digital value of the Ext ID
308
308
309
309
memset (&value[0 ], 0 , sizeof (value));
310
310
311
- strncpy (value, (char *)&(m_bufferUSBtin [9 ]), 1 ); // read the DLC (lenght of message)
311
+ strncpy (value, (char *)&(m_USBtinBuffer [9 ]), 1 ); // read the DLC (lenght of message)
312
312
int DLChexNumber;
313
313
sscanf (value, " %x" , &DLChexNumber);
314
314
@@ -325,7 +325,7 @@ void USBtin::ParseData(const char *pData, int Len)
325
325
for (i=0 ;i<=DLChexNumber;i++){
326
326
ValData = 0 ;
327
327
328
- strncpy (value, (char *)&(m_bufferUSBtin [10 +(2 *i)]), 2 ); // to fill the Buffer of 8 bytes
328
+ strncpy (value, (char *)&(m_USBtinBuffer [10 +(2 *i)]), 2 ); // to fill the Buffer of 8 bytes
329
329
sscanf (value, " %x" , &ValData);
330
330
331
331
Buffer_Octets[i]=ValData;
@@ -336,30 +336,30 @@ void USBtin::ParseData(const char *pData, int Len)
336
336
if ( (Bus_CANType&Multibloc_V8) == Multibloc_V8 ){ // multibloc V8 Management !
337
337
Traitement_MultiblocV8 (IDhexNumber,DLChexNumber,Buffer_Octets);
338
338
// So in debug mode we can check good reception after treatment :
339
- if ( BOOL_Debug == true ) _log.Log (LOG_NORM," USBtin: Traitement trame multiblocV8 : #%s#" ,m_bufferUSBtin );
339
+ if ( m_BOOL_USBtinDebug == true ) _log.Log (LOG_NORM," USBtin: Traitement trame multiblocV8 : #%s#" ,m_USBtinBuffer );
340
340
}
341
341
342
342
if ( Bus_CANType == 0 ){ // No management !
343
- if ( BOOL_Debug == true ) _log.Log (LOG_NORM," USBtin: Frame receive not managed: #%s#" ,m_bufferUSBtin );
343
+ if ( m_BOOL_USBtinDebug == true ) _log.Log (LOG_NORM," USBtin: Frame receive not managed: #%s#" ,m_USBtinBuffer );
344
344
}
345
345
346
346
}
347
- else if ( m_bufferUSBtin [0 ] == USBTIN_NOR_TRAME_RECEIVE ){ // Receive Normale Frame (ie: ID is not extended)
347
+ else if ( m_USBtinBuffer [0 ] == USBTIN_NOR_TRAME_RECEIVE ){ // Receive Normale Frame (ie: ID is not extended)
348
348
// _log.Log(LOG_NORM,"USBtin: Normale Frame receive : #%s#",m_buffer);
349
349
}
350
- else if ( m_bufferUSBtin [0 ] == USBTIN_GOODSENDING_NOR_TRAME || m_bufferUSBtin [0 ] == USBTIN_GOODSENDING_EXT_TRAME ){
350
+ else if ( m_USBtinBuffer [0 ] == USBTIN_GOODSENDING_NOR_TRAME || m_USBtinBuffer [0 ] == USBTIN_GOODSENDING_EXT_TRAME ){
351
351
// The Gateway answers USBTIN_GOODSENDING_NOR_TRAME or USBTIN_GOODSENDING_EXT_TRAME each times a frame is sent correctly
352
- if ( BOOL_Debug == true ) _log.Log (LOG_NORM," USBtin: Frame Send OK !" ); // So in debug mode we CAN check it, convenient way to check
352
+ if ( m_BOOL_USBtinDebug == true ) _log.Log (LOG_NORM," USBtin: Frame Send OK !" ); // So in debug mode we CAN check it, convenient way to check
353
353
// if the CAN communication is in good life ;-)
354
354
}
355
355
else { // over things here...
356
- if ( BOOL_Debug == true ) _log.Log (LOG_ERROR," USBtin: receive command not supported : #%s#" , m_bufferUSBtin );
356
+ if ( m_BOOL_USBtinDebug == true ) _log.Log (LOG_ERROR," USBtin: receive command not supported : #%s#" , m_USBtinBuffer );
357
357
}
358
358
// rreset of the pointer here
359
- m_bufferpos = 0 ;
359
+ m_USBtinBufferpos = 0 ;
360
360
}
361
361
else {
362
- m_bufferpos ++;
362
+ m_USBtinBufferpos ++;
363
363
}
364
364
365
365
ii++;
@@ -372,7 +372,7 @@ bool USBtin::writeFrame(const std::string & data)
372
372
if (!isOpen ()){
373
373
return false ;
374
374
}
375
- if ( BOOL_Debug == true ) _log.Log (LOG_NORM," USBtin: write frame to Can Gateway: #%s# " ,data.c_str ());
375
+ if ( m_BOOL_USBtinDebug == true ) _log.Log (LOG_NORM," USBtin: write frame to Can Gateway: #%s# " ,data.c_str ());
376
376
std::string frame;
377
377
frame.append (data);
378
378
frame.append (" \x0D " ); // add the "carry return" at end
0 commit comments