Howto implement HardwareSerial inside my library

Hi all!

As I have posted before, I've been working on a new Modbus library. The fact is that it still needs to append the HardwareSerial inside it. At this moment, it is just outside the class declaration:

HardwareSerial port = Serial; ///< Pointer to Serial class object
/* _____CLASS DEFINITIONS____________________________________________________ */
/**
 * Arduino class library for communicating with Modbus devices over
 * RS232/485 (via RTU protocol).
 */
class Modbus {
private:
  uint8_t u8id; // 0=master, 1..247=slave number
  uint8_t u8serno; // serial port: 0-Serial, 1..3-Serial1..Serial3
  uint8_t u8txenpin; // 0=USB or RS-232 mode, >0=RS-485 mode
  uint8_t u8state;
  uint8_t au8Buffer[MAX_BUFFER];
  uint8_t u8BufferSize;
  uint8_t u8lastRec;
  uint16_t *au16regs;
  uint16_t u16InCnt, u16OutCnt, u16errCnt;
  uint16_t u16timeOut;
  uint32_t u32time, u32timeOut;
  uint8_t u8regsize;

  void init(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin);
  void sendTxBuffer(); // transmit buffer to serial port
  int8_t getRxBuffer(); // get serial buffer contents
  uint16_t calcCRC(uint8_t u8length); // get CRC from au8Buffer until u8length
  uint8_t validateAnswer();
  uint8_t validateRequest(); // validate master request
  void get_FC1(); // *** only master ***
  void get_FC3(); // *** only master ***
  int8_t process_FC1( uint16_t *regs, uint8_t u8size ); // *** only slave ***
  int8_t process_FC3( uint16_t *regs, uint8_t u8size ); // *** only slave ***
  int8_t process_FC5( uint16_t *regs, uint8_t u8size ); // *** only slave ***
  int8_t process_FC6( uint16_t *regs, uint8_t u8size ); // *** only slave ***
  int8_t process_FC15( uint16_t *regs, uint8_t u8size ); // *** only slave ***
  int8_t process_FC16( uint16_t *regs, uint8_t u8size ); // *** only slave ***
  void buildException( uint8_t u8exception ); // build exception message

public:
  Modbus();
  Modbus(uint8_t u8id, uint8_t u8serno);
  Modbus(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin);
  void begin(long u32speed);
  void begin();
  void setTimeOut( uint16_t u16timeout); // only for master
  uint16_t getTimeOut(); // only for master 
  int8_t query( modbus_t telegram ); // only for master
  int8_t poll(); // cyclic poll for master
  int8_t poll( uint16_t *regs, uint8_t u8size ); // cyclic poll for slave
  uint16_t getInCnt(); // number of incoming messages
  uint16_t getOutCnt(); // number of outcoming messages
  uint16_t getErrCnt(); // error counter
  uint8_t getID();
  uint8_t getState();
};

Whenever more than one object is declared, the processor stops working. I'm afraid that all these objects are using the same "port" variable.

How can I implement HardwareSerial inside my class?

Best Regards,

The fact is that it still needs to append the HardwareSerial inside it

No. it doesn't. There is one HardwareSerial instance for each hardware serial port. You can NOT create another one.

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

Nonsense. There are no pointers involved here.

How can I implement HardwareSerial inside my class?

You don't need to implement anything. All you need to do is USE Serial.

Thanks for your comments, PaulS!

No. it doesn't. There is one HardwareSerial instance for each hardware serial port. You can NOT create another one.

Regarding to this issue, I need to make my code refer to a HardwareSerial instance. Of course, I'm planning to create another one, but I do refer to it, isn't it?

You don't need to implement anything. All you need to do is USE Serial.

I cannot directly use Serial or Serial1. My class should point to it, but I don't know how to make it to point to it.
As a summary,

  • My class constructor declares which Serial is being used
  • My class includes methods that use anything from this Serial
  • My program may use more than one object derived from this class and each of these objects refers to different Serial instances

The question is:
How do I call this Serial-"x"?