Using software serial within a header file

Hi,

I am trying to create a library for LIN. I would like to include/use software serial within the header file but it is coming up with error. Can anyone please help.

INO code:

#include <SoftwareSerial.h>
#include <LIN.h>

LIN LINserial(9600,2,3);

void setup() {
  // put your setup code here, to run once:
  LINserial.begin(9600);
  LINserial.gotoPos(10);
}

void loop() {


}

Header(.h) code:

#ifndef	LIN_H
#define	LIN_H

#include <Arduino.h>
#include <SoftwareSerial.h>

#define BIT_CHK(x,y) (((x)&(1<<(y)))>0? 1:0) //returns value selected bit '0' or '1'

class SoftwareSerial;
class LINClass{
public:
	
	uint8_t LIN(uint16_t Baud,uint8_t RxPin,uint8_t TxPin)
	{
		Bauddefault=Baud;
		TsyncbrkMIN=(13/Baud)*1000000; //store in microseconds instead of seconds
  
		if((RxPin<14) && (TxPin<14) && (RxPin!=TxPin)){
			serial(RxPin,TxPin);
			return 1;
		}
  
		return 0;		
	}
	
	static void gotoPos(uint8_t pos)
    {
        serial.write(pos);
    }
	
	static uint8_t Init(uint16_t Baud,uint8_t RxPin,uint8_t TxPin);
	
private:
  static uint16_t Bauddefault;
  static uint16_t TsyncbrkMIN;
  SoftwareSerial serial;
};

extern LINClass LIN;

#endif

.cpp file

#include <LIN.h>

LINClass LIN;

uint16_t LINClass::Bauddefault=0;
uint16_t LINClass::TsyncbrkMIN=0; 

uint8_t LINClass::Init(uint16_t Baud,uint8_t RxPin,uint8_t TxPin)
{

  Bauddefault=Baud;
  TsyncbrkMIN=(13/Baud)*1000000; //store in microseconds instead of seconds
  
  if((RxPin<14) && (TxPin<14) && (RxPin!=TxPin)){
	serial(RxPin,TxPin);
	return 1;
  }
  
  return 0;
}

error log:

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h: In member function 'uint8_t LINClass::LIN(uint16_t, uint8_t, uint8_t)':

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h:25:22: error: no match for call to '(SoftwareSerial) (uint8_t&, uint8_t&)'

    serial(RxPin,TxPin);

                      ^

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h: In static member function 'static void LINClass::gotoPos(uint8_t)':

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h:42:18: error: invalid use of member 'LINClass::serial' in static member function

   SoftwareSerial serial;

                  ^

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h:34:9: error: from this location

         serial.write(pos);

         ^

\\uk-ntce-s0100.nissaneu.nmcorp.nissan.biz\HomeFolders\DINAHS\My Documents\Arduino\LIN_codes_1\LIN_codes_1.ino: At global scope:

LIN_codes_1:4: error: 'LIN' does not name a type

 LIN LINserial(9600,2,3);

 ^

\\uk-ntce-s0100.nissaneu.nmcorp.nissan.biz\HomeFolders\DINAHS\My Documents\Arduino\LIN_codes_1\LIN_codes_1.ino: In function 'void setup()':

LIN_codes_1:8: error: 'LINserial' was not declared in this scope

   LINserial.begin(9600);

   ^

exit status 1
'LIN' does not name a type

Many Thanks,
Sherzaad

You need to construct the serial instance at the same time as you create the LINSerial instance, which means that you MUST define a constructor for your class.

