Arduino + ccnet

Hello. I am implementing the ccnet protocol of the bill acceptor on Arduino, the data is sent and received normally, but there is one hitch - the bill acceptor, apparently, sends the response in parts and I receive the response to the first request after I have sent the second. I tried to make the wait, but for some reason nothing comes. Code:

const uint8_t CMD_IDENT[] = {0x02, 0x03, 0x06, 0x37};
const uint8_t CMD_BILL_TABLE[] = {0x02, 0x03, 0x06, 0x41};

 void getCashData()
{

  uint8_t pack_size_hex = 0;
  long pack_size = 0;

  if (ccnetSerial->available())
  {
    uint8_t code = ccnetSerial->read();
    uint8_t device_type = ccnetSerial->read();
    pack_size_hex = ccnetSerial->read();

    pack_size = convert.hexaToDecimal(String(pack_size_hex, HEX));

    Serial.print("size of pack: ");
    Serial.print(pack_size_hex, HEX);
    Serial.print("---");
    Serial.println(pack_size);

  }

  int psz = 0;

  while (psz <= pack_size)
  {


    psz += 1;
    int getByte = ccnetSerial->read();


    Serial.print(getByte, HEX);
    Serial.print("--");
    Serial.print(psz);
    Serial.print("--");
    Serial.print(pack_size);
    Serial.println("");

    if (getByte == -1)
    {

      delay (5000);
      
      getByte = ccnetSerial->read();
    }

    if (psz == pack_size)
      break;

  }
  Serial.println("*************");
}

void loop() {

      Serial.println("CMD_IDENT: ");
      sendCommand(CMD_IDENT, sizeof(CMD_IDENT));
      delay (1000);
      getCashData();

      delay (5000);

      Serial.println("CMD_BILL_TABLE: ");
      sendCommand(CMD_BILL_TABLE, sizeof(CMD_BILL_TABLE));
      delay (1000);
      getCashData();

}

In response, I receive the following from the serial port:

CMD_IDENT: 
Send data: 02,03,06,37,FE,C7
size of pack: 27---39
4D--1--39
53--2--39
4D--3--39
2D--4--39
55--5--39
41--6--39
31--7--39
38--8--39
33--9--39
38--10--39
20--11--39
20--12--39
20--13--39
20--14--39
20--15--39
30--16--39
33--17--39
4B--18--39
42--19--39
34--20--39
30--21--39
30--22--39
33--23--39
38--24--39
39--25--39
32--26--39
31--27--39
27--28--39
68--29--39
FFFFFFFF--30--39
FFFFFFFF--31--39
FFFFFFFF--32--39
FFFFFFFF--33--39
FFFFFFFF--34--39
FFFFFFFF--35--39
FFFFFFFF--36--39
FFFFFFFF--37--39
FFFFFFFF--38--39
FFFFFFFF--39--39
*************


CMD_BILL_TABLE: 
Send data: 02,03,06,41,4F,D1
size of pack: 7D---125
5--1--125
55--2--125
4B--3--125
52--4--125
0--5--125
1--6--125
55--7--125
4B--8--125
52--9--125
1--10--125
2--11--125
55--12--125
4B--13--125
52--14--125
1--15--125
5--16--125
55--17--125
4B--18--125
52--19--125
1--20--125
1--21--125
55--22--125
4B--23--125
52--24--125
2--25--125
2--26--125
55--27--125
4B--28--125
52--29--125
FFFFFFFF--30--125
FFFFFFFF--31--125
FFFFFFFF--32--125
FFFFFFFF--33--125
FFFFFFFF--34--125
FFFFFFFF--35--125
……..

Please tell me how to correctly get data in this context?

Your sketch is incomplete and does not compile.

That basically reads
if one byte is available, read three bytes

I doubt that a little. Serial transmission is slow and after you receive the first byte the transmission of the second byte might still be in progress but you already try to read it.

