Arduino sketch not compiling


#1

I am trying to convert an Arduino sketch to run on a Photon but I am getting strange errors. The errors are below.

could not convert ‘_fetch_usbserial()’ from ‘USBSerial’ to ‘HardwareSerial& {aka USARTSerial&}’

The class is being instantiated using the following statement:

Leddar16 Leddar1(115200,1);

void setup()
{
	//Initialize Leddar 
	Leddar1.init();
}

Here is the .h file. I would appreciate any help to determine the cause.

#ifndef Leddar_h
#define Leddar_h

/* _____STANDARD INCLUDES____________________________________________________ */
#include "Arduino.h"

/* _____PROJECT INCLUDES_____________________________________________________ */


/* _____LIBRARY DEFINITIONS__________________________________________________ */


#define DEFAULT_BAUD_RATE 115200

// Default address for the slave
#define DEFAULT_ADDRESS 0x01

//Error codes
#define ERR_LEDDAR_BAD_CRC -1
#define ERR_LEDDAR_NO_RESPONSE -2
#define ERR_LEDDAR_BAD_RESPONSE -3
#define ERR_LEDDAR_SHORT_RESPONSE -4
#define ERR_LEDDAR_SERIAL_PORT -5

// Number of detections does not match 
#define ERR_LEDDAR_NB_DETECTIONS -6;

/// Represents a measurement:
struct Detection
{
	unsigned char Segment;
	unsigned int Distance;
	unsigned int Amplitude;
	
	// Default constructor
	Detection() : Segment(0), Distance(0xFFFF), Amplitude(0) { }
};

class Leddar16
{
public:
	Leddar16() 
	: BaudRate(DEFAULT_BAUD_RATE), SlaveAddress(DEFAULT_ADDRESS), NbDet(0), TxEn_Pin(0), TxEn_Action(0), SerialPort(Serial) 
	{ }
	
	Leddar16( unsigned long Baud, unsigned char Addr = DEFAULT_ADDRESS, HardwareSerial& Port = Serial, unsigned char Pin = 0, unsigned char Action = 0) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Port)
	{ }
	
	Leddar16( unsigned long Baud, unsigned char Addr, unsigned char Pin , unsigned char Action ) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Serial)
	{ }
	
	void init();
	char getDetections();
	char sendRequest();
	char parseResponse();
	void clearDetections();

public:
	unsigned char NbDet;
	Detection Detections[50];
	unsigned long TimeStamp;
	unsigned char LEDPower;
	unsigned char Status;   

private:
	unsigned long BaudRate;
	unsigned char SlaveAddress;
	unsigned char TxEn_Pin;
	unsigned char TxEn_Action;
	HardwareSerial& SerialPort;
};

#endif

#2

Could you share the rest of your sketch? Maybe use the SHARE THIS REVISION button?


#3

Hello @nrobinson2000,

The code is found at the link below. The specific example I am trying to run is Simple16channelNoLCD.ino.

https://playground.arduino.cc/Code/Leddar


#4

In the (pure) Paritcle world there is no implementation of HardwareSerial but here it’s called USARTSerial for the interface conneted to the RX/TX pins (aka Serial1) and USBSerial for … well … the USB Serial port (Serial).

But the main issue I’d suspect is that you actually can’t assign Serial (USBSerial) to a USARTSerial object. These two interfaces are “incompatible”.

That’s what that error message is trying to tell you.

Try replacing the instances of Serial (default values in the library constructors) with Serial1


#5

Thank you very much for your time @ScruffR.

I changed all the instances of SerialPort in the .cpp file to Serial1 but would appreciate your help with the .h file as I have no experience with them.

The lines that most likely need to be changed are:

	Leddar16() 
	: BaudRate(DEFAULT_BAUD_RATE), SlaveAddress(DEFAULT_ADDRESS), NbDet(0), TxEn_Pin(0), TxEn_Action(0), SerialPort(Serial) 
	{ }
	
	Leddar16( unsigned long Baud, unsigned char Addr = DEFAULT_ADDRESS, HardwareSerial& Port = Serial, unsigned char Pin = 0, unsigned char Action = 0) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Port)
	{ }
	
	Leddar16( unsigned long Baud, unsigned char Addr, unsigned char Pin , unsigned char Action ) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Serial)
	{ }

and

private:
	unsigned long BaudRate;
	unsigned char SlaveAddress;
	unsigned char TxEn_Pin;
	unsigned char TxEn_Action;
	HardwareSerial& SerialPort;  

Here is the entire .h file code:

// *****************************************************************************
//
/// \file    Leddar.h
///
/// \brief   Arduino library for communicating with a Leddar(TM)  via Modbus RTU
///          protocol over UART/RS485.  Supports 16-channel and single 
///          channel sensors.
///
//  This file is part of the Leddar Library.
//  
//  
// The LeddarTM Arduino Library is licensed under the terms of the MIT License,
// as is without any warranty. See License.txt for more details.
//
// Copyright© LeddarTech, Inc. 2015
// *****************************************************************************

#ifndef Leddar_h
#define Leddar_h

/* _____STANDARD INCLUDES____________________________________________________ */
#include "Arduino.h"

/* _____PROJECT INCLUDES_____________________________________________________ */


