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)