Skip to content

Commit 918115e

Browse files
authored
Merge pull request #2136 from Reivax851/development
New Version UBStin / Multibloc_V8 (SFSP)
2 parents 39eb525 + 51207be commit 918115e

File tree

5 files changed

+601
-283
lines changed

5 files changed

+601
-283
lines changed

hardware/USBtin.cpp

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ Description : This class manage the USBtin CAN gateway.
88
- Receiving CAN Frame and switching then to appropriate CAN Layer
99
- Sending CAN Frame with writeframe, writeframe is virtualized inside each CAN Layer
1010
Supported Layer :
11-
* MultiblocV8 CAN : Scheiber spécific communication
11+
* MultiblocV8 CAN : Scheiber spécific communication
1212
1313
History :
1414
- 2017-10-01 : Creation by X.PONCET
1515
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+
1619
*/
1720
#include "stdafx.h"
1821
#include "USBtin.h"
@@ -72,10 +75,10 @@ m_szSerialPort(devname)
7275
{
7376
m_HwdID = ID;
7477
m_stoprequested=false;
75-
m_retrycntr=USBTIN_RETRY_DELAY*5;
78+
m_USBtinRetrycntr=USBTIN_RETRY_DELAY*5;
7679
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;
7982
Init();
8083
}
8184

@@ -85,14 +88,14 @@ USBtin::~USBtin(void){
8588

8689
void USBtin::Init()
8790
{
88-
m_bufferpos = 0;
91+
m_USBtinBufferpos = 0;
8992
}
9093

9194
bool USBtin::StartHardware()
9295
{
9396
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
9699
m_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&USBtin::Do_Work, this)));
97100
return (m_thread!=NULL);
98101
}
@@ -116,50 +119,50 @@ bool USBtin::StopHardware()
116119

117120
void USBtin::Do_Work()
118121
{
119-
int sec_counter = 0;
122+
int m_V8secCounterBase = 0;
120123
int msec_counter = 0;
121-
EtapeInitCan = 0;
124+
m_EtapeInitCan = 0;
122125

123126
while (!m_stoprequested)
124127
{
125128
sleep_milliseconds(TIME_200ms);
126129

127130
if (m_stoprequested){
128-
EtapeInitCan = 0;
131+
m_EtapeInitCan = 0;
129132
break;
130133
}
131134

132135
msec_counter++;
133136
if (msec_counter == 5)
134137
{
135138
msec_counter = 0;
136-
sec_counter++;
139+
m_V8secCounterBase++;
137140

138-
if (sec_counter % 12 == 0) {
141+
if (m_V8secCounterBase % 12 == 0) {
139142
m_LastHeartbeat = mytime(NULL);
140143
}
141144

142145
if (isOpen()) //Serial port open, we can initiate the Can BUS :
143146
{
144-
switch(EtapeInitCan){
147+
switch(m_EtapeInitCan){
145148
case 0 :
146149
_log.Log(LOG_STATUS, "USBtin: Serial port is now open !");
147150
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++;
151154
break;
152155
case 1 :
153156
GetHWVersion();
154-
EtapeInitCan++;
157+
m_EtapeInitCan++;
155158
break;
156159
case 2 :
157160
GetFWVersion();
158-
EtapeInitCan++;
161+
m_EtapeInitCan++;
159162
break;
160163
case 3 :
161164
GetSerialNumber();
162-
EtapeInitCan++;
165+
m_EtapeInitCan++;
163166
break;
164167
case 4 :
165168
SetBaudRate250Kbd();
@@ -168,13 +171,16 @@ void USBtin::Do_Work()
168171
if( (Bus_CANType&FreeCan) == FreeCan ) _log.Log(LOG_STATUS, "USBtin: FreeCAN is Selected !");
169172

170173
if( Bus_CANType == 0 ) _log.Log(LOG_ERROR, "USBtin: WARNING: No Can management Selected !");
171-
EtapeInitCan++;
174+
m_EtapeInitCan++;
172175
break;
173176
case 5 : //openning can port :
174177
//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+
}
176182
OpenCanPort();
177-
EtapeInitCan++;
183+
m_EtapeInitCan++;
178184
break;
179185

180186
case 6 ://All is good !
@@ -188,16 +194,16 @@ void USBtin::Do_Work()
188194

189195
if (!isOpen()) //serial not open
190196
{
191-
if (m_retrycntr==0)
197+
if (m_USBtinRetrycntr==0)
192198
{
193199
_log.Log(LOG_STATUS,"USBtin: serial retrying in %d seconds...", USBTIN_RETRY_DELAY);
194200
}
195-
m_retrycntr++;
196-
if (m_retrycntr/5>=USBTIN_RETRY_DELAY)
201+
m_USBtinRetrycntr++;
202+
if (m_USBtinRetrycntr/5>=USBTIN_RETRY_DELAY)
197203
{
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;
201207
OpenSerialDevice();
202208
}
203209
}
@@ -232,8 +238,8 @@ bool USBtin::OpenSerialDevice()
232238
}
233239

234240
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));
237243
setReadCallback(boost::bind(&USBtin::readCallback, this, _1, _2));
238244

239245
sOnConnected(this);
@@ -246,7 +252,7 @@ void USBtin::readCallback(const char *data, size_t len)
246252
boost::lock_guard<boost::mutex> l(readQueueMutex);
247253
if (!m_bEnableReceive)
248254
return; //receiving not enabled
249-
if (len > sizeof(m_bufferUSBtin)){
255+
if (len > sizeof(m_USBtinBuffer)){
250256
_log.Log(LOG_ERROR,"USBtin: Warning Error buffer size reaches/ maybe Can is overrun...");
251257
return;
252258
}
@@ -257,58 +263,52 @@ void USBtin::ParseData(const char *pData, int Len)
257263
{
258264
char value[30] = "";
259265
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+
267267
int ii = 0;
268268
while (ii<Len)
269269
{
270-
m_bufferUSBtin[m_bufferpos] = pData[ii];
270+
m_USBtinBuffer[m_USBtinBufferpos] = pData[ii];
271271
// 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] ){
273273
//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 !
276276
_log.Log(LOG_ERROR,"USBtin: 3x times BEL signal receive : restart gateway ");
277277
Restart();
278278
}
279279
else{
280280
_log.Log(LOG_ERROR,"USBtin: BEL signal (commande allready active or Gateway error) ! ");
281281
}
282-
m_bufferpos = 0;
282+
m_USBtinBufferpos = 0;
283283
}
284284
//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] )
286286
{
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);
290290
_log.Log(LOG_STATUS,"USBtin: Hardware Version: %s", value);
291291

292292
}
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);
295295
_log.Log(LOG_STATUS,"USBtin: Firware Version: %s", value);
296296
}
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);
299299
_log.Log(LOG_STATUS,"USBtin: Serial Number: %s", value);
300300
}
301-
else if( m_bufferUSBtin[0] == USBTIN_CR ){
301+
else if( m_USBtinBuffer[0] == USBTIN_CR ){
302302
_log.Log(LOG_STATUS,"USBtin: return OK :-)");
303303
}
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
306306
int IDhexNumber;
307307
sscanf(value, "%x", &IDhexNumber); //IDhexNumber now contains the the digital value of the Ext ID
308308

309309
memset(&value[0], 0, sizeof(value));
310310

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)
312312
int DLChexNumber;
313313
sscanf(value, "%x", &DLChexNumber);
314314

