This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRS485Message.cpp
182 lines (144 loc) · 4.03 KB
/
RS485Message.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
/*
RS485 communicator synchronization library
Copyright (C) 2018 - Ivan Vaccari <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RS485Message.h"
RS485Message::RS485Message(){
clean();
}
bool RS485Message::setDestinationAddress(byte address){
// 255 is not a valid address. It's a control value
if (address==255)
return false;
buffer[0]=address;
return true;
}
byte RS485Message::getDestinationAddress(){
return buffer[0];
}
bool RS485Message::setSourceAddress(byte address){
// 254 cannot be a sender. It's the broadcast address
if (address==254)
return false;
// 255 is not a valid address. It's a control value
if (address==255)
return false;
buffer[1]=address;
return true;
}
byte RS485Message::getSourceAddress(){
return buffer[1];
}
bool RS485Message::setBytePayload(byte b, byte index){
if (index>=RS485_PAYLOAD_LENGTH){
return false;
}
byte tmp=b;
bitWrite(tmp,0,0);
buffer[3+index]=tmp;
byte fixByteIndex = floor(index/7);
byte fixBitIndex= index%7;
tmp=buffer[3+RS485_PAYLOAD_LENGTH+fixByteIndex];
bitWrite(tmp,fixBitIndex,bitRead(b,0));
buffer[3+RS485_PAYLOAD_LENGTH+fixByteIndex]=tmp;
return true;
}
bool RS485Message::setByteArrayPayload(byte bytes[], byte arraySize){
if (arraySize>RS485_PAYLOAD_LENGTH){
return false;
}
for( byte b=0; b<arraySize; b++){
setBytePayload(bytes[b],b);
}
for( byte b=0; b<RS485_PAYLOAD_LENGTH-arraySize; b++){
setBytePayload(0,arraySize+b);
}
return true;
}
bool RS485Message::setStringPayload(const String &string){
if (string.length()>RS485_PAYLOAD_LENGTH){
return false;
}
memset(buffer+3,0,RS485_PAYLOAD_LENGTH+RS485_PAYLOAD_FIX_BYTES);
for (byte b=0;b<string.length();b++){
setBytePayload(string[b],b);
}
return true;
}
bool RS485Message::getBytePayload(byte index, byte* value){
if (index>=RS485_PAYLOAD_LENGTH){
return false;
}
*value=buffer[3+index];
byte fixByteIndex = floor(index/7);
byte fixBitIndex= index%7;
bitWrite(*value,0,bitRead(buffer[3+RS485_PAYLOAD_LENGTH+fixByteIndex],fixBitIndex));
return true;
}
bool RS485Message::getStringPayload(String* value){
for (byte b=0;b<5+RS485_PAYLOAD_LENGTH;b++){
byte v;
if(getBytePayload(b,&v)){
if ( v != 0){
value->concat((char)v);
}
}
}
return true;
}
bool RS485Message::getArrayPayload(byte* value,byte *length){
return true;
}
void RS485Message::setMessageType(MessageType t){
byte tmp=0;
switch(t){
case MessageStandard: {
buffer[2] = tmp;
break;
}
case MessageToken: {
bitWrite(tmp,0,1);
buffer[2] = tmp;
break;
}
default:{
bitWrite(tmp,0,1);
bitWrite(tmp,1,1);
bitWrite(tmp,2,1);
buffer[2] = tmp;
break;
}
}
}
RS485Message::MessageType RS485Message::getMessageType(){
if ((buffer[2] & 0x05) == 0){
return MessageStandard;
}
if ((buffer[2] & 0x05) == 0x01){
return MessageToken;
}
return UnknownType;
}
void RS485Message::clean(){
bufferWrittenBytes=0;
memset(buffer,0,3+RS485_PAYLOAD_LENGTH+RS485_PAYLOAD_FIX_BYTES+1);
}
byte RS485Message::getReceivedCrcByte(){
return buffer[3+RS485_PAYLOAD_LENGTH+RS485_PAYLOAD_FIX_BYTES];
}
RS485Message& RS485Message::operator=( const RS485Message& other){
if (this != &other) {
memcpy(buffer,other.buffer,3+RS485_PAYLOAD_LENGTH+RS485_PAYLOAD_FIX_BYTES+1);
}
return *this;
}