Skip to content

Commit 199e042

Browse files
committed
Set TX pin to low when sending break
1 parent 53a477e commit 199e042

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/RS485.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
#include "RS485.h"
2121

22-
RS485Class::RS485Class(HardwareSerial& hwSerial, int rePin, int dePin) :
22+
RS485Class::RS485Class(HardwareSerial& hwSerial, int txPin, int rePin, int dePin) :
2323
_serial(&hwSerial),
24+
_txPin(txPin),
2425
_rePin(rePin),
2526
_dePin(dePin),
2627
_transmisionBegun(false)
@@ -82,6 +83,7 @@ void RS485Class::flush()
8283
size_t RS485Class::write(uint8_t b)
8384
{
8485
if (!_transmisionBegun) {
86+
setWriteError();
8587
return 0;
8688
}
8789

@@ -125,15 +127,19 @@ void RS485Class::noReceive()
125127

126128
void RS485Class::sendBreak(unsigned int duration)
127129
{
130+
_serial->flush();
128131
_serial->end();
132+
pinMode(_txPin, OUTPUT);
133+
digitalWrite(_txPin, LOW);
129134
delay(duration);
130135
_serial->begin(_baudrate, _config);
131136
}
132137

133-
void RS485Class::setPins(int rePin, int dePin)
138+
void RS485Class::setPins(int txPin, int rePin, int dePin)
134139
{
140+
_txPin = txPin;
135141
_rePin = rePin;
136142
_dePin = dePin;
137143
}
138144

139-
RS485Class RS485(SERIAL_PORT_HARDWARE, RS845_DEFAULT_RE_PIN, RS845_DEFAULT_DE_PIN);
145+
RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_RE_PIN, RS845_DEFAULT_DE_PIN);

src/RS485.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222

2323
#include <Arduino.h>
2424

25+
#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX
2526
#define RS845_DEFAULT_RE_PIN A5
2627
#define RS845_DEFAULT_DE_PIN A6
2728

2829
class RS485Class : public HardwareSerial {
2930
public:
30-
RS485Class(HardwareSerial& hwSerial, int rePin, int dePin);
31+
RS485Class(HardwareSerial& hwSerial, int txPin, int rePin, int dePin);
3132

3233
virtual void begin(unsigned long baudrate);
3334
virtual void begin(unsigned long baudrate, uint16_t config);
@@ -47,10 +48,11 @@ class RS485Class : public HardwareSerial {
4748

4849
void sendBreak(unsigned int duration);
4950

50-
void setPins(int rePin, int dePin);
51+
void setPins(int txPin, int rePin, int dePin);
5152

5253
private:
5354
HardwareSerial* _serial;
55+
int _txPin;
5456
int _rePin;
5557
int _dePin;
5658

0 commit comments

Comments
 (0)