LIN-BUS, MCP2004, Serial write does not work 2

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;
}

What does that mean? ... exactly

Im not able to send any message.

So you're keying commands in the serial monitor?

I'm using an osciloscope to see the output. But as the code is not mine, i'm not sure the conections I made are okey or if the code has some failure. I know is not an easy thing to ask

Hi,
post a schematic of the project, (can be done freehand).
Understanding your project makes it easy to help.

I've never used this IC, but from the datasheet I understood that you also need to use its TX pin, but I didn't see the use of TX in your code.

The output of what? You only ever write to pin spin2, and this just gets toggled...

      if (s2) 
      {
        digitalWrite(spin2, HIGH);
      }
      else 
      {
        digitalWrite(spin2, LOW);
      }
      
      s2 = !s2;

Should you be writing out some serial data somewhere?

Are you sure this code actually works because it doesn't seem to be doing much?

I guess it doesn't work. I have found another solution using other program, i hope they cloae this theme.

Just mark the topic as resolved by ticking the "Solution" box on one of the posts.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.