Arduino LIN reader RGB colors

Hi, I need to create a code that will read data from the module about the current color setting. Can someone help me? I need to decode data from LINU, I currently have this code and I made a few test logs. My code:

#include <SoftwareSerial.h> // Jeśli korzystasz z dodatkowych pinów UART

// Ustawienia UART
const int RX_PIN = 2;  // Pin RX, zmień jeśli potrzeba
const int TX_PIN = 3;  // Pin TX, zmień jeśli potrzeba
const long BAUD_RATE = 19200; // Prędkość UART (LIN najczęściej 19200 bps)

SoftwareSerial linSerial(RX_PIN, TX_PIN); // Użyj SoftwareSerial, jeśli masz jedno UART

// Parametry pakietu LIN
const int MAX_PACKET_SIZE = 512; // Bufor na maksymalny rozmiar danych LIN
byte linBuffer[MAX_PACKET_SIZE];
int bufferIndex = 0;

// Czas do wysyłania pełnych odczytów
unsigned long lastSendTime = 0;
const unsigned long SEND_INTERVAL = 3000; // Co 3 sekundy

void setup() {
  Serial.begin(9600); // UART dla monitorowania danych
  linSerial.begin(BAUD_RATE); // UART dla komunikacji z konwerterem LIN
  
  Serial.println("Arduino LIN to UART Receiver");
}

void loop() {
  // Odbieranie danych z LIN
  if (linSerial.available()) {
    byte receivedByte = linSerial.read();
    
    // Dodaj dane do bufora
    if (bufferIndex < MAX_PACKET_SIZE) {
      linBuffer[bufferIndex++] = receivedByte;
    } else {
      Serial.println("Bufor przepełniony!");
      bufferIndex = 0; // Zresetuj bufor w razie problemów
    }
  }

  // Sprawdź, czy nadszedł czas na wysłanie danych
  if (millis() - lastSendTime >= SEND_INTERVAL) {
    sendLinData();
    lastSendTime = millis();
  }
}

void sendLinData() {
  if (bufferIndex > 0) {
    Serial.println("Odebrano pakiety LIN (pełny bufor):");
    Serial.print("Liczba bajtów w pakiecie: ");
    Serial.println(bufferIndex); // Wyświetl liczbę bajtów w pakiecie

    // Wyświetl dane w formacie heksadecymalnym
    for (int i = 0; i < bufferIndex; i++) {
      Serial.print("0x");
      if (linBuffer[i] < 0x10) Serial.print("0"); // Dodaj zero dla jednocyfrowych wartości
      Serial.print(linBuffer[i], HEX);
      Serial.print(" ");
    }
    Serial.println(); // Nowa linia po pakiecie
  } else {
    Serial.println("Brak nowych danych w buforze.");
  }
  
  bufferIndex = 0; // Zresetuj bufor po wysłaniu
}

RED logi: Odebrano pakiety LIN (pełny bufor):Liczba bajtów w pakiecie: 3870x03 0x01 0x - Pastebin.com
BLUE Logi: Bufor przepełniony!Bufor przepełniony!Bufor przepełniony!Bufor przepełnion - Pastebin.com
GREEN logi: Odebrano pakiety LIN (pełny bufor):Liczba bajtów w pakiecie: 3850x00 0x55 0x - Pastebin.com

Edit your post, use the <CODE> button to create a code block, then paste your formatted code here.

Done.

1 Like

Any ideas?

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