Changing if (ccnetSerial->available()) to if (ccnetSerial->available()>=3) might do the trick. If not, please post the spec of the ccnet protocol and your full code.

Full code:

const uint8_t BILL_TX = 8;
const uint8_t BILL_RX = 21;

#include <SoftwareSerial.h>

#include "Convert.h"

Convert convert;
SoftwareSerial *ccnetSerial;
#define POLYNOMIAL 0x08408


const uint8_t CMD_RESET[] = {0x02, 0x03, 0x06, 0x30};
const uint8_t CMD_ASK[] = {0x02, 0x03, 0x06, 0x00};
const uint8_t CMD_POLL[] = {0x02, 0x03, 0x06, 0x33};
const uint8_t CMD_IDENT[] = {0x02, 0x03, 0x06, 0x37};
const uint8_t CMD_ENABLE_BILL_ACCEPTANCE[] = {0x02, 0x03, 0x0C, 0x34, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00};

const uint8_t CMD_BILL_TABLE[] = {0x02, 0x03, 0x06, 0x41};

uint16_t calculateCRC16(const uint8_t *data, size_t length) {
  uint16_t crc = 0x0000; 

  for (size_t i = 0; i < length; i++) {
    crc ^= data[i]; 

    for (uint8_t j = 0; j < 8; j++) {
      if (crc & 0x0001) {
        crc = (crc >> 1) ^ POLYNOMIAL; 
      } else {
        crc >>= 1;
      }
    }
  }
  return crc; 
}

void sendCommand(const uint8_t *command, size_t length) {

  uint16_t crc = calculateCRC16(command, length); 

  uint8_t makeSend[length + 2];

  Serial.print ("Send data: ");

  for (int x = 0; x < length; x++)
  {
    makeSend [x] = command [x];

    if (command [x] < 0x10) Serial.print("0");
    Serial.print (command [x], HEX);
    Serial.print(",");
  }


  makeSend[length] = crc & 0xFF;          
  makeSend[length + 1] = (crc >> 8) & 0xFF; 

  Serial.print(makeSend[length], HEX);
  Serial.print(",");
  Serial.print(makeSend[length + 1], HEX);

  ccnetSerial->write(makeSend, (length + 2));


  Serial.println ("");
}

void getCashData()
{

  uint8_t pack_size_hex = 0;
  long pack_size = 0;

  if (ccnetSerial->available() >= 3)
  {
    uint8_t code = ccnetSerial->read();
    uint8_t device_type = ccnetSerial->read();
    pack_size_hex = ccnetSerial->read();

    pack_size = convert.hexaToDecimal(String(pack_size_hex, HEX));

    Serial.print("size of pack: ");
    Serial.print(pack_size_hex, HEX);
    Serial.print("---");
    Serial.println(pack_size);

    int psz = 0;

    while (psz <= pack_size)
    {


      psz += 1;
      int getByte = ccnetSerial->read();


      Serial.print(getByte, HEX);
      Serial.print("--");
      Serial.print(psz);
      Serial.print("--");
      Serial.print(pack_size);
      Serial.println("");

      if (psz == pack_size)
        break;



    }

  }


  Serial.println("*************");


}

void setup() {
   Serial.begin(115200);
  Serial.setTimeout(10);

  ccnetSerial = new SoftwareSerial(BILL_RX, BILL_TX); // RX - 21, TX - 8
  ccnetSerial->begin(9600);
  ccnetSerial->setTimeout(10);

}

void loop() {
  
if (Serial.available ())
  {
    String getCmd = Serial.readString();


    Serial.print("get cmd: ");
    Serial.println(getCmd);

    Serial.println("try send reset");
      sendCommand(CMD_RESET, sizeof(CMD_RESET));
      delay (1000);
      getCashData();

      delay (5000);

      Serial.println("CMD_IDENT: ");
      sendCommand(CMD_IDENT, sizeof(CMD_IDENT));
      delay (1000);
      getCashData();

      delay (5000);

      Serial.println("CMD_BILL_TABLE: ");
      sendCommand(CMD_BILL_TABLE, sizeof(CMD_BILL_TABLE));
      delay (1000);
      getCashData();

  }

}