/* _____LIBRARY DEFINITIONS__________________________________________________ */


#define DEFAULT_BAUD_RATE 115200

// Default address for the slave
#define DEFAULT_ADDRESS 0x01

//Error codes
#define ERR_LEDDAR_BAD_CRC -1
#define ERR_LEDDAR_NO_RESPONSE -2
#define ERR_LEDDAR_BAD_RESPONSE -3
#define ERR_LEDDAR_SHORT_RESPONSE -4
#define ERR_LEDDAR_SERIAL_PORT -5

// Number of detections does not match 
#define ERR_LEDDAR_NB_DETECTIONS -6;


/* _____CLASS DEFINITIONS____________________________________________________ */

/// Represents a measurement:
struct Detection
{
	unsigned char Segment;
	unsigned int Distance;
	unsigned int Amplitude;
	
	// Default constructor
	Detection() : Segment(0), Distance(0xFFFF), Amplitude(0) { }
};

/// Class to connect to a 16-channel Leddar module (Evaluation Kit, M16, IS16):
class Leddar16
{
public:
	Leddar16() 
	: BaudRate(DEFAULT_BAUD_RATE), SlaveAddress(DEFAULT_ADDRESS), NbDet(0), TxEn_Pin(0), TxEn_Action(0), SerialPort(Serial) 
	{ }
	
	Leddar16( unsigned long Baud, unsigned char Addr = DEFAULT_ADDRESS, HardwareSerial& Port = Serial, unsigned char Pin = 0, unsigned char Action = 0) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Port)
	{ }
	
	Leddar16( unsigned long Baud, unsigned char Addr, unsigned char Pin , unsigned char Action ) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Serial)
	{ }
	
	void init();
	char getDetections();
	char sendRequest();
	char parseResponse();
	void clearDetections();

public:
	unsigned char NbDet;
	Detection Detections[50];
	unsigned long TimeStamp;
	unsigned char LEDPower;
	unsigned char Status;   

private:
	unsigned long BaudRate;
	unsigned char SlaveAddress;
	unsigned char TxEn_Pin;
	unsigned char TxEn_Action;
	HardwareSerial& SerialPort;
};

#endif

#6

These are the three constructors I meant above.
Each of them has Serial set as default value that you should change to Serial1.

You should not have changed SerialPort tho’ as this is the way to allow the user of the library to actually select the desired serial port.


#7

Thank you very much @ScruffR. I will change it back in the cpp file.

Just one last question, what do I do with:

HardwareSerial& SerialPort;

Thanks again.


#8

Just leave it as is (otherwise I would have said - I expect it to build that way :wink: )

You could change it to

USARTSerial& SerialPort;

but that should not be required, since you already #include "Arduino.h" which has a

typedef USARTSerial HardwareSerial;

setup


#9

Hello @ScruffR,

Thank you for helping me with the code change below. It has been working flawlessly.

	Leddar16( unsigned long Baud, unsigned char Addr = DEFAULT_ADDRESS, HardwareSerial& Port = **Serial1**, unsigned char Pin = 0, unsigned char Action = 0) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Port)
	{ }

Can you tell me how to make “Serial1” a variable so it can be set as the first or second serial port on a Xenon depending on the value of another variable?

Thank you in advance.


#10

In Particle land HardwareSerial would be called USARTSerial


#11

Thanks @ScruffR. The code changes you made work. I just wanted to ask how does one make “Serial1” a variable so the same code can be used with a different serial port depending on some value …


#12

The code you quoted shows how it’s done.

  • you create a variable/reference of the respective type (e.g. USARTSerial & mySerial)
  • the constructor provides a parameter (as seen in the Leddar16() example above)
  • when called the constructor will set the reference (seen above like above SerialPort(Port))

#13

Thank you @Scruffr for your time and sorry for my lack of C++ knowledge. Please indulge one final clarification to my question:

Currently, I am using:

Leddar16 Leddar1(115200, 1);

which operates on (and therefore uses Serial1):

	Leddar16( unsigned long Baud, unsigned char Addr = DEFAULT_ADDRESS, HardwareSerial& Port =  Serial1, unsigned char Pin = 0, unsigned char Action = 0) 
	: BaudRate(Baud), SlaveAddress(Addr), NbDet(0), TxEn_Pin(Pin), TxEn_Action(Action)  , SerialPort(Port)
	{ }

How can I (for example) when executing:

        `Leddar16 Leddar1(115200, 1, 1);`     Use Serial1

        `Leddar16 Leddar1(115200, 1, 2);`     Use Serial2

This is what I am trying to do …


#14

When you look at the parameter list, you see some parameter names followed by = something

This is what you’d call a default parameter. When it is not provided in the call the default will be assumed.

Hence when you write Leddar16 Leddar1(115200, 1); you are actually calling Leddar16 Leddar1(115200, 1, Serial1, 0, 0);

Now taking that info an comparing it to your version of the call should show you how to do it right.


#15

Thank you @Scruffr for explaining the default values.

I changed the code to:

#include "Serial2/Serial2.h"

...
...
Leddar16 Leddar1(115200,1, Serial2);

It now compiles but I have not tested the code with the sensor yet.

Thanks again.