19
19
20
20
#include " RS485.h"
21
21
22
- RS485Class::RS485Class (HardwareSerial& hwSerial, int txPin, int dePin, int rePin) :
23
- _serial(&hwSerial),
24
- _txPin(txPin),
25
- _dePin(dePin),
26
- _rePin(rePin),
27
- _transmisionBegun(false )
22
+ RS485Class::RS485Class (Stream &stream, int txPin, int dePin, int rePin) : _stream(&stream),
23
+ _txPin(txPin),
24
+ _dePin(dePin),
25
+ _rePin(rePin),
26
+ _transmissionBegun(false )
28
27
{
29
28
}
30
29
31
- void RS485Class::begin (unsigned long baudrate )
30
+ void RS485Class::begin (unsigned long _baudrate )
32
31
{
33
- begin (baudrate, SERIAL_8N1 , RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
32
+ begin (0 , 0 , RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
34
33
}
35
34
36
- void RS485Class::begin (unsigned long baudrate , int predelay, int postdelay)
35
+ void RS485Class::begin (unsigned long _baudrate , int predelay, int postdelay)
37
36
{
38
- begin (baudrate, SERIAL_8N1 , predelay, postdelay);
37
+ begin (0 , 0 , predelay, postdelay);
39
38
}
40
39
41
- void RS485Class::begin (unsigned long baudrate , uint16_t config )
40
+ void RS485Class::begin (unsigned long _baudrate , uint16_t _config )
42
41
{
43
- begin (baudrate, config , RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
42
+ begin (0 , 0 , RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
44
43
}
45
44
46
- void RS485Class::begin (unsigned long baudrate , uint16_t config , int predelay, int postdelay)
45
+ void RS485Class::begin (unsigned long _baudrate , uint16_t _config , int predelay, int postdelay)
47
46
{
48
- _baudrate = baudrate;
49
- _config = config;
50
-
51
47
// Set only if not already initialized with ::setDelays
52
48
_predelay = _predelay == 0 ? predelay : _predelay;
53
49
_postdelay = _postdelay == 0 ? postdelay : _postdelay;
54
50
55
- if (_dePin > -1 ) {
51
+ if (_dePin > -1 )
52
+ {
56
53
pinMode (_dePin, OUTPUT);
57
54
digitalWrite (_dePin, LOW);
58
55
}
59
56
60
- if (_rePin > -1 ) {
57
+ if (_rePin > -1 )
58
+ {
61
59
pinMode (_rePin, OUTPUT);
62
60
digitalWrite (_rePin, HIGH);
63
61
}
64
62
65
- _transmisionBegun = false ;
66
-
67
- _serial->begin (baudrate, config);
63
+ _transmissionBegun = false ;
68
64
}
69
65
70
66
void RS485Class::end ()
71
67
{
72
- _serial->end ();
73
-
74
- if (_rePin > -1 ) {
68
+ if (_rePin > -1 )
69
+ {
75
70
digitalWrite (_rePin, LOW);
76
71
pinMode (_dePin, INPUT);
77
72
}
78
-
79
- if (_dePin > -1 ) {
73
+
74
+ if (_dePin > -1 )
75
+ {
80
76
digitalWrite (_dePin, LOW);
81
77
pinMode (_rePin, INPUT);
82
78
}
83
79
}
84
80
85
81
int RS485Class::available ()
86
82
{
87
- return _serial ->available ();
83
+ return _stream ->available ();
88
84
}
89
85
90
86
int RS485Class::peek ()
91
87
{
92
- return _serial ->peek ();
88
+ return _stream ->peek ();
93
89
}
94
90
95
91
int RS485Class::read (void )
96
92
{
97
- return _serial ->read ();
93
+ return _stream ->read ();
98
94
}
99
95
100
96
void RS485Class::flush ()
101
97
{
102
- return _serial ->flush ();
98
+ return _stream ->flush ();
103
99
}
104
100
105
101
size_t RS485Class::write (uint8_t b)
106
102
{
107
- if (!_transmisionBegun) {
103
+ if (!_transmissionBegun)
104
+ {
108
105
setWriteError ();
109
106
return 0 ;
110
107
}
111
108
112
- return _serial ->write (b);
109
+ return _stream ->write (b);
113
110
}
114
111
115
112
RS485Class::operator bool ()
@@ -119,58 +116,60 @@ RS485Class::operator bool()
119
116
120
117
void RS485Class::beginTransmission ()
121
118
{
122
- if (_dePin > -1 ) {
119
+ if (_dePin > -1 )
120
+ {
123
121
digitalWrite (_dePin, HIGH);
124
- if (_predelay) delayMicroseconds (_predelay);
122
+ if (_predelay)
123
+ delayMicroseconds (_predelay);
125
124
}
126
125
127
- _transmisionBegun = true ;
126
+ _transmissionBegun = true ;
128
127
}
129
128
130
129
void RS485Class::endTransmission ()
131
130
{
132
- _serial ->flush ();
131
+ _stream ->flush ();
133
132
134
- if (_dePin > -1 ) {
135
- if (_postdelay) delayMicroseconds (_postdelay);
133
+ if (_dePin > -1 )
134
+ {
135
+ if (_postdelay)
136
+ delayMicroseconds (_postdelay);
136
137
digitalWrite (_dePin, LOW);
137
138
}
138
139
139
- _transmisionBegun = false ;
140
+ _transmissionBegun = false ;
140
141
}
141
142
142
143
void RS485Class::receive ()
143
144
{
144
- if (_rePin > -1 ) {
145
+ if (_rePin > -1 )
146
+ {
145
147
digitalWrite (_rePin, LOW);
146
148
}
147
149
}
148
150
149
151
void RS485Class::noReceive ()
150
152
{
151
- if (_rePin > -1 ) {
153
+ if (_rePin > -1 )
154
+ {
152
155
digitalWrite (_rePin, HIGH);
153
156
}
154
157
}
155
158
156
159
void RS485Class::sendBreak (unsigned int duration)
157
160
{
158
- _serial->flush ();
159
- _serial->end ();
161
+ _stream->flush ();
160
162
pinMode (_txPin, OUTPUT);
161
163
digitalWrite (_txPin, LOW);
162
164
delay (duration);
163
- _serial->begin (_baudrate, _config);
164
165
}
165
166
166
167
void RS485Class::sendBreakMicroseconds (unsigned int duration)
167
168
{
168
- _serial->flush ();
169
- _serial->end ();
169
+ _stream->flush ();
170
170
pinMode (_txPin, OUTPUT);
171
171
digitalWrite (_txPin, LOW);
172
172
delayMicroseconds (duration);
173
- _serial->begin (_baudrate, _config);
174
173
}
175
174
176
175
void RS485Class::setPins (int txPin, int dePin, int rePin)
@@ -185,5 +184,3 @@ void RS485Class::setDelays(int predelay, int postdelay)
185
184
_predelay = predelay;
186
185
_postdelay = postdelay;
187
186
}
188
-
189
- RS485Class RS485 (SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
0 commit comments