19
19
20
20
#include " RS485.h"
21
21
22
- RS485Class::RS485Class (HardwareSerial& hwSerial, int txPin, int rePin , int dePin ) :
22
+ RS485Class::RS485Class (HardwareSerial& hwSerial, int txPin, int dePin , int rePin ) :
23
23
_serial(&hwSerial),
24
24
_txPin(txPin),
25
- _rePin(rePin),
26
25
_dePin(dePin),
26
+ _rePin(rePin),
27
27
_transmisionBegun(false )
28
28
{
29
29
}
@@ -38,11 +38,15 @@ void RS485Class::begin(unsigned long baudrate, uint16_t config)
38
38
_baudrate = baudrate;
39
39
_config = config;
40
40
41
- pinMode (_rePin, OUTPUT);
42
- pinMode (_dePin, OUTPUT);
41
+ if (_dePin > -1 ) {
42
+ pinMode (_dePin, OUTPUT);
43
+ digitalWrite (_dePin, LOW);
44
+ }
43
45
44
- digitalWrite (_rePin, HIGH);
45
- digitalWrite (_dePin, LOW);
46
+ if (_rePin > -1 ) {
47
+ pinMode (_rePin, OUTPUT);
48
+ digitalWrite (_rePin, HIGH);
49
+ }
46
50
47
51
_transmisionBegun = false ;
48
52
@@ -53,11 +57,15 @@ void RS485Class::end()
53
57
{
54
58
_serial->end ();
55
59
56
- digitalWrite (_rePin, LOW);
57
- digitalWrite (_dePin, LOW);
58
-
59
- pinMode (_rePin, INPUT);
60
- pinMode (_dePin, INPUT);
60
+ if (_rePin > -1 ) {
61
+ digitalWrite (_rePin, LOW);
62
+ pinMode (_dePin, INPUT);
63
+ }
64
+
65
+ if (_dePin > -1 ) {
66
+ digitalWrite (_dePin, LOW);
67
+ pinMode (_rePin, INPUT);
68
+ }
61
69
}
62
70
63
71
int RS485Class::available ()
@@ -97,29 +105,38 @@ RS485Class::operator bool()
97
105
98
106
void RS485Class::beginTransmission ()
99
107
{
100
- digitalWrite (_dePin, HIGH);
101
- delayMicroseconds (50 );
108
+ if (_dePin > -1 ) {
109
+ digitalWrite (_dePin, HIGH);
110
+ delayMicroseconds (50 );
111
+ }
102
112
103
113
_transmisionBegun = true ;
104
114
}
105
115
106
116
void RS485Class::endTransmission ()
107
117
{
108
118
_serial->flush ();
109
- delayMicroseconds (50 );
110
- digitalWrite (_dePin, LOW);
119
+
120
+ if (_dePin > -1 ) {
121
+ delayMicroseconds (50 );
122
+ digitalWrite (_dePin, LOW);
123
+ }
111
124
112
125
_transmisionBegun = false ;
113
126
}
114
127
115
128
void RS485Class::receive ()
116
129
{
117
- digitalWrite (_rePin, LOW);
130
+ if (_rePin > -1 ) {
131
+ digitalWrite (_rePin, LOW);
132
+ }
118
133
}
119
134
120
135
void RS485Class::noReceive ()
121
136
{
122
- digitalWrite (_rePin, HIGH);
137
+ if (_rePin > -1 ) {
138
+ digitalWrite (_rePin, HIGH);
139
+ }
123
140
}
124
141
125
142
void RS485Class::sendBreak (unsigned int duration)
@@ -142,11 +159,11 @@ void RS485Class::sendBreakMicroseconds(unsigned int duration)
142
159
_serial->begin (_baudrate, _config);
143
160
}
144
161
145
- void RS485Class::setPins (int txPin, int rePin , int dePin )
162
+ void RS485Class::setPins (int txPin, int dePin , int rePin )
146
163
{
147
164
_txPin = txPin;
148
- _rePin = rePin;
149
165
_dePin = dePin;
166
+ _rePin = rePin;
150
167
}
151
168
152
- RS485Class RS485 (SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_RE_PIN, RS845_DEFAULT_DE_PIN );
169
+ RS485Class RS485 (SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN );
0 commit comments