class LINClass
{ // Down here where the damned thing belongs
public:
   LINClass(uint8_t RxPin,uint8_t TxPin);

Thereisnoexcuseforjammingthe{againsttheclassname.

LINClass::LINClass(uint8_t RxPin,uint8_t TxPin):serial(RxPin, TxPin)
{
}

Thank you for your input. I have re-written the header file as follows:

#ifndef	LIN_H
#define	LIN_H

#include <Arduino.h>
#include <SoftwareSerial.h>

class SoftwareSerial;
class LIN{
public:
   LIN(uint8_t RxPin,uint8_t TxPin):serial(RxPin, TxPin)
   {
   }
   
   void gotoPos(uint8_t pos)
   {
       serial.write(pos);
   }
	
private:
  SoftwareSerial serial;
};

#endif

but now I am getting the following errors:

C:\Users\dinahs\AppData\Local\Temp\builde7ab6983a54af0949d49162396d9bf2e.tmp\sketch\LIN_codes_1.ino.cpp.o: In function `LIN':

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h:18: undefined reference to `SoftwareSerial::SoftwareSerial(unsigned char, unsigned char, bool)'

C:\Users\dinahs\AppData\Local\Temp\builde7ab6983a54af0949d49162396d9bf2e.tmp\sketch\LIN_codes_1.ino.cpp.o: In function `~LIN':

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h:16: undefined reference to `SoftwareSerial::~SoftwareSerial()'

C:\Users\dinahs\AppData\Local\Temp\builde7ab6983a54af0949d49162396d9bf2e.tmp\sketch\LIN_codes_1.ino.cpp.o: In function `LIN::gotoPos(unsigned char)':

D:\LocalData\dinahs\Downloads\arduino-nightly\libraries\LIN/LIN.h:24: undefined reference to `SoftwareSerial::write(unsigned char)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling.

any ideas as to why?

I have re-written the header file as follows:

Why? That is NOT how I said to do things.

class LIN{

Igiveup.

White space IS important!

what u suggested I have tried it is gave the same errors.

I also tried the code from this post as well and I am getting the following errors when I try to pass the variable to the private variables. THIS IS THE ISSUE

http://forum.arduino.cc/index.php?topic=212824.0

If I remove the private variables it compiles OK. what am I missing?

INO code

#include <SoftwareSerial.h>
#include <LIN.h>

LINUART LIN(9600,2,4);
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

header file

#ifndef	LIN_H
#define	LIN_H

#include <Arduino.h>
#include <SoftwareSerial.h>

#define BIT_CHK(x,y) (((x)&(1<<(y)))>0? 1:0) //returns value selected bit '0' or '1'

class SoftwareSerial;

class LINUART{
private:
  static uint16_t bauddefault;
  static uint16_t Tsyncbrk;
  static uint8_t rx;
  static uint8_t tx; 
  SoftwareSerial serial;

public:
	
	LINUART(uint16_t Baud,uint8_t rxPin,uint8_t txPin):serial(rxPin,txPin)
	{
			bauddefault=Baud;                 //If I comment these out, it compiles OK
			Tsyncbrk=(13/Baud)*1000000; //
	}
};

extern LINUART LIN;

#endif

Errors

C:\Users\Sherzaad\AppData\Local\Temp\build9b353a4c388863a7faa42cd40152b720.tmp\sketch\sketch_jul11a.ino.cpp.o: In function `LINUART':

C:\Users\Sherzaad\Downloads\arduino-1.6.7\libraries\LIN/LIN.h:28: undefined reference to `LINUART::bauddefault'

C:\Users\Sherzaad\Downloads\arduino-1.6.7\libraries\LIN/LIN.h:28: undefined reference to `LINUART::bauddefault'

C:\Users\Sherzaad\Downloads\arduino-1.6.7\libraries\LIN/LIN.h:29: undefined reference to `LINUART::Tsyncbrk'

C:\Users\Sherzaad\Downloads\arduino-1.6.7\libraries\LIN/LIN.h:29: undefined reference to `LINUART::Tsyncbrk'

collect2.exe: error: ld returned 1 exit status

Any ideas... anyone?

Found the reason that was causing the error:

I declared the variables inside the class, but did not define them. Adding the definition after the class fix the errors! :slight_smile:

Header file

#ifndef	LIN_H
#define	LIN_H

#include <Arduino.h>
#include <SoftwareSerial.h>

#define BIT_CHK(x,y) (((x)&(1<<(y)))>0? 1:0) //returns value selected bit '0' or '1'

class SoftwareSerial;

class LINUART{
private:
  static uint16_t bauddefault;
  static uint16_t Tsyncbrk;
  static uint8_t rx;
  static uint8_t tx; 
  SoftwareSerial serial;

public:
	
	LINUART(uint16_t Baud,uint8_t rxPin,uint8_t txPin):serial(rxPin,txPin)
	{
			bauddefault=Baud;
			Tsyncbrk=(13/Baud)*1000000;
	}
};

uint16_t LINUART::bauddefault;
uint16_t LINUART::Tsyncbrk;
uint8_t LINUART::rx;
uint8_t LINUART::tx; 

extern LINUART LIN;

#endif

PaulS thank you for your comments even if they were not very helpful!

what u suggested I have tried

No, you did not. What I suggested was that you add a constructor to the header file and implement it in the source file. You put the implementation and definition in the header file.

The header file should NOT contain implementation code.

PaulS thank you for your comments even if they were not very helpful!

Why? Because you didn't follow them?

you are right. I moved the implementation code from the header to a .cpp file AFTER I had managed to get it working and it still is working find.

I had a fundamental coding problem in the header file and all I needed was help to understand where the error was coming from and why, which I found out by myself reading other forums.

bad coding formatting does not stop the code to work just more difficult for us humans to understand! :slight_smile: