@@ -118,6 +118,7 @@ char KW1281_dv::KWPReceiveBlock(char receivedMessage[], int maxMessageSize, int
118118 }
119119
120120 unsigned long timeout = millis () + 1000 ;
121+
121122 while ((receivedMessageCounter == 0 ) || (receivedMessageCounter != receivedMessageSize)) {
122123 while (obd->available ()) {
123124 data = obdRead ();
@@ -255,12 +256,6 @@ char KW1281_dv::KWPReadAscii(char id[], uint16_t &coding, uint32_t &wsc) {
255256
256257 coding = ((b1 << 8 ) + ((b2 >> 4 ) << 4 )) >> 1 ;
257258 wsc = (((uint32_t )b2 & 0xF ) << 16 ) + (b3 << 8 ) + b4;
258- #ifdef DEBUG
259- Serial.print (F (" Coding: " ));
260- Serial.print (coding);
261- Serial.print (F (" , WSC: " ));
262- Serial.println (wsc);
263- #endif
264259 break ;
265260 } else {
266261 if (first) {
@@ -282,6 +277,40 @@ char KW1281_dv::KWPReadAscii(char id[], uint16_t &coding, uint32_t &wsc) {
282277 return RETURN_SUCCESS ;
283278}
284279
280+ char KW1281_dv::KWPReadAscii (uint16_t &coding, uint32_t &wsc) {
281+ #ifdef DEBUG_SHOW_CURRENT_FUNCTION
282+ Serial.println (F (" KWPReadAscii" ));
283+ #endif
284+ while (true ) {
285+ int size = 0 ;
286+ char recvMessage[65 ];
287+ if (KWPReceiveBlock (recvMessage, 64 , size) != RETURN_SUCCESS ) return RETURN_RECEIVE_ERROR ;
288+ if (size == 0 ) return RETURN_NULLSIZE_ERROR ;
289+ if (recvMessage[2 ] == KWP_ACK ) break ; // if we got an ack block then it finished sending the ascii data
290+ if (recvMessage[2 ] != KWP_R_ASCII_DATA ) { // if it's not sending ASCII
291+ #ifdef DEBUG_SHOW_ERRORS
292+ Serial.println (F (" ERROR: unexpected answer" ));
293+ #endif
294+ disconnect ();
295+ errorData++;
296+ return RETURN_UNEXPECTEDANSWER_ERROR ;
297+ }
298+
299+ if (size == 9 ) {
300+ uint8_t b1 = recvMessage[4 ] &= 0xFF ,
301+ b2 = recvMessage[5 ] &= 0xFF ,
302+ b3 = recvMessage[6 ] &= 0xFF ,
303+ b4 = recvMessage[7 ] &= 0xFF ;
304+
305+ coding = ((b1 << 8 ) + ((b2 >> 4 ) << 4 )) >> 1 ;
306+ wsc = (((uint32_t )b2 & 0xF ) << 16 ) + (b3 << 8 ) + b4;
307+ break ;
308+ }
309+ if (KWPSendAckBlock () != RETURN_SUCCESS ) return RETURN_SEND_ERROR ;
310+ }
311+ return RETURN_SUCCESS ;
312+ }
313+
285314char KW1281_dv::KWPReadAscii () {
286315#ifdef DEBUG_SHOW_CURRENT_FUNCTION
287316 Serial.println (F (" KWPReadAscii" ));
@@ -318,7 +347,47 @@ char KW1281_dv::connect(uint8_t address, int baud, char id[], uint16_t &coding,
318347 // call the function which was defined by the user
319348 // usually it would contain a 1-second delay and reinitialising variables
320349 if_has_reset ();
350+
351+ connected = false ;
352+ blockCounter = 0 ; // reset the sequence counter
353+ currAddr = 0 ;
354+
355+ obd->begin (baud); // init SoftwareSerial
356+
357+ bitBang5Baud (address); // send the slow init sequence
358+
359+ // if the connection was successful, we should get the message 55 01 8A
360+ char s[3 ];
361+ int size = 3 ;
362+ if (KWPReceiveBlock (s, 3 , size) != RETURN_SUCCESS ) return RETURN_RECEIVE_ERROR ; // receive the block of 3 bytes
321363
364+ if ((((uint8_t )s[0 ]) != 0x55 ) || (((uint8_t )s[1 ]) != 0x01 ) || (((uint8_t )s[2 ]) != 0x8A )) {
365+ #ifdef DEBUG_SHOW_ERRORS
366+ Serial.println (F (" ERROR: Unexpected init response" ));
367+ #endif
368+ disconnect ();
369+ errorData++;
370+ return RETURN_UNEXPECTEDANSWER_ERROR ;
371+ }
372+ currAddr = address;
373+ connected = true ;
374+ // now read the text data the control module always sends when connected
375+ if (KWPReadAscii (id, coding, wsc) != RETURN_SUCCESS ) return RETURN_RECEIVE_ERROR ;
376+ return RETURN_SUCCESS ;
377+ }
378+
379+ char KW1281_dv::connect (uint8_t address, int baud, uint16_t &coding, uint32_t &wsc) {
380+ #ifdef DEBUG_SHOW_CURRENT_FUNCTION
381+ Serial.println (F (" connect" ));
382+ #endif
383+ #ifdef DEBUG
384+ Serial.print (F (" Connecting to " )); Serial.print (address, HEX ); Serial.print (F (" at baud " )); Serial.println (baud);
385+ #endif
386+
387+ // call the function which was defined by the user
388+ // usually it would contain a 1-second delay and reinitialising variables
389+ if_has_reset ();
390+
322391 connected = false ;
323392 blockCounter = 0 ; // reset the sequence counter
324393 currAddr = 0 ;
@@ -343,7 +412,7 @@ char KW1281_dv::connect(uint8_t address, int baud, char id[], uint16_t &coding,
343412 currAddr = address;
344413 connected = true ;
345414 // now read the text data the control module always sends when connected
346- if (KWPReadAscii (id, coding, wsc) != RETURN_SUCCESS ) return RETURN_RECEIVE_ERROR ;
415+ if (KWPReadAscii (coding, wsc) != RETURN_SUCCESS ) return RETURN_RECEIVE_ERROR ;
347416 return RETURN_SUCCESS ;
348417}
349418
@@ -355,6 +424,10 @@ char KW1281_dv::connect(uint8_t address, int baud) {
355424 Serial.print (F (" Connecting to " )); Serial.print (address, HEX ); Serial.print (F (" at baud " )); Serial.println (baud);
356425#endif
357426
427+ // call the function which was defined by the user
428+ // usually it would contain a 1-second delay and reinitialising variables
429+ if_has_reset ();
430+
358431 connected = false ;
359432 blockCounter = 0 ; // reset the sequence counter
360433 currAddr = 0 ;
0 commit comments