Difference between revisions of "EPEVER 485"
(→Code) |
(→Code: comments) |
||
Line 296: | Line 296: | ||
void loop() | void loop() | ||
{ | { | ||
− | + | int start_register = 0x903D //binary value of register starting location | |
− | Response_size = onezero(Response, | + | int num_registers = 0x0003 //Number of registers, only 16-bit for consistency |
+ | //0x007B is the maximum value | ||
+ | |||
+ | |||
+ | Response_size = onezero(Response, start_register, num_registers, arry, LOW); | ||
if(Response_size==0) Serial.println("Received Error from 0x01"); | if(Response_size==0) Serial.println("Received Error from 0x01"); | ||
Line 599: | Line 603: | ||
byte onezero(byte rs[],int start, int num, byte arr[], bool confirmation) | byte onezero(byte rs[],int start, int num, byte arr[], bool confirmation) | ||
{ | { | ||
− | byte rq[132]; // | + | byte rq[132]; //This is the size of the maximum possible buffer |
− | |||
− | |||
short c; //CRC value | short c; //CRC value | ||
byte i, j,sz; //Counters, size | byte i, j,sz; //Counters, size | ||
Line 615: | Line 617: | ||
rq[2]=byte((start&0xFF00)>>8); | rq[2]=byte((start&0xFF00)>>8); | ||
rq[5]=byte(num&0x00FF); | rq[5]=byte(num&0x00FF); | ||
− | rq[4]=byte((num&0xFF00)>>8); | + | rq[4]=byte((num&0xFF00)>>8); |
− | rq[6]=num<<1; | + | |
+ | rq[6]=num<<1; //num<<1 is same as num*2, but faster | ||
+ | //total number of 8-bit bytes | ||
+ | //Copies the array in parameter list into the request. | ||
+ | //From o to 2*num bytes | ||
for(int i=0;i<(num<<1);i++) | for(int i=0;i<(num<<1);i++) | ||
{ | { | ||
Line 623: | Line 629: | ||
} | } | ||
+ | //Calculates the CRC | ||
c = CRC16(rq,7+(num<<1)); | c = CRC16(rq,7+(num<<1)); | ||
− | rq[8+(num<<1)]=0x00FF & c; | + | //copies the CRC 16 bits into two bytes in the array using bit masking |
− | rq[7+(num<<1)]=c>>8; | + | //High byte is stored into [8+2*num] |
+ | //Low byte is stored into [7+2*num] | ||
+ | rq[8+(num<<1)] = 0x00FF & c; //Erase upper bits, copy only the lower 8 by bitwise AND | ||
+ | rq[7+(num<<1)] = c>>8; //right shift the upper 8 bits into the lower 8 bits, stored into array | ||
− | if(confirmation) | + | if(confirmation) //If true, issue the command |
{ | { | ||
for(i=0;i<9+(num<<1);i++) | for(i=0;i<9+(num<<1);i++) | ||
Line 634: | Line 644: | ||
} | } | ||
} | } | ||
− | else | + | else //else do nothing but give the coder access to the Request. |
{ | { | ||
for(i=0;i<9+(num<<1);i++) | for(i=0;i<9+(num<<1);i++) | ||
{ | { | ||
− | rs[i]=rq[i]; | + | rs[i]=rq[i]; //Request is copied into Response |
} | } | ||
} | } | ||
− | sz = 9+(num<<1); | + | sz = 9+(num<<1); //Size of the Request |
if(sz>BUFS) | if(sz>BUFS) | ||
{ | { | ||
Line 647: | Line 657: | ||
return 0; | return 0; | ||
} | } | ||
− | if(confirmation) | + | if(confirmation) //We are writing to the device |
{ | { | ||
− | sz=8; | + | sz=8; //The Response of 0x10 is always 8 bytes (64 bits long) |
− | for(i=0;i< | + | for(i=0;i<sz;) //always 8 |
{ | { | ||
Line 659: | Line 669: | ||
} | } | ||
} | } | ||
− | c=CRC16(rs,sz-2); | + | //Test the CRC values of response on the buffer (sparing the given CRC codes) |
− | if(c!=short((rs[sz-2]<<8 | rs[sz-1]))) | + | c=CRC16(rs,sz-2); //Test everything except last two bytes. |
+ | if(c!=short((rs[sz-2]<<8 | rs[sz-1]))) //If either byte of CRC is wrong, then error | ||
{ | { | ||
Serial.println("CRC error in 0x04"); | Serial.println("CRC error in 0x04"); |
Revision as of 00:17, 18 November 2020
Contents
Synopsis
Issues 485 request and display result
Notes
9000-9100 Registers
0xFFFF9000 : 0x00 0xFFFF9001 : 0x70 0xFFFF9002 : 0x00 0xFFFF9003 : 0x86 0xFFFF9004 : 0x68 0xFFFF9005 : 0x5E 0xFFFF9006 : 0x54 0xFFFF9007 : 0x54 0xFFFF9008 : 0xC8 0xFFFF9009 : 0x50 0xFFFF900A : 0x00 0xFFFF900B : 0x00 0xFFFF900C : 0x60 0xFFFF900D : 0xAC 0xFFFF900E : 0x48 0xFFFF900F : 0x00 0xFFFF9010 : 0x64 0xFFFF9011 : 0x00 0xFFFF9012 : Error 0x02 0xFFFF9013 : 0x27 0xFFFF9014 : 0x15 0xFFFF9015 : 0x0B 0xFFFF9016 : 0x1E 0xFFFF9017 : 0x4C 0xFFFF9018 : 0x00 0xFFFF9019 : 0x1C 0xFFFF901A : 0x40 0xFFFF901B : 0x34 0xFFFF901C : 0x4C 0xFFFF901D : 0x02 0xFFFF901E : 0xE8 0xFFFF901F : 0x0A 0xFFFF9020 : 0xB0 0xFFFF9021 : 0x0A 0xFFFF9022 : Error 0x02 0xFFFF9023 : Error 0x02 0xFFFF9024 : Error 0x02 0xFFFF9025 : Error 0x02 0xFFFF9026 : Error 0x02 0xFFFF9027 : Error 0x02 0xFFFF9028 : Error 0x02 0xFFFF9029 : Error 0x02 0xFFFF902A : Error 0x02 0xFFFF902B : Error 0x02 0xFFFF902C : Error 0x02 0xFFFF902D : Error 0x02 0xFFFF902E : Error 0x02 0xFFFF902F : Error 0x02 0xFFFF9030 : Error 0x02 0xFFFF9031 : Error 0x02 0xFFFF9032 : Error 0x02 0xFFFF9033 : Error 0x02 0xFFFF9034 : Error 0x02 0xFFFF9035 : Error 0x02 0xFFFF9036 : Error 0x02 0xFFFF9037 : Error 0x02 0xFFFF9038 : Error 0x02 0xFFFF9039 : Error 0x02 0xFFFF903A : Error 0x02 0xFFFF903B : Error 0x02 0xFFFF903C : Error 0x02 0xFFFF903D : 0x00 0xFFFF903E : 0x00 0xFFFF903F : 0x00 0xFFFF9040 : Error 0x02 0xFFFF9041 : Error 0x02 0xFFFF9042 : 0x00 0xFFFF9043 : 0x00 0xFFFF9044 : 0x13 0xFFFF9045 : 0x00 0xFFFF9046 : 0x00 0xFFFF9047 : 0x06 0xFFFF9048 : 0x00 0xFFFF9049 : 0x00 0xFFFF904A : 0x13 0xFFFF904B : 0x00 0xFFFF904C : 0x00 0xFFFF904D : 0x06 0xFFFF904E : Error 0x02 0xFFFF904F : Error 0x02 0xFFFF9050 : Error 0x02 0xFFFF9051 : Error 0x02 0xFFFF9052 : Error 0x02 0xFFFF9053 : Error 0x02 0xFFFF9054 : Error 0x02 0xFFFF9055 : Error 0x02 0xFFFF9056 : Error 0x02 0xFFFF9057 : Error 0x02 0xFFFF9058 : Error 0x02 0xFFFF9059 : Error 0x02 0xFFFF905A : Error 0x02 0xFFFF905B : Error 0x02 0xFFFF905C : Error 0x02 0xFFFF905D : Error 0x02 0xFFFF905E : Error 0x02 0xFFFF905F : Error 0x02 0xFFFF9060 : Error 0x02 0xFFFF9061 : Error 0x02 0xFFFF9062 : Error 0x02 0xFFFF9063 : 0x1E 0xFFFF9064 : 0x02 0xFFFF9065 : 0x00 0xFFFF9066 : Error 0x02 0xFFFF9067 : 0x02 0xFFFF9068 : Error 0x02 0xFFFF9069 : 0x00 0xFFFF906A : 0x00 0xFFFF906B : 0x78 0xFFFF906C : 0x50 0xFFFF906D : 0x1E 0xFFFF906E : 0x50 0xFFFF906F : 0x28 0xFFFF9070 : 0x00 0xFFFF9071 : Error 0x02 0xFFFF9072 : Error 0x02 0xFFFF9073 : Error 0x02 0xFFFF9074 : Error 0x02 0xFFFF9075 : Error 0x02 0xFFFF9076 : Error 0x02 0xFFFF9077 : Error 0x02 0xFFFF9078 : Error 0x02 0xFFFF9079 : Error 0x02 0xFFFF907A : Error 0x02 0xFFFF907B : Error 0x02 0xFFFF907C : Error 0x02 0xFFFF907D : Error 0x02 0xFFFF907E : Error 0x02 0xFFFF907F : Error 0x02 0xFFFF9080 : Error 0x02 0xFFFF9081 : Error 0x02 0xFFFF9082 : Error 0x02 0xFFFF9083 : Error 0x02 0xFFFF9084 : Error 0x02 0xFFFF9085 : Error 0x02 0xFFFF9086 : Error 0x02 0xFFFF9087 : Error 0x02 0xFFFF9088 : Error 0x02 0xFFFF9089 : Error 0x02 0xFFFF908A : Error 0x02 0xFFFF908B : Error 0x02 0xFFFF908C : Error 0x02 0xFFFF908D : Error 0x02 0xFFFF908E : Error 0x02 0xFFFF908F : Error 0x02 0xFFFF9090 : 0x00 0xFFFF9091 : 0x00 0xFFFF9092 : 0x00 0xFFFF9093 : 0x00 0xFFFF9094 : 0x57 0xFFFF9095 : 0x00 0xFFFF9096 : 0x00 0xFFFF9097 : 0x38 0xFFFF9098 : 0xEE 0xFFFF9099 : Error 0x02 0xFFFF909A : Error 0x02 0xFFFF909B : Error 0x02 0xFFFF909C : Error 0x02 0xFFFF909D : Error 0x02 0xFFFF909E : Error 0x02 0xFFFF909F : Error 0x02 0xFFFF90A0 : 0x00 0xFFFF90A1 : 0x00 0xFFFF90A2 : 0x00 0xFFFF90A3 : 0x00 0xFFFF90A4 : 0x00 0xFFFF90A5 : Error 0x02 0xFFFF90A6 : Error 0x02 0xFFFF90A7 : Error 0x02 0xFFFF90A8 : Error 0x02 0xFFFF90A9 : Error 0x02 0xFFFF90AA : Error 0x02 0xFFFF90AB : Error 0x02 0xFFFF90AC : Error 0x02 0xFFFF90AD : Error 0x02 0xFFFF90AE : Error 0x02 0xFFFF90AF : Error 0x02 0xFFFF90B0 : 0xD2 0xFFFF90B1 : 0x60 0xFFFF90B2 : 0x13 0xFFFF90B3 : 0x7A 0xFFFF90B4 : 0x00 0xFFFF90B5 : 0xA7 0xFFFF90B6 : 0x89 0xFFFF90B7 : 0x35 0xFFFF90B8 : 0x75 0xFFFF90B9 : 0x87 0xFFFF90BA : 0x00 0xFFFF90BB : 0x00 0xFFFF90BC : Error 0x02 0xFFFF90BD : 0x00 0xFFFF90BE : 0x00 0xFFFF90BF : 0xE0 0xFFFF90C0 : Error 0x02 0xFFFF90C1 : Error 0x02 0xFFFF90C2 : Error 0x02 0xFFFF90C3 : Error 0x02 0xFFFF90C4 : Error 0x02 0xFFFF90C5 : Error 0x02 0xFFFF90C6 : Error 0x02 0xFFFF90C7 : Error 0x02 0xFFFF90C8 : Error 0x02 0xFFFF90C9 : Error 0x02 0xFFFF90CA : Error 0x02 0xFFFF90CB : Error 0x02 0xFFFF90CC : Error 0x02 0xFFFF90CD : Error 0x02 0xFFFF90CE : Error 0x02 0xFFFF90CF : Error 0x02 0xFFFF90D0 : Error 0x02 0xFFFF90D1 : Error 0x02 0xFFFF90D2 : Error 0x02 0xFFFF90D3 : Error 0x02 0xFFFF90D4 : Error 0x02 0xFFFF90D5 : Error 0x02 0xFFFF90D6 : Error 0x02 0xFFFF90D7 : Error 0x02 0xFFFF90D8 : Error 0x02 0xFFFF90D9 : Error 0x02 0xFFFF90DA : Error 0x02 0xFFFF90DB : Error 0x02 0xFFFF90DC : Error 0x02 0xFFFF90DD : Error 0x02 0xFFFF90DE : Error 0x02 0xFFFF90DF : Error 0x02 0xFFFF90E0 : Error 0x02 0xFFFF90E1 : Error 0x02 0xFFFF90E2 : Error 0x02 0xFFFF90E3 : Error 0x02 0xFFFF90E4 : Error 0x02 0xFFFF90E5 : Error 0x02 0xFFFF90E6 : Error 0x02 0xFFFF90E7 : Error 0x02 0xFFFF90E8 : Error 0x02 0xFFFF90E9 : Error 0x02 0xFFFF90EA : Error 0x02 0xFFFF90EB : Error 0x02 0xFFFF90EC : Error 0x02 0xFFFF90ED : Error 0x02 0xFFFF90EE : Error 0x02 0xFFFF90EF : Error 0x02 0xFFFF90F0 : Error 0x02 0xFFFF90F1 : Error 0x02 0xFFFF90F2 : Error 0x02 0xFFFF90F3 : Error 0x02 0xFFFF90F4 : Error 0x02 0xFFFF90F5 : Error 0x02 0xFFFF90F6 : Error 0x02 0xFFFF90F7 : Error 0x02 0xFFFF90F8 : Error 0x02 0xFFFF90F9 : Error 0x02 0xFFFF90FA : Error 0x02 0xFFFF90FB : Error 0x02 0xFFFF90FC : Error 0x02 0xFFFF90FD : Error 0x02 0xFFFF90FE : Error 0x02 0xFFFF90FF : Error 0x02
Code
1 //#include <SoftwareSerial.h>
2 //const int SSERIAL_RX_PIN = 10; //Soft Serial Receive pin
3 //const int SSERIAL_TX_PIN = 11; //Soft Serial Transmit pin
4
5 // Create Soft Serial Port object and define pins to use
6 //SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN); // RX, TX
7
8 #define DEVICE 0x01
9 #define BUFS 64 //Buffer size
10 short crc;
11 byte arry[6] = {0x00, 0x02, 0x02, 0x00, 0x02, 0x00};
12 byte Response[BUFS];
13 byte Response_size;
14 byte countdown = 2;
15 //===============================================================================
16 // Initialization
17 //===============================================================================
18 void setup()
19 {
20 Serial.begin(115200); // Start the built-in serial port
21 Serial1.begin(115200); // Start the RS485 soft serial port
22 delay(1000);
23 Serial.println();
24
25
26 }
27
28
29 //===============================================================================
30 // Main
31 //===============================================================================
32 void loop()
33 {
34 int start_register = 0x903D //binary value of register starting location
35 int num_registers = 0x0003 //Number of registers, only 16-bit for consistency
36 //0x007B is the maximum value
37
38
39 Response_size = onezero(Response, start_register, num_registers, arry, LOW);
40 if(Response_size==0) Serial.println("Received Error from 0x01");
41
42 Serial.print("Size is : ");
43 Serial.println(Response_size);
44
45 Serial.print("Response string is : ");
46 for(byte i=0;i<Response_size;i++)
47 {
48 Serial.print("0x");
49 if(Response[i]<16) Serial.print("0");
50 Serial.print(Response[i],HEX);
51 Serial.print(" ");
52 }
53 Serial.println();
54
55 while(1)
56 {}
57 }
58
59 byte zeroone(byte rs[],int start, int num)
60 {
61 byte rq[8]; //Always 8 bytes
62 //int addr;
63 //int len;
64 short c; //CRC value
65 byte i, j,sz; //Counters, size
66
67 if(num==0 || num>0x7d0)
68 {
69 Serial.println("Illegal quantity of inputs");
70 return 0;
71 }
72
73
74 rq[0]=DEVICE; //Global device IO
75 rq[1]=0x01;
76 rq[3]=byte(start&0x00FF);
77 rq[2]=byte((start&0xFF00)>>8);
78 rq[5]=byte(num&0x00FF);
79 rq[4]=byte((num&0xFF00)>>8);
80
81 c = CRC16(rq,6);
82 rq[7]=0x00FF & c;
83 rq[6]=c>>8;
84
85 for(i=0;i<8;i++)
86 {
87 Serial1.write(rq[i]);
88 }
89 sz = 5+rq[5];
90 //if((rq[5]%8)!=0 && rq[5]>8) sz++; //Refer to *N at top of Page 12
91 if(sz>BUFS)
92 {
93 Serial.println("Buffer overflow prevented in 0x01. Recompile with BUFS to a higher value");
94 return 0;
95 }
96 for(i=0;i<sz;)
97 {
98 if (Serial1.available()) //Data from the Slave is available
99 {
100 rs[i]=Serial1.read();
101 if(i==1 && rs[i]==0x83) sz=5;
102 i++;
103 }
104
105 }
106 c=CRC16(rs,sz-2);
107 if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
108 {
109 Serial.println("CRC error in 0x01");
110 return 0;
111 }
112
113 return sz;
114 }
115
116
117 byte zerotwo(byte rs[], int start, int num)
118 {
119 byte rq[8]; //Always 8 bytes
120 //int addr;
121 //int len;
122 short c; //CRC value
123 byte i, j,sz; //Counters, size
124
125 rq[0]=DEVICE; //Global device IO
126 rq[1]=0x02;
127 rq[3]=byte(start&0x00FF);
128 rq[2]=byte((start&0xFF00)>>8);
129 rq[5]=byte(num&0x00FF);
130 rq[4]=byte((num&0xFF00)>>8);
131
132 c = CRC16(rq,6);
133 rq[7]=0x00FF & c;
134 rq[6]=c>>8;
135
136 for(i=0;i<8;i++)
137 {
138 Serial1.write(rq[i]);
139 }
140 sz = 5+rq[5];
141 if(sz>BUFS)
142 {
143 Serial.println("Buffer overflow prevented in 0x02. Recompile with BUFS to a higher value");
144 return 0;
145 }
146 for(i=0;i<sz;)
147 {
148
149 if (Serial1.available()) //Data from the Slave is available
150 {
151 rs[i]=Serial1.read();
152 i++;
153 }
154 }
155 c=CRC16(rs,sz-2);
156 if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
157 {
158 Serial.println("CRC error in 0x02");
159 return 0;
160 }
161 return sz;
162 }
163
164 byte zerothree(byte rs[],int start, int num)
165 {
166 byte rq[8]; //Always 8 bytes
167 //int addr;
168 //int len;
169 short c; //CRC value
170 byte i, j,sz; //Counters, size
171
172 if(num==0 || num>0x7d)
173 {
174 Serial.println("Illegal quantity of inputs");
175 return 0;
176 }
177
178
179 rq[0]=DEVICE; //Global device IO
180 rq[1]=0x03;
181 rq[3]=byte(start&0x00FF);
182 rq[2]=byte((start&0xFF00)>>8);
183 rq[5]=byte(num&0x00FF);
184 rq[4]=byte((num&0xFF00)>>8);
185
186 c = CRC16(rq,6);
187 rq[7]=0x00FF & c;
188 rq[6]=c>>8;
189
190 for(i=0;i<8;i++)
191 {
192 Serial1.write(rq[i]);
193 }
194 sz = 5+rq[5]*2;
195 if(sz>BUFS)
196 {
197 Serial.println("Buffer overflow prevented in 0x03. Recompile with BUFS to a higher value");
198 return 0;
199 }
200 for(i=0;i<sz;)
201 {
202 if (Serial1.available()) //Data from the Slave is available
203 {
204 rs[i]=Serial1.read();
205 if(i==1 && rs[i]==0x83) sz=5;
206 i++;
207 }
208
209 }
210 c=CRC16(rs,sz-2);
211 if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
212 {
213 Serial.println("CRC error in 0x03");
214 return 0;
215 }
216
217 return sz;
218 }
219
220
221
222 //Assigns rs[] array, returns size or zero for CRC error
223 //This does NOT detect broadcast CRC errors, or any exception
224 //handling at all
225 //
226 // rs : Response[] array
227 // start : Starting address
228 // num : Number of registers requested
229 byte zerofour(byte rs[],int start, int num)
230 {
231 byte rq[8]; //Always 8 bytes
232 //int addr;
233 //int len;
234 short c; //CRC value
235 byte i, j,sz; //Counters, size
236
237 rq[0]=DEVICE; //Global device IO
238 rq[1]=0x04;
239 rq[3]=byte(start&0x00FF);
240 rq[2]=byte((start&0xFF00)>>8);
241 rq[5]=byte(num&0x00FF);
242 rq[4]=byte((num&0xFF00)>>8);
243
244 c = CRC16(rq,6);
245 rq[7]=0x00FF & c;
246 rq[6]=c>>8;
247
248 for(i=0;i<8;i++)
249 {
250 Serial1.write(rq[i]);
251 }
252 sz = 5+rq[5]*2;
253 if(sz>BUFS)
254 {
255 Serial.println("Buffer overflow prevented in 0x04. Recompile with BUFS to a higher value");
256 return 0;
257 }
258 for(i=0;i<sz;)
259 {
260
261 if (Serial1.available()) //Data from the Slave is available
262 {
263 rs[i]=Serial1.read();
264 i++;
265 }
266 }
267 c=CRC16(rs,sz-2);
268 if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
269 {
270 Serial.println("CRC error in 0x04");
271 return 0;
272 }
273
274 return sz;
275 }
276 ///////////////
277 byte zerofive(byte rs[],int addr, bool set, bool confirmation)
278 {
279 byte rq[8]; //Always 8 bytes
280 //int addr;
281 //int len;
282 short c; //CRC value
283 byte i, j,sz; //Counters, size
284
285
286 rq[0]=DEVICE; //Global device IO
287 rq[1]=0x05;
288 rq[3]=byte(addr&0x00FF);
289 rq[2]=byte((addr&0xFF00)>>8);
290 rq[4]=0x00;
291 if(set) rq[5]=0xFF;
292 else rq[5]=0x00;
293
294
295 c = CRC16(rq,6);
296 rq[7]=0x00FF & c;
297 rq[6]=c>>8;
298 if(confirmation)
299 {
300 for(i=0;i<8;i++)
301 {
302 Serial1.write(rq[i]);
303 }
304 }
305 sz = 6;
306 if(sz>BUFS)
307 {
308 Serial.println("Buffer overflow prevented in 0x03. Recompile with BUFS to a higher value");
309 return 0;
310 }
311 if(confirmation)
312 {
313 for(i=0;i<sz;)
314 {
315 if (Serial1.available()) //Data from the Slave is available
316 {
317 rs[i]=Serial1.read();
318 if(i==1 && rs[i]==0x83) sz=5;
319 i++;
320 }
321 }
322 }
323 else
324 {
325 for(i=0;i<sz;)
326 {
327 rs[i]=rq[i];
328 i++;
329 }
330 }
331 c=CRC16(rs,sz-2);
332 if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
333 {
334 Serial.println("CRC error in 0x03");
335 return 0;
336 }
337 }
338 ///////////////
339 byte onezero(byte rs[],int start, int num, byte arr[], bool confirmation)
340 {
341 byte rq[132]; //This is the size of the maximum possible buffer
342 short c; //CRC value
343 byte i, j,sz; //Counters, size
344
345 if(num==0 || num>0x7B)
346 {
347 Serial.println("Invalid number of registers being written in 0x10.");
348 return 0;
349 }
350 rq[0]=DEVICE; //Global device IO
351 rq[1]=0x10;
352 rq[3]=byte(start&0x00FF);
353 rq[2]=byte((start&0xFF00)>>8);
354 rq[5]=byte(num&0x00FF);
355 rq[4]=byte((num&0xFF00)>>8);
356
357 rq[6]=num<<1; //num<<1 is same as num*2, but faster
358 //total number of 8-bit bytes
359
360 //Copies the array in parameter list into the request.
361 //From o to 2*num bytes
362 for(int i=0;i<(num<<1);i++)
363 {
364 rq[7+i]=arr[i];
365 }
366
367 //Calculates the CRC
368 c = CRC16(rq,7+(num<<1));
369 //copies the CRC 16 bits into two bytes in the array using bit masking
370 //High byte is stored into [8+2*num]
371 //Low byte is stored into [7+2*num]
372 rq[8+(num<<1)] = 0x00FF & c; //Erase upper bits, copy only the lower 8 by bitwise AND
373 rq[7+(num<<1)] = c>>8; //right shift the upper 8 bits into the lower 8 bits, stored into array
374
375 if(confirmation) //If true, issue the command
376 {
377 for(i=0;i<9+(num<<1);i++)
378 {
379 Serial1.write(rq[i]);
380 }
381 }
382 else //else do nothing but give the coder access to the Request.
383 {
384 for(i=0;i<9+(num<<1);i++)
385 {
386 rs[i]=rq[i]; //Request is copied into Response
387 }
388 }
389 sz = 9+(num<<1); //Size of the Request
390 if(sz>BUFS)
391 {
392 Serial.println("Buffer overflow prevented in 0x04. Recompile with BUFS to a higher value");
393 return 0;
394 }
395 if(confirmation) //We are writing to the device
396 {
397 sz=8; //The Response of 0x10 is always 8 bytes (64 bits long)
398 for(i=0;i<sz;) //always 8
399 {
400
401 if (Serial1.available()) //Data from the Slave is available
402 {
403 rs[i]=Serial1.read();
404 i++;
405 }
406 }
407 //Test the CRC values of response on the buffer (sparing the given CRC codes)
408 c=CRC16(rs,sz-2); //Test everything except last two bytes.
409 if(c!=short((rs[sz-2]<<8 | rs[sz-1]))) //If either byte of CRC is wrong, then error
410 {
411 Serial.println("CRC error in 0x04");
412 return 0;
413 }
414 }
415 return sz;
416 }
417
418
419 short CRC16(byte array[],byte s)
420 {
421 static const byte auchCRCHi[] = {
422 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
423 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
424 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
425 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
426 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
427 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
428 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
429 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
430 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
431 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
432 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
433 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
434 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
435 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
436 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
437 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
438 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
439 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
440 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
441 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
442 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
443 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
444 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
445 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
446 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
447 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
448 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
449 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
450 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
451 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
452 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
453 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40} ;
454
455 static const byte auchCRCLo[] = {
456 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2,
457 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04,
458 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
459 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8,
460 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
461 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
462 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6,
463 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10,
464 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
465 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
466 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE,
467 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
468 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA,
469 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
470 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
471 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0,
472 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62,
473 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
474 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE,
475 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
476 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
477 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C,
478 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76,
479 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
480 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
481 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54,
482 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
483 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
484 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A,
485 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
486 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86,
487 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40} ;
488
489 byte uchCRCHi = 0xFF;
490 byte uchCRCLo = 0xFF;
491 byte uIndex;
492
493 while(s--)
494 {
495 uIndex = uchCRCHi ^ *array++ ;
496 uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
497 uchCRCLo = auchCRCLo[uIndex] ;
498 }
499 return short(uchCRCHi << 8 | uchCRCLo);
500 }