// NCM C7 PAS6 Speed Unlock
// Packet Data https://github.com/NorthyIE/DasKit-C7BB-Tuning/blob/master/Documentation/Display-2-Controller.md
#define BAUDRATE 9600
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3); // RX, TX
const byte numBytes = 32;
byte receivedBytes1[numBytes];
byte numReceived1 = 0;
boolean newData1 = false;
byte sendBytes[numBytes];
void setup() {
Serial1.begin(BAUDRATE);
}
void loop() {
CheckMessage(); // Check for Message from C7 Display
ModifyMessage(); // Modify Message
GenerateChecksum(); // Calculate Checksum
SendModifiedMessage(); // Send modified message
}
void CheckMessage() {
static boolean recvInProgress = false;
static byte ndx = 0;
byte startMarker = 0x3A; // Start byte 0x3A
byte endMarker1 = 0x0D;
byte endMarker2 = 0x0A; // End with CR LF
byte rb;
while (Serial1.available() > 0 && newData1 == false) {
rb = Serial1.read();
if (recvInProgress == true) {
if ((rb != endMarker2) || (receivedBytes1[ndx-1] !=endMarker1)) {
receivedBytes1[ndx] = rb;
ndx++;
if (ndx >= numBytes) {
ndx = numBytes - 1;
}
}
else {
receivedBytes1[ndx-1] = '\0'; // terminate the string
recvInProgress = false;
numReceived1 = ndx-1; // save the number for use when printing
ndx = 0;
newData1 = true;
}
}
else if (rb == startMarker) {
recvInProgress = true;
}
}
}
void ModifyMessage() {
sendBytes[0]=0x3A; // New message starts with 0x3A
for (byte n=0; n < numReceived1; n++) {
sendBytes[n+1]=receivedBytes1[n]; // Copy message bytes
}
sendBytes[10]=0x0D; // End with CR LF
sendBytes[11]=0x0A;
if (sendBytes[3] == (0x46) || sendBytes[3] == (0xC6) ) { // If PAS == 6th stage with backlight off or no
sendBytes[2]=(0x0B); // Make sure we're in Power Mode (0x0B => Power Mode, 0x0A => Normal, 0x09 => Eco)
sendBytes[4]=(0x36); // Set Max Speed (ex : 0x24 => 36km/h, 0x36 => 54km/h)
sendBytes[7]=(0x36); // Set Throttle Max Speed
}
}
void GenerateChecksum() {
sendBytes[8]=0;
for (byte n=1; n<8; n++){
sendBytes[8]=sendBytes[8]+sendBytes[n];
}
}
void SendModifiedMessage() {
if (newData1 == true) {
Serial1.write(sendBytes,12); // Send Message
newData1 = false;
}
}