get output:

08:26:06.355 -> get cmd: test
08:26:06.355 -> try send reset
08:26:06.355 -> Send data: 02,03,06,30,41,B3
08:26:07.392 -> size of pack: 6---6
08:26:07.392 -> 0--1--6
08:26:07.392 -> C2--2--6
08:26:07.392 -> 82--3--6
08:26:07.392 -> FFFFFFFF--4--6
08:26:07.392 -> FFFFFFFF--5--6
08:26:07.392 -> FFFFFFFF--6--6
08:26:07.392 -> *************
08:26:12.395 -> CMD_IDENT: 
08:26:12.395 -> Send data: 02,03,06,37,FE,C7
08:26:13.390 -> size of pack: 27---39 **<--- Get correct size by ccnet documentation**
08:26:13.390 -> 4D--1--39
08:26:13.390 -> 53--2--39
08:26:13.390 -> 4D--3--39
08:26:13.390 -> 2D--4--39
08:26:13.390 -> 55--5--39
08:26:13.390 -> 41--6--39
08:26:13.390 -> 31--7--39
08:26:13.390 -> 38--8--39
08:26:13.390 -> 33--9--39
08:26:13.390 -> 38--10--39
08:26:13.390 -> 20--11--39
08:26:13.390 -> 20--12--39
08:26:13.390 -> 20--13--39
08:26:13.390 -> 20--14--39
08:26:13.390 -> 20--15--39
08:26:13.390 -> 30--16--39
08:26:13.390 -> 33--17--39
08:26:13.390 -> 4B--18--39
08:26:13.390 -> 42--19--39
08:26:13.390 -> 34--20--39
08:26:13.390 -> 30--21--39
08:26:13.390 -> 30--22--39
08:26:13.390 -> 33--23--39
08:26:13.390 -> 38--24--39
08:26:13.390 -> 39--25--39
08:26:13.390 -> 32--26--39
08:26:13.390 -> 31--27--39
08:26:13.390 -> 27--28--39
08:26:13.390 -> 68--29--39
08:26:13.390 -> FFFFFFFF--30--39
08:26:13.390 -> FFFFFFFF--31--39
08:26:13.390 -> FFFFFFFF--32--39
08:26:13.390 -> FFFFFFFF--33--39
08:26:13.390 -> FFFFFFFF--34--39
08:26:13.390 -> FFFFFFFF--35--39
08:26:13.390 -> FFFFFFFF--36--39
08:26:13.390 -> FFFFFFFF--37--39
08:26:13.390 -> FFFFFFFF--38--39
08:26:13.390 -> FFFFFFFF--39--39
08:26:13.390 -> *************
08:26:18.383 -> CMD_BILL_TABLE: 
08:26:18.383 -> Send data: 02,03,06,41,4F,D1
08:26:19.367 -> size of pack: 7D---125 **<--- Get wrong size by ccnet documentation**
08:26:19.367 -> 5--1--125
08:26:19.367 -> 55--2--125
08:26:19.367 -> 4B--3--125
08:26:19.367 -> 52--4--125
08:26:19.367 -> 0--5--125
08:26:19.367 -> 1--6--125
08:26:19.367 -> 55--7--125
08:26:19.367 -> 4B--8--125
08:26:19.367 -> 52--9--125
08:26:19.367 -> 1--10--125
08:26:19.367 -> 2--11--125
08:26:19.367 -> 55--12--125
08:26:19.367 -> 4B--13--125
08:26:19.367 -> 52--14--125
08:26:19.367 -> 1--15--125
08:26:19.367 -> 5--16--125
08:26:19.367 -> 55--17--125
08:26:19.367 -> 4B--18--125
08:26:19.367 -> 52--19--125
08:26:19.367 -> 1--20--125
08:26:19.367 -> 1--21--125
08:26:19.367 -> 55--22--125
08:26:19.367 -> 4B--23--125
08:26:19.367 -> 52--24--125
08:26:19.367 -> 2--25--125
08:26:19.367 -> 2--26--125
08:26:19.367 -> 55--27--125
08:26:19.367 -> 4B--28--125
08:26:19.367 -> 52--29--125
08:26:19.367 -> FFFFFFFF--30--125
08:26:19.367 -> FFFFFFFF--31--125
08:26:19.367 -> FFFFFFFF--32--125
08:26:19.367 -> FFFFFFFF--33--125
08:26:19.367 -> FFFFFFFF--34--125
08:26:19.367 -> FFFFFFFF--35--125
08:26:19.367 -> FFFFFFFF--36--125
08:26:19.367 -> FFFFFFFF--37--125
08:26:19.367 -> FFFFFFFF--38--125
08:26:19.367 -> FFFFFFFF--39--125
08:26:19.367 -> FFFFFFFF--40--125
08:26:19.367 -> FFFFFFFF--41--125
08:26:19.367 -> FFFFFFFF--42--125
08:26:19.367 -> FFFFFFFF--43--125
08:26:19.367 -> FFFFFFFF--44--125
08:26:19.367 -> FFFFFFFF--45--125
08:26:19.367 -> FFFFFFFF--46--125
08:26:19.367 -> FFFFFFFF--47--125
08:26:19.367 -> FFFFFFFF--48--125
08:26:19.367 -> FFFFFFFF--49--125
08:26:19.367 -> FFFFFFFF--50--125
08:26:19.367 -> FFFFFFFF--51--125
08:26:19.367 -> FFFFFFFF--52--125
08:26:19.367 -> FFFFFFFF--53--125
08:26:19.367 -> FFFFFFFF--54--125
08:26:19.367 -> FFFFFFFF--55--125
08:26:19.367 -> FFFFFFFF--56--125
08:26:19.367 -> FFFFFFFF--57--125
08:26:19.367 -> FFFFFFFF--58--125
08:26:19.367 -> FFFFFFFF--59--125
08:26:19.367 -> FFFFFFFF--60--125
08:26:19.367 -> FFFFFFFF--61--125
08:26:19.367 -> FFFFFFFF--62--125
08:26:19.367 -> FFFFFFFF--63--125
08:26:19.367 -> FFFFFFFF--64--125
08:26:19.367 -> FFFFFFFF--65--125
08:26:19.367 -> FFFFFFFF--66--125
08:26:19.367 -> FFFFFFFF--67--125
08:26:19.367 -> FFFFFFFF--68--125
08:26:19.367 -> FFFFFFFF--69--125
08:26:19.367 -> FFFFFFFF--70--125
08:26:19.367 -> FFFFFFFF--71--125
08:26:19.367 -> FFFFFFFF--72--125
08:26:19.367 -> FFFFFFFF--73--125
08:26:19.367 -> FFFFFFFF--74--125
08:26:19.367 -> FFFFFFFF--75--125
08:26:19.367 -> FFFFFFFF--76--125
08:26:19.367 -> FFFFFFFF--77--125
08:26:19.367 -> FFFFFFFF--78--125
08:26:19.367 -> FFFFFFFF--79--125
08:26:19.367 -> FFFFFFFF--80--125
08:26:19.367 -> FFFFFFFF--81--125
08:26:19.367 -> FFFFFFFF--82--125
08:26:19.367 -> FFFFFFFF--83--125
08:26:19.367 -> FFFFFFFF--84--125
08:26:19.367 -> FFFFFFFF--85--125
08:26:19.367 -> FFFFFFFF--86--125
08:26:19.367 -> FFFFFFFF--87--125
08:26:19.367 -> FFFFFFFF--88--125
08:26:19.367 -> FFFFFFFF--89--125
08:26:19.367 -> FFFFFFFF--90--125
08:26:19.367 -> FFFFFFFF--91--125
08:26:19.367 -> FFFFFFFF--92--125
08:26:19.367 -> FFFFFFFF--93--125
08:26:19.367 -> FFFFFFFF--94--125
08:26:19.367 -> FFFFFFFF--95--125
08:26:19.367 -> FFFFFFFF--96--125
08:26:19.367 -> FFFFFFFF--97--125
08:26:19.367 -> FFFFFFFF--98--125
08:26:19.367 -> FFFFFFFF--99--125
08:26:19.367 -> FFFFFFFF--100--125
08:26:19.367 -> FFFFFFFF--101--125
08:26:19.367 -> FFFFFFFF--102--125
08:26:19.367 -> FFFFFFFF--103--125
08:26:19.367 -> FFFFFFFF--104--125
08:26:19.367 -> FFFFFFFF--105--125
08:26:19.367 -> FFFFFFFF--106--125
08:26:19.367 -> FFFFFFFF--107--125
08:26:19.367 -> FFFFFFFF--108--125
08:26:19.367 -> FFFFFFFF--109--125
08:26:19.367 -> FFFFFFFF--110--125
08:26:19.367 -> FFFFFFFF--111--125
08:26:19.367 -> FFFFFFFF--112--125
08:26:19.367 -> FFFFFFFF--113--125
08:26:19.367 -> FFFFFFFF--114--125
08:26:19.367 -> FFFFFFFF--115--125
08:26:19.367 -> FFFFFFFF--116--125
08:26:19.367 -> FFFFFFFF--117--125
08:26:19.367 -> FFFFFFFF--118--125
08:26:19.367 -> FFFFFFFF--119--125
08:26:19.367 -> FFFFFFFF--120--125
08:26:19.367 -> FFFFFFFF--121--125
08:26:19.367 -> FFFFFFFF--122--125
08:26:19.367 -> FFFFFFFF--123--125
08:26:19.367 -> FFFFFFFF--124--125
08:26:19.367 -> FFFFFFFF--125--125
08:26:19.367 -> *************

