Skip to content

Commit a0989f7

Browse files
authored
Merge pull request #43 from MathiasMoog/fix_software_serial
Use SoftwareSerial with RS485
2 parents 4272801 + 98f232d commit a0989f7

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

ModbusRtu.h

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class Modbus
195195
Modbus(uint8_t u8id);
196196
void begin(long u32speed);
197197
void begin(SoftwareSerial *sPort, long u32speed);
198+
void begin(SoftwareSerial *sPort, long u32speed, uint8_t u8txenpin);
198199
//void begin(long u32speed, uint8_t u8config);
199200
void begin();
200201
void setTimeOut( uint16_t u16timeOut); //!<write communication watch-dog timer
@@ -339,8 +340,8 @@ void Modbus::begin(long u32speed)
339340
* Sets up the software serial port using specified baud rate and SoftwareSerial object.
340341
* Call once class has been instantiated, typically within setup().
341342
*
342-
* @param speed *softPort, pointer to SoftwareSerial class object
343-
* @param speed baud rate, in standard increments (300..115200)
343+
* @param sPort *softPort, pointer to SoftwareSerial class object
344+
* @param u32speed baud rate, in standard increments (300..115200)
344345
* @ingroup setup
345346
*/
346347
void Modbus::begin(SoftwareSerial *sPort, long u32speed)
@@ -362,6 +363,38 @@ void Modbus::begin(SoftwareSerial *sPort, long u32speed)
362363
u16InCnt = u16OutCnt = u16errCnt = 0;
363364
}
364365

366+
/**
367+
* @brief
368+
* Initialize class object.
369+
*
370+
* Sets up the software serial port using specified baud rate and SoftwareSerial object.
371+
* Call once class has been instantiated, typically within setup().
372+
*
373+
* @param *sPort pointer to SoftwareSerial class object
374+
* @param u32speed baud rate, in standard increments (300..115200)
375+
* @param u8txenpin pin for txen RS-485 (=0 means USB/RS232C mode)
376+
* @ingroup setup
377+
*/
378+
void Modbus::begin(SoftwareSerial *sPort, long u32speed, uint8_t u8txenpin)
379+
{
380+
381+
this->u8txenpin=u8txenpin;
382+
softPort=sPort;
383+
384+
softPort->begin(u32speed);
385+
386+
if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
387+
{
388+
// return RS485 transceiver to transmit mode
389+
pinMode(u8txenpin, OUTPUT);
390+
digitalWrite(u8txenpin, LOW);
391+
}
392+
393+
while(softPort->read() >= 0);
394+
u8lastRec = u8BufferSize = 0;
395+
u16InCnt = u16OutCnt = u16errCnt = 0;
396+
}
397+
365398
/**
366399
* @brief
367400
* Initialize class object.
@@ -914,8 +947,6 @@ int8_t Modbus::getRxBuffer()
914947
*/
915948
void Modbus::sendTxBuffer()
916949
{
917-
uint8_t i = 0;
918-
919950
// append CRC to message
920951
uint16_t u16crc = calcCRC( u8BufferSize );
921952
au8Buffer[ u8BufferSize ] = u16crc >> 8;
@@ -1131,7 +1162,7 @@ void Modbus::buildException( uint8_t u8exception )
11311162
*/
11321163
void Modbus::get_FC1()
11331164
{
1134-
uint8_t u8byte, i, maxI;
1165+
uint8_t u8byte, i;
11351166
u8byte = 3;
11361167
for (i=0; i< au8Buffer[2]; i++) {
11371168

@@ -1176,7 +1207,7 @@ void Modbus::get_FC3()
11761207
* @return u8BufferSize Response to master length
11771208
* @ingroup discrete
11781209
*/
1179-
int8_t Modbus::process_FC1( uint16_t *regs, uint8_t u8size )
1210+
int8_t Modbus::process_FC1( uint16_t *regs, uint8_t /*u8size*/ )
11801211
{
11811212
uint8_t u8currentRegister, u8currentBit, u8bytesno, u8bitsno;
11821213
uint8_t u8CopyBufferSize;
@@ -1230,7 +1261,7 @@ int8_t Modbus::process_FC1( uint16_t *regs, uint8_t u8size )
12301261
* @return u8BufferSize Response to master length
12311262
* @ingroup register
12321263
*/
1233-
int8_t Modbus::process_FC3( uint16_t *regs, uint8_t u8size )
1264+
int8_t Modbus::process_FC3( uint16_t *regs, uint8_t /*u8size*/ )
12341265
{
12351266

12361267
uint8_t u8StartAdd = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] );
@@ -1262,7 +1293,7 @@ int8_t Modbus::process_FC3( uint16_t *regs, uint8_t u8size )
12621293
* @return u8BufferSize Response to master length
12631294
* @ingroup discrete
12641295
*/
1265-
int8_t Modbus::process_FC5( uint16_t *regs, uint8_t u8size )
1296+
int8_t Modbus::process_FC5( uint16_t *regs, uint8_t /*u8size*/ )
12661297
{
12671298
uint8_t u8currentRegister, u8currentBit;
12681299
uint8_t u8CopyBufferSize;
@@ -1295,7 +1326,7 @@ int8_t Modbus::process_FC5( uint16_t *regs, uint8_t u8size )
12951326
* @return u8BufferSize Response to master length
12961327
* @ingroup register
12971328
*/
1298-
int8_t Modbus::process_FC6( uint16_t *regs, uint8_t u8size )
1329+
int8_t Modbus::process_FC6( uint16_t *regs, uint8_t /*u8size*/ )
12991330
{
13001331

13011332
uint8_t u8add = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] );
@@ -1321,7 +1352,7 @@ int8_t Modbus::process_FC6( uint16_t *regs, uint8_t u8size )
13211352
* @return u8BufferSize Response to master length
13221353
* @ingroup discrete
13231354
*/
1324-
int8_t Modbus::process_FC15( uint16_t *regs, uint8_t u8size )
1355+
int8_t Modbus::process_FC15( uint16_t *regs, uint8_t /*u8size*/ )
13251356
{
13261357
uint8_t u8currentRegister, u8currentBit, u8frameByte, u8bitsno;
13271358
uint8_t u8CopyBufferSize;
@@ -1377,9 +1408,8 @@ int8_t Modbus::process_FC15( uint16_t *regs, uint8_t u8size )
13771408
* @return u8BufferSize Response to master length
13781409
* @ingroup register
13791410
*/
1380-
int8_t Modbus::process_FC16( uint16_t *regs, uint8_t u8size )
1411+
int8_t Modbus::process_FC16( uint16_t *regs, uint8_t /*u8size*/ )
13811412
{
1382-
uint8_t u8func = au8Buffer[ FUNC ]; // get the original FUNC code
13831413
uint8_t u8StartAdd = au8Buffer[ ADD_HI ] << 8 | au8Buffer[ ADD_LO ];
13841414
uint8_t u8regsno = au8Buffer[ NB_HI ] << 8 | au8Buffer[ NB_LO ];
13851415
uint8_t u8CopyBufferSize;

0 commit comments

Comments
 (0)