Modbus over SoftwareSerial


is it possbile to modify the Modbus library to send and receive messages from Software serial instead of Hardware one?

I’m quite new and I stared to create an object softwareserial inside the ModBus library but the compile show an error.

Code part modified tryin to create softwareserial

HardwareSerial MBSerial = Serial; ///< Pointer to Serial class object

/* Try to instantiate the Softwareserial object*/
#include “…\SoftwareSerial\SoftwareSerial.h”
//define the software serial tx and rx pins
#define rxPin 9
#define txPin 8

// set up the ‘fake’ serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

And the compiler answer is…

ModbusMaster\ModbusMaster.cpp.o: In function __static_initialization_and_destruction_0': E:\Arduino\arduino-0021\libraries\ModbusMaster/ModbusMaster.cpp:46: undefined reference to SoftwareSerial::SoftwareSerial(unsigned char, unsigned char)’


First off, SoftwareSerial os obsolete. You should be using NewSoftSerial.

The compiler is telling you that it can’t find the SoftwareSerial constructor in any of the files it is trying to link together. The only files that are actually compiled and linked are those that have header files included in the sketch. What does your sketch look like?

Thanks for the tips PaulS,

I tried to change to NewSoftSerial without news
Of course there is someting that i don’t have completly understand about OOP :stuck_out_tongue:

  • I copy the new softserial library into \arduino\library\NewSoftSerial\

  • Change the ModBus library code (ModbusMaster.cpp) into:

/* PROJECT INCLUDES____________ */
#include “ModbusMaster.h”
#include “…\NewSoftSerial\NewSoftSerial.h”

/* GLOBAL VARIABLES_________ */
HardwareSerial MBSerial = Serial; ///< Pointer to Serial class object

NewSoftSerial mySerial(2, 3);

  • The compiler error is

ModbusMaster\ModbusMaster.cpp.o: In function `__static_initialization_and_destruction_0’:

E:\Arduino\arduino-0021\libraries\ModbusMaster/ModbusMaster.cpp:41: undefined reference to `NewSoftSerial::NewSoftSerial(unsigned char, unsigned char, bool)’

E:\Arduino\arduino-0021\libraries\ModbusMaster/ModbusMaster.cpp:41: undefined reference to `NewSoftSerial::~NewSoftSerial()’


Changing from SoftwareSerial to NewSoftSerial doesn't change the fact that NewSoftSerial.h needs to be included in your sketch in order for NewSoftSerial.cpp to be compiled. I'll repeat my question. What does your sketch look like?


well in the sketch folder i have both: \Arduino-0021\libraries\NewSoftSerial \Arduino-0021\libraries\ModbusMaster ... other library

The Arduino IDE correctly find both two libraries and i able to see them into the sketch menu.

The strange think that I'm not secure to understand is that to create an object Newsoftserial inside the ModbusMaster library i have to include NewSoftSerial.h file into: - the example file (Basic.pde) and at the same time - into the ModbusMaster.cpp

The Arduino IDE determines what needs to be compiled by looking at the header files included in the sketch. If NewSoftSerial.h is not included in the sketch, the NewSoftSerial.cpp file will not be compiled, even though the header file is included in ModbusMaster.h or ModbusMaster.cpp.

As a result, you'll get unresolved external references to the NewSoftSerial methods.