ccnet protoco in attach
CCNET.pdf (1.3 MB)

0xFFFFFFFF is -1 and would indicate that there is no byte available to read.

yes, on reset command I understand why, but for other - no. For IDEN I got 0x27 (39 bytes) but data was only in 29

Why did you decided that the data is in the HEX format?

Do you understand that your pack_size and pack_size_hex are exactly the same? And therefore, your hexaToDecimal() conversion is pointless?

Where is

@juffin
Your actual problem looks similar to your thread a year ago:
https://forum.arduino.cc/t/bluetoothserial-get-wrong-data/1195931/21
You again are trying to read from a port without making sure there is data on the port.

It seems you lack an understanding of how Serial communications works.
Start from reading Serial Input Basics - updated

you can check it in ccnet protocol.

Do you understand that your pack_size and pack_size_hex are exactly the same

How hex can be the same to dec?...

Convert.h

Your output is proof of that:

Remove the conversion and make sure that the result will be the same:

so the conversion is obsolete

It's a joke?...

0x06 = 6 in dec

08:26:12.395 -> CMD_IDENT: 
08:26:12.395 -> Send data: 02,03,06,37,FE,C7
08:26:13.390 -> size of pack: 27---39

0x27 !=27 in dec! 0x27 = 39 in dec

Is it really new to you that the computer stores this data not as a dec or hex, but as a binary number?
They are presented as hex or dec just for humans only in the print operator, so there is no need to convert anything inside controller.

If you don't believe me, run the code below:

// two values below are the same
byte pack_size_hex = 0x27;
byte pack_size = 39;
// test that
if (pack_size_hex == pack_size) {
 Serial.println("HEX and DEC are the same!");
}
else {
Serial.println("HEX and DEC are different");
}