Hello, I have been trying to send messages in LIN-BUS, and I used a code made by someone from this forum years ago (system), but it doesn´t work.
This is the code:
#include <SoftwareSerial.h>
//SoftwareSerial IBusSerial(10, 11); // RX, TX for IBus
// Pins we use for MCP2004
const int faultPin = 6;
const int cspin = 7;
const int ledpin = 13;
const int spin1 = 2;
const int spin2 = 3;
const int spin3 = 4;
const int spin4 = 5;
boolean read_byte = false;
boolean led = false;
boolean s1 = false;
boolean s2 = false;
boolean s3 = false;
boolean s4 = false;
byte readbuffer[64];
int i;
int buffer_index = 0;
int buffer_max = 64;
int cksum;
long lastrx;
long lasttx;
void setup() {
// initialize buffer array to 0's
memset(readbuffer, 0, sizeof(readbuffer));
// Open serial communications to host (PC) and wait for port to open:
Serial.begin(9600, SERIAL_8E1);
// Serial.println("IBus Debugging begins");
// set the data rate for the SoftwareSerial port
//IBusSerial.begin(9600);
// Set high to enable TX on MCP2004
pinMode(cspin, OUTPUT);
digitalWrite(cspin, HIGH);
// Set high to enable something ont he MCP2004
pinMode(faultPin, OUTPUT);
digitalWrite(faultPin, HIGH);
lastrx = millis();
lasttx = millis();
}
void loop() {
delayMicroseconds(1000);
lasttx = millis();
if ((millis() - lastrx) > 15) {
memset(readbuffer, 0, sizeof(readbuffer));
buffer_index = 0;
read_byte = false;
buffer_max = 64;
lastrx = millis();
return;
}
if (Serial.available()) {
readbuffer[buffer_index] = Serial.read();
read_byte = true;
}
if (read_byte) {
if (buffer_index == 1) {
buffer_max = readbuffer[buffer_index] + 2;
cksum = readbuffer[0] ^ readbuffer[1];
} else if ((buffer_index > 1 ) && (buffer_index < buffer_max)) {
cksum = cksum ^ readbuffer[buffer_index];
}
}
//0x50 <-- Multi function wheel
//0x46 <-- Center display
//0xC0 <-- Multi info display
//0xE7 <-- Front display <-- 20:59
//0x3B <-- Navigation display, visas i navi, radiokanal namn
// Reset buffer_index when it is buffer_max - 1.
if (buffer_index == (buffer_max - 1)) {
if (cksum == 0) {
if (s2) {
digitalWrite(spin2, HIGH);
}
else {
digitalWrite(spin2, LOW);
}
s2 = !s2;
Serial.print("Good message: ");
//Serial.print(millis());
Serial.print(" S:");
Serial.print(readbuffer[0], HEX);
Serial.print( " L:");
Serial.print(readbuffer[1], DEC);
Serial.print( " ");
for (i = 2; i < buffer_max; i++) {
Serial.print(readbuffer[i], HEX);
Serial.print(" ");
}
} else {
Serial.print("Invalid message. cksum: ");
Serial.println(cksum, HEX);
for (i = 0; i < buffer_max; i++) {
Serial.print(readbuffer[i], HEX);
Serial.print(" ");
}
}
// handleMessage(0x2D);
Serial.println();
// Empty the buffer
memset(readbuffer, 0, sizeof(readbuffer));
buffer_index = 0;
read_byte = false;
lastrx = millis();
}
// Increment index if we put something into the buffer
if (read_byte == true) {
read_byte = false;
buffer_index++;
lastrx = millis();
}
}
void handleMessage(byte messageType) {
byte volup[6] = { 0x50, 0x04, 0x68, 0x32, 0x11, 0x1F};
byte voldown2[6] = { 0x50, 0x04, 0x68, 0x32, 0x10, 0x1E};
byte voldown[6] = { 0xF0, 0x04, 0x68, 0x32, 0x10, 0xBE };
byte pressmode[6] = { 0xF0, 0x4, 0x68, 0x48, 0x23, 0xF7 };
byte releasemode[6] = { 0xF0, 0x4, 0x68, 0x48, 0xA3, 0x77 };
byte ibussucks[22] = {
0x68, 0x12, 0x3B, 0x23, 0x62, 0x10, 0x49, 0x42, 0x55,
0x53, 0x53, 0x55, 0x43, 0x4b, 0x53, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x40
};
byte infoMessage[27] = {0x30, 0x19, 0x80, 0x1A, 0x35 , 0x0 , 0x20 , 0x20 , 0x43 , 0x48 , 0x45 , 0x43 , 0x4B , 0x20 , 0x43 , 0x4F , 0x4E , 0x54 , 0x52 , 0x4F , 0x4C , 0x20 , 0x4F , 0x4B , 0x20 , 0x20 , 0x83};
// OP Was setting faultPin HIGH to write?
// digitalWrite(faultPin, HIGH);
switch (messageType) {
case 0x2B: // + for vol up
Serial.println("Sending vol up.");
sendMessage(volup, 6);
break;
case 0x2D: // - for vol down
Serial.println("Sending vol down.");
sendMessage(voldown2, 6);
break;
case 0x42: // B for IBus Sucks
Serial.println("Sending Info message.");
sendMessage(infoMessage, 27);
break;
case 0x6D: // m for mode
Serial.println("Sending Mode.");
sendMessage(pressmode, 6);
delay(150);
sendMessage(releasemode, 6);
break;
}
}
void sendMessage(byte message_data[], int length) {
//byte message_cksum = gen_cksum(message_data);
for (int k = 0; k < length ; k++) {
Serial.write(message_data[k]);
}
}
//Not used
byte gen_cksum(const byte message[]) {
byte cksum = 0x00;
for (int i = 1; i <= message[0]; i++) {
cksum = cksum ^ message[i];
}
return cksum;
}