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() {
}
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
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);
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.
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
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
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!
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!