This repository was archived by the owner on Mar 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRS485.cpp
184 lines (152 loc) · 3.28 KB
/
RS485.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <RS485.h>
#include "Arduino.h"
SoftwareSerial rs485 (RX_PIN, TX_PIN);
void fWrite (const byte what)
{
rs485.write (what);
//Serial.print((int)what);
//Serial.print(' ');
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
byte crc8 (const char *addr)
{
byte len = 0;
while((byte)addr[len] != '\0')
{
len++;
}
byte crc = 0;
while (len--)
{
byte inbyte = *addr++;
for (byte i = 8; i; i--)
{
byte mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix)
crc ^= 0x8C;
inbyte >>= 1;
} // end of for
} // end of while
return crc;
} // end of crc8
void EncodeMessage(char message[maxMsgLen+1], unsigned char data[maxMsgLen+1+3])
{
int index=0;
int index2 = 0;
data[index] = STX;
while(message[index2] != '\0')
{
index++;
data[index] = (char)message[index2];
index2++;
}
index++;
data[index] = ETX;
index++;
data[index] = (char)crc8(message);
index++;
data[index] = '\0';
}
void RS485_Begin(int baud)
{
pinMode(ENABLE_PIN, OUTPUT); // driver output enable
rs485.begin(baud);
}
bool RS485_SendMessage(char message[maxMsgLen+1],WriteCallback fWrite,int EnablePin)
{
unsigned char temp[maxMsgLen+1+2];
EncodeMessage(message,temp);
digitalWrite (EnablePin, HIGH); // enable sending
int counter =0;
while(temp[counter] != '\0')
{
fWrite(temp[counter]);
counter++;
}
delay(1); // Delay abit or else the RS485 will switch off before the last byte has been sent.
digitalWrite (EnablePin, LOW); // disable sending
return true;
}
bool RS485_ReadMessage(AvailableCallback fAvailable, ReadCallback fRead, char data[maxMsgLen+3+1])
{
bool bSTX = false;
bool bETX = false;
byte index = 0;
int input = -1;
byte CRC=0;
if(fAvailable())
{
while(fAvailable())
{
input = fRead();
switch(input)
{
// Start of message
case STX :
bSTX = true;
index = 0;
break;
// End of message
case ETX :
bETX = true;
break;
default:
if(!bSTX)
// Don't read if the message has not started
break;
else
{
// While not the end of the message keep collection bytes
if(!bETX)
{
data[index] = input;
index++;
if(index >= maxMsgLen+3+1)
{
return false;
}
data[index] = '\0';
}
else
{
CRC = input;
}
}
}
delay(1); // Delay a bit or you will reach the end of the buffer before its even been filled.
}
if(CRC != crc8(data))
{
return false;
}
return true;
}
return false;
}
// For test the bytes comming in
bool RS485_ReadPlainMessage(AvailableCallback fAvailable, ReadCallback fRead, char message[maxMsgLen+3+1])
{
if(fAvailable())
{
int input = -1;
int counter=0;
while(fAvailable())
{
input = fRead();
message[counter] = input;
counter++;
message[counter] = '\0';
delay(1); // Delay a bit or you will reach the end of the buffer before its even been filled.
}
return true;
}
return false;
}