Skip to content

Commit

Permalink
added pointer to serial to use other than hardcoded interface
Browse files Browse the repository at this point in the history
  • Loading branch information
brainelectronics authored and angeloc committed Jul 15, 2019
1 parent 6cc27c9 commit 725632a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
@@ -1,5 +1,4 @@
#include "SimpleModbusSlaveSoftwareSerial.h"
#include "SoftwareSerial.h"

#define BUFFER_SIZE 128

Expand All @@ -14,31 +13,30 @@ unsigned char TxEnablePin;
unsigned int errorCount;
unsigned int T1_5; // inter character time out
unsigned int T3_5; // frame delay
SoftwareSerial* _port;

// function definitions
void exceptionResponse(unsigned char exception);
unsigned int calculateCRC(unsigned char bufferSize);
void sendPacket(unsigned char bufferSize);

SoftwareSerial mySerial(0, 1);

unsigned int modbus_update(unsigned int *holdingRegs)
{
unsigned char buffer = 0;
unsigned char overflow = 0;

while (mySerial.available())
while ((*_port).available())
{
// The maximum number of bytes is limited to the serial buffer size of 128 bytes
// If more bytes is received than the BUFFER_SIZE the overflow flag will be set and the
// serial buffer will be red untill all the data is cleared from the receive buffer.
if (overflow)
mySerial.read();
(*_port).read();
else
{
if (buffer == BUFFER_SIZE)
overflow = 1;
frame[buffer] = mySerial.read();
frame[buffer] = (*_port).read();
buffer++;
}
delayMicroseconds(T1_5); // inter character time out
Expand Down Expand Up @@ -190,10 +188,11 @@ void exceptionResponse(unsigned char exception)
}
}

void modbus_configure(long baud, unsigned char _slaveID, unsigned char _TxEnablePin, unsigned int _holdingRegsSize)
void modbus_configure(SoftwareSerial* comPort, long baud, unsigned char _slaveID, unsigned char _TxEnablePin, unsigned int _holdingRegsSize)
{
_port = comPort;
slaveID = _slaveID;
mySerial.begin(baud);
(*_port).begin(baud);

if (_TxEnablePin > 1)
{ // pin 0 & pin 1 are reserved for RX/TX. To disable set txenpin < 2
Expand Down Expand Up @@ -254,9 +253,9 @@ void sendPacket(unsigned char bufferSize)
digitalWrite(TxEnablePin, HIGH);

for (unsigned char i = 0; i < bufferSize; i++)
mySerial.write(frame[i]);
(*_port).write(frame[i]);

mySerial.flush();
(*_port).flush();

// allow a frame delay to indicate end of transmission
delayMicroseconds(T3_5);
Expand Down
Expand Up @@ -83,7 +83,7 @@
#include "SoftwareSerial.h"

// function definitions
void modbus_configure(long baud, byte _slaveID, byte _TxEnablePin, unsigned int _holdingRegsSize);
void modbus_configure(SoftwareSerial* comPort, long baud, unsigned char _slaveID, unsigned char _TxEnablePin, unsigned int _holdingRegsSize);
unsigned int modbus_update(unsigned int *holdingRegs);


Expand Down
Expand Up @@ -67,9 +67,20 @@ enum
unsigned int holdingRegs[TOTAL_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////

#define RX 0 // Arduino defined pin (PB0, package pin #5)
#define TX 1 // Arduino defined pin (PB1, package pin #6)
#define RS485_EN 2 // pin to set transmission mode on RS485 chip (PB2, package pin #7)
#define BAUD_RATE 9600 // baud rate for serial communication
#define deviceID 1 // this device address

// SoftwareSerial mySerial(receive pin, transmit pin)
SoftwareSerial rs485(RX, TX);

void setup()
{
/* parameters(long baudrate,
/* parameters(
SoftwareSerial* comPort
long baudrate,
unsigned char ID,
unsigned char transmit enable pin,
unsigned int holding registers size)
Expand All @@ -78,7 +89,7 @@ void setup()
to deactivate this mode use any value < 2 because 0 & 1 is reserved for Rx & Tx
*/

modbus_configure(9600, 1, 2, TOTAL_REGS_SIZE);
modbus_configure(&rs485, BAUD_RATE, deviceID, RS485_EN, TOTAL_REGS_SIZE);
pinMode(ADC1, INPUT);
pinMode(ADC2, INPUT);
}
Expand Down

0 comments on commit 725632a

Please sign in to comment.