@@ -325,7 +325,7 @@ void USBtin::ParseData(const char *pData, int Len)
325325
for(i=0;i<=DLChexNumber;i++){
326326
ValData = 0;
327327

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
329329
sscanf(value, "%x", &ValData);
330330

331331
Buffer_Octets[i]=ValData;
@@ -336,30 +336,30 @@ void USBtin::ParseData(const char *pData, int Len)
336336
if( (Bus_CANType&Multibloc_V8) == Multibloc_V8 ){ //multibloc V8 Management !
337337
Traitement_MultiblocV8(IDhexNumber,DLChexNumber,Buffer_Octets);
338338
//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);
340340
}
341341

342342
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);
344344
}
345345

346346
}
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)
348348
//_log.Log(LOG_NORM,"USBtin: Normale Frame receive : #%s#",m_buffer);
349349
}
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 ){
351351
//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
353353
//if the CAN communication is in good life ;-)
354354
}
355355
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);
357357
}
358358
//rreset of the pointer here
359-
m_bufferpos = 0;
359+
m_USBtinBufferpos = 0;
360360
}
361361
else{
362-
m_bufferpos++;
362+
m_USBtinBufferpos++;
363363
}
364364

365365
ii++;
@@ -372,7 +372,7 @@ bool USBtin::writeFrame(const std::string & data)
372372
if (!isOpen()){
373373
return false;
374374
}
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());
376376
std::string frame;
377377
frame.append(data);
378378
frame.append("\x0D"); //add the "carry return" at end

hardware/USBtin.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,27 @@ class USBtin : public USBtin_MultiblocV8,AsyncSerial
3030
~USBtin();
3131
std::string m_szSerialPort;
3232
void Restart();
33-
33+
unsigned int Bus_CANType;
34+
unsigned long switch_id_base;
3435

3536
private:
36-
bool StartHardware();
37-
bool StopHardware();
38-
3937
volatile bool m_stoprequested;
40-
bool OpenSerialDevice();
41-
unsigned int EtapeInitCan;
42-
43-
int m_retrycntr;
44-
int BelErrorCount;
45-
char m_bufferUSBtin[390]; //buffer capable de stocker 15 trames en 1 fois
46-
int m_bufferpos;
47-
unsigned int Bus_CANType;
48-
bool BOOL_Debug; //1 = activ
38+
unsigned int m_EtapeInitCan;
39+
int m_USBtinRetrycntr;
40+
int m_USBtinBelErrorCount;
41+
char m_USBtinBuffer[390]; //buffer capable de stocker 15 trames en 1 fois
42+
int m_USBtinBufferpos;
43+
bool m_BOOL_USBtinDebug; //1 = activ
4944

5045
boost::asio::serial_port_base::parity m_iOptParity;
5146
boost::asio::serial_port_base::character_size m_iOptCsize;
5247
boost::asio::serial_port_base::flow_control m_iOptFlow;
5348
boost::asio::serial_port_base::stop_bits m_iOptStop;
54-
5549
boost::shared_ptr<boost::thread> m_thread;
50+
51+
bool StartHardware();
52+
bool StopHardware();
53+
bool OpenSerialDevice();
5654
void Do_Work();
5755
// Read callback, stores data in the buffer
5856
void readCallback(const char *data, size_t len);
@@ -66,4 +64,5 @@ class USBtin : public USBtin_MultiblocV8,AsyncSerial
6664
void SetBaudRate250Kbd();
6765
void OpenCanPort();
6866
void CloseCanPort();
67+
6968
};

0 commit comments

Comments
 (0)