Lydia - Printhead
Modbus Object Instantiation/Initialization

Functions

void Modbus::begin (long u32speed)
 Initialize class object. More...
 
void Modbus::begin (long u32speed, uint8_t u8config)
 Initialize class object. More...
 
void Modbus::begin (SoftwareSerial *sPort, long u32speed)
 Initialize class object. More...
 
uint8_t Modbus::getID ()
 get slave ID between 1 and 247 More...
 
 Modbus::Modbus ()
 Default Constructor for Master through Serial. More...
 
void Modbus::setID (uint8_t u8id)
 write new ID for the slave More...
 
void Modbus::setTimeOut (uint16_t u16timeout)
 write communication watch-dog timer More...
 

Detailed Description

Function Documentation

◆ begin() [1/3]

void Modbus::begin ( long  u32speed)

Initialize class object.

Sets up the serial port using specified baud rate. Call once class has been instantiated, typically within setup().

See also
http://arduino.cc/en/Serial/Begin#.Uy4CJ6aKlHY
Parameters
speedbaud rate, in standard increments (300..115200)

Definition at line 298 of file ModbusRtu.h.

299 {
300 
301  switch (u8serno)
302  {
303 #if defined(UBRR1H)
304  case 1:
305  port = &Serial1;
306  break;
307 #endif
308 
309 #if defined(UBRR2H)
310  case 2:
311  port = &Serial2;
312  break;
313 #endif
314 
315 #if defined(UBRR3H)
316  case 3:
317  port = &Serial3;
318  break;
319 #endif
320  case 0:
321  default:
322 
323  port = &Serial;
324  break;
325  }
326 
327  port->begin(u32speed);
328  if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
329  {
330  // return RS485 transceiver to transmit mode
334  // pinMode(u8txenpin, OUTPUT);
335  // digitalWrite(u8txenpin, LOW);
336  }
337 
338  while (port->read() >= 0)
339  ;
340  u8lastRec = u8BufferSize = 0;
341  u16InCnt = u16OutCnt = u16errCnt = 0;
342 }

◆ begin() [2/3]

void Modbus::begin ( long  u32speed,
uint8_t  u8config 
)

Initialize class object.

Sets up the serial port using specified baud rate. Call once class has been instantiated, typically within setup().

See also
http://arduino.cc/en/Serial/Begin#.Uy4CJ6aKlHY
Parameters
speedbaud rate, in standard increments (300..115200)
configdata frame settings (data length, parity and stop bits)

Definition at line 395 of file ModbusRtu.h.

396 {
397 
398  switch (u8serno)
399  {
400 #if defined(UBRR1H)
401  case 1:
402  port = &Serial1;
403  break;
404 #endif
405 
406 #if defined(UBRR2H)
407  case 2:
408  port = &Serial2;
409  break;
410 #endif
411 
412 #if defined(UBRR3H)
413  case 3:
414  port = &Serial3;
415  break;
416 #endif
417  case 0:
418  default:
419  port = &Serial;
420  break;
421  }
422 
423  port->begin(u32speed, u8config);
424  if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
425  {
426  // return RS485 transceiver to transmit mode
430  // pinMode(u8txenpin, OUTPUT);
431  // digitalWrite(u8txenpin, LOW);
432  }
433 
434  while (port->read() >= 0)
435  ;
436  u8lastRec = u8BufferSize = 0;
437  u16InCnt = u16OutCnt = u16errCnt = 0;
438 }

◆ begin() [3/3]

void Modbus::begin ( SoftwareSerial *  sPort,
long  u32speed 
)

Initialize class object.

Sets up the software serial port using specified baud rate and SoftwareSerial object. Call once class has been instantiated, typically within setup().

Parameters
speed*softPort, pointer to SoftwareSerial class object
speedbaud rate, in standard increments (300..115200)

Definition at line 360 of file ModbusRtu.h.

361 {
362 
363  softPort = sPort;
364 
365  softPort->begin(u32speed);
366 
367  if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
368  {
369  // return RS485 transceiver to transmit mode
373  // pinMode(u8txenpin, OUTPUT);
374  // digitalWrite(u8txenpin, LOW);
375  }
376 
377  while (softPort->read() >= 0)
378  ;
379  u8lastRec = u8BufferSize = 0;
380  u16InCnt = u16OutCnt = u16errCnt = 0;
381 }

◆ getID()

uint8_t Modbus::getID ( )

get slave ID between 1 and 247

Method to read current slave ID address.

Returns
u8id current slave address between 1 and 247

Definition at line 477 of file ModbusRtu.h.

478 {
479  return this->u8id;
480 }

◆ Modbus()

Modbus::Modbus ( )

Default Constructor for Master through Serial.

Definition at line 228 of file ModbusRtu.h.

229 {
230  init(0, 0, 0);
231  debugSend = false;
232 }

◆ setID()

void Modbus::setID ( uint8_t  u8id)

write new ID for the slave

Method to write a new slave ID address.

Parameters
u8idnew slave address between 1 and 247

Definition at line 462 of file ModbusRtu.h.

463 {
464  if ((u8id != 0) && (u8id <= 247))
465  {
466  this->u8id = u8id;
467  }
468 }

◆ setTimeOut()

void Modbus::setTimeOut ( uint16_t  u16timeOut)

write communication watch-dog timer

Initialize time-out parameter.

Call once class has been instantiated, typically within setup(). The time-out timer is reset each time that there is a successful communication between Master and Slave. It works for both.

Parameters
time-outvalue (ms)

Definition at line 493 of file ModbusRtu.h.

494 {
495  this->u16timeOut = u16timeOut;
496 }
RS485_CLEAR_DE
#define RS485_CLEAR_DE
Definition: ModbusRtu.h:51
RS485_PIN_DE
#define RS485_PIN_DE
Definition: ModbusRtu.h:47
Modbus::u8BufferSize
uint8_t u8BufferSize
Definition: ModbusRtu.h:163
Modbus::debugSend
bool debugSend
Definition: ModbusRtu.h:214
Modbus::u8lastRec
uint8_t u8lastRec
Definition: ModbusRtu.h:164
Modbus::u16errCnt
uint16_t u16errCnt
Definition: ModbusRtu.h:166
RS485_PIN_RE
#define RS485_PIN_RE
Definition: ModbusRtu.h:48
Modbus::u8serno
uint8_t u8serno
serial port: 0-Serial, 1..3-Serial1..Serial3; 4: use software serial
Definition: ModbusRtu.h:158
Modbus::u16OutCnt
uint16_t u16OutCnt
Definition: ModbusRtu.h:166
Modbus::init
void init(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
Definition: ModbusRtu.h:879
Modbus::u16InCnt
uint16_t u16InCnt
Definition: ModbusRtu.h:166
RS485_CLEAR_RE
#define RS485_CLEAR_RE
Definition: ModbusRtu.h:52
Modbus::u16timeOut
uint16_t u16timeOut
Definition: ModbusRtu.h:167
Modbus::u8txenpin
uint8_t u8txenpin
flow control pin: 0=USB or RS-232 mode, >0=RS-485 mode
Definition: ModbusRtu.h:159
RS485_DIR_REG
#define RS485_DIR_REG
Definition: ModbusRtu.h:49
Modbus::softPort
SoftwareSerial * softPort
Pointer to SoftwareSerial class object.
Definition: ModbusRtu.h:156
Modbus::port
HardwareSerial * port
Pointer to Serial class object.
Definition: ModbusRtu.h:155
Modbus::u8id
uint8_t u8id
0=master, 1..247=slave number
Definition: ModbusRtu.h:157