Thanks for the welcome and reply!
The baudrate is 19200.
Regarding read() / listen() / readStream() they are called continuously in a while loop for e.g 200ms until there is a positive response or the time expires (pretty much a copy from the LED control master example)
The 0x55 0x9c can be consistent for a specific execution of the code. But e.g. if changing the delays in the scheduling of the write() and readStream() or changing debug level lin_bus.h, the data can be different e.g 0x55 5b e fe ff ff ff ff.
When readStream() is stalling it still returns 1. The stalling is detected by measuring the time the function call takes, see code below. When it stalls it is allways for 1000ms
readTimer = millis();
readOk = lin_master.readStream(readData,readDataSize);
if(DEBUG_READDATA)Serial.printf(“3. Read OK: %d, data read duration: %d ms\n”, readOk, (millis() - readTimer));
Basically what I want to achieve with the code is to simulate a LIN master on the LIN bus. Sending a header to a separate LIN ECU for it to respond. Then I want to read that response and look at some specific bits in the data and take different actions based on the value of the data (currently the “action” is represented by the LED). I presume the listen() function is the appropriate one to use in this case since I’m only interested in data from one specific ID. The reason for currently using readStream() is that is the only way I can read any data. Please find source code below.
#include <Wire.h>
#include “lin_bus.h”
#ifdef RAK4630
#define BOARD "RAK4631 "
int lin_tx = 16;
#elif ESP32
#define BOARD "RAK11200 "
int lin_tx = 21;
#else
#define BOARD "RAK11300 "
int lin_tx = 0;
#endif
#define DEBUG 0
#define DEBUG_READDATA 1
#define DEBUG_WRITEDATA 1
#define WB_LED1 12
#define WB_LED2 2
int lin_en = WB_IO6; //internal pulldown, EN=0 is sleep mode, EN=1 is normal operation mode.
int lin_wk = WB_IO5; //low active
// LIN Object
lin_bus lin_master(Serial1,LIN_V2, lin_en, lin_wk, lin_tx);
unsigned long baudrate = 19200;
uint8_t masterID = 0x1B;
uint8_t masterData[8] = {0x0e, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
uint16_t dataWriteTime;
uint8_t slaveID = 0x1C;
uint8_t slaveHeaderData[0];
const uint8_t schedulingDelay = 20;
const int readDataSize = 8;
uint8_t readData[readDataSize];
boolean dataRead = false;
uint8_t theInterestingBits;
int readOk;
unsigned long timer, readTimer;
void setup() {
pinMode(WB_LED1, OUTPUT);
pinMode(WB_LED2, OUTPUT);
pinMode(lin_wk,OUTPUT);
digitalWrite(lin_wk,HIGH);
if(DEBUG || DEBUG_READDATA || DEBUG_WRITEDATA){
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
}
boolean masterInitiated = false;
while(!masterInitiated)
{
masterInitiated = lin_master.master(baudrate);
if(DEBUG)Serial.println(“LIN master waiting to initiate”);
delay(100);
}
//Set board in 1=normal operation
lin_master.sleep(1);
if(DEBUG)Serial.println(“RAK13005 set in normal operation”);
delay(100);
lin_master.busWakeUp();
if(DEBUG)Serial.println(“LIN-bus wakup sent”);
delay(100);
}
void loop() {
timer = millis();
dataWriteTime = lin_master.write(masterID, masterData, 8); // Write data to LIN
if(DEBUG_WRITEDATA)Serial.printf(“1. Wrote data to LIN bus: %x, %x, %x, %x, %x, %x, %x, %x Duration: %dms\n”, masterData[0], masterData[1], masterData[2], masterData[3], masterData[4], masterData[5], masterData[6], masterData[7], dataWriteTime);
dataWriteTime = lin_master.write(slaveID, slaveHeaderData, sizeof(slaveHeaderData));
if(DEBUG_WRITEDATA)Serial.printf(“2. Wrote header data to ID: %x, Duration: %dms\n”, slaveID, dataWriteTime);
if(DEBUG_WRITEDATA)Serial.printf(“Total write time: %dms\n”, millis() - timer);
//while( (millis() - timer) < schedulingDelay ) delay(1);
delay(schedulingDelay);
timer = millis();
while( (millis() - timer) < schedulingDelay ) //Continously reads LIN bus until data is recieved or time expires
{
if(!dataRead)
{
readTimer = millis();
readOk = lin_master.readStream(readData,readDataSize);
if(DEBUG_READDATA)Serial.printf(“3. Read OK: %d, data read duration: %d ms\n”, readOk, (millis() - readTimer));
if(readOk)
{
dataRead = true;
digitalWrite(WB_LED1, HIGH);
if(DEBUG_READDATA)printReadData();
if(readData[5] == 0)
{
theInterestingBits = readData[4] & B11100000;
if(DEBUG_READDATA)Serial.printf("theInterestingBits: %x\n", theInterestingBits);
if( theInterestingBits > 0 ){
digitalWrite(WB_LED2, HIGH);
}
else
{
digitalWrite(WB_LED2, LOW);
}
}
}
else
{
if(DEBUG_READDATA)Serial.println("4. Read fail");
}
}
}
digitalWrite(WB_LED1, LOW);
dataRead = false;
readOk = 0;
}
void printReadData()
{
Serial.print(“4. Data received: “);
for( uint8_t i=0; i<readDataSize ;i++ )
{
Serial.printf(”%x “, readData[i]);
}
Serial.printf(”\n”);
}