Difference between revisions of "EPEVER 485"

From Public Wiki
Jump to navigation Jump to search
(Created page with "==Synopsis== Issues 485 request and display result ==Notes== ==Code== <syntaxhighlight lang="C++" line='line'> //#include <SoftwareSerial.h> //const int SSERIAL_RX_PIN = 10;...")
 
Line 5: Line 5:
 
==Code==
 
==Code==
 
<syntaxhighlight lang="C++" line='line'>
 
<syntaxhighlight lang="C++" line='line'>
 
 
//#include <SoftwareSerial.h>
 
//#include <SoftwareSerial.h>
 
//const int SSERIAL_RX_PIN = 10;  //Soft Serial Receive pin
 
//const int SSERIAL_RX_PIN = 10;  //Soft Serial Receive pin
Line 18: Line 17:
 
byte Response[BUFS];
 
byte Response[BUFS];
 
byte Response_size;
 
byte Response_size;
 
+
byte countdown = 2;
 
//===============================================================================
 
//===============================================================================
 
//  Initialization
 
//  Initialization
Line 29: Line 28:
 
   Serial.println();
 
   Serial.println();
  
 +
 +
}
 +
 +
 +
//===============================================================================
 +
//  Main
 +
//===============================================================================
 +
void loop()
 +
{
 +
 +
if(countdown>0)
 +
{
 
   Response_size = zerofour(Response, 0x3302, 0x0012);
 
   Response_size = zerofour(Response, 0x3302, 0x0012);
 
     if(Response_size==0) Serial.println("Received Error from 0x04");
 
     if(Response_size==0) Serial.println("Received Error from 0x04");
  
 +
  //Print result
 +
  for(byte i=0;i<Response_size;i++)
 +
  {
 +
    Serial.print("0x");
 +
    Serial.print(Response[i],HEX);
 +
    Serial.print(" ");
 +
  }
 +
  Serial.println("Finished 0x04");
 +
 +
  delay(1000);
 +
 
 +
  Response_size = zerotwo(Response, 0x2000, 0x0001);
 +
  if(Response_size==0) Serial.println("Received Error from 0x04");
 +
 
 
   //Print result
 
   //Print result
 
   for(byte i=0;i<Response_size;i++)
 
   for(byte i=0;i<Response_size;i++)
Line 40: Line 65:
 
   }
 
   }
 
   Serial.println();
 
   Serial.println();
 +
  delay(1000);
 +
  countdown--;
 +
}
 
}
 
}
  
 +
byte zerotwo(byte rs[], int start, int num)
 +
{
 +
  byte rq[8];  //Always 8 bytes
 +
  //int addr;
 +
  //int len;
 +
  short c; //CRC value
 +
  byte i, j,sz;  //Counters, size
 +
 
 +
  rq[0]=DEVICE; //Global device IO
 +
  rq[1]=0x02;
 +
  rq[3]=byte(start&0x00FF);
 +
  rq[2]=byte((start&0xFF00)>>8);
 +
  rq[5]=byte(num&0x00FF);
 +
  rq[4]=byte((num&0xFF00)>>8);
  
//===============================================================================
+
  c = CRC16(rq,6);
//  Main
+
  rq[7]=0x00FF & c;
//===============================================================================
+
  rq[6]=c>>8;
void loop()
 
{
 
  
 +
  for(i=0;i<8;i++)
 +
  {
 +
    Serial1.write(rq[i]);
 +
  }
 +
  sz = 5+rq[5];
 +
  if(sz>BUFS)
 +
  {
 +
    Serial.println("Buffer overflow prevented in 0x02.  Recompile with BUFS to a higher value");
 +
    return 0;
 +
  }
 +
  for(i=0;i<sz;)
 +
  {
 +
   
 +
    if (Serial1.available())            //Data from the Slave is available
 +
    {
 +
      rs[i]=Serial1.read();
 +
      i++;
 +
    }
 +
  }
 +
  c=CRC16(rs,sz-2);
 +
  if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
 +
  {
 +
    Serial.println("CRC error in 0x02");
 +
    return 0;
 +
  }
 +
  return sz;
 
}
 
}
  

Revision as of 23:53, 16 November 2020

Synopsis

Issues 485 request and display result

Notes

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 Response[BUFS];
 12 byte Response_size;
 13 byte countdown = 2;
 14 //===============================================================================
 15 //  Initialization
 16 //===============================================================================
 17 void setup()
 18 {
 19   Serial.begin(115200);           // Start the built-in serial port   
 20   Serial1.begin(115200);   // Start the RS485 soft serial port 
 21   delay(1000);
 22   Serial.println();
 23 
 24 
 25 }
 26 
 27 
 28 //===============================================================================
 29 //  Main
 30 //===============================================================================
 31 void loop() 
 32 {
 33 
 34 if(countdown>0)
 35 {
 36   Response_size = zerofour(Response, 0x3302, 0x0012);
 37     if(Response_size==0) Serial.println("Received Error from 0x04");
 38 
 39   //Print result
 40   for(byte i=0;i<Response_size;i++)
 41   {
 42     Serial.print("0x");
 43     Serial.print(Response[i],HEX);
 44     Serial.print(" ");
 45   }
 46   Serial.println("Finished 0x04");
 47 
 48   delay(1000);
 49   
 50   Response_size = zerotwo(Response, 0x2000, 0x0001);
 51   if(Response_size==0) Serial.println("Received Error from 0x04");
 52   
 53   //Print result
 54   for(byte i=0;i<Response_size;i++)
 55   {
 56     Serial.print("0x");
 57     Serial.print(Response[i],HEX);
 58     Serial.print(" ");
 59   }
 60   Serial.println();
 61   delay(1000);
 62   countdown--;
 63 }
 64 }
 65 
 66 byte zerotwo(byte rs[], int start, int num)
 67 {
 68   byte rq[8];  //Always 8 bytes
 69   //int addr;
 70   //int len;
 71   short c; //CRC value
 72   byte i, j,sz;  //Counters, size
 73   
 74   rq[0]=DEVICE; //Global device IO
 75   rq[1]=0x02;
 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(sz>BUFS)
 91   {
 92     Serial.println("Buffer overflow prevented in 0x02.  Recompile with BUFS to a higher value");
 93     return 0;
 94   }
 95   for(i=0;i<sz;)
 96   {
 97     
 98     if (Serial1.available())            //Data from the Slave is available
 99     {
100       rs[i]=Serial1.read();
101       i++;
102     }
103   }
104   c=CRC16(rs,sz-2);
105   if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
106   {
107     Serial.println("CRC error in 0x02");
108     return 0;
109   }
110   return sz;
111 }
112 
113 //Assigns rs[] array, returns size or zero for CRC error
114 //This does NOT detect broadcast CRC errors, or any exception
115 //handling at all
116 //
117 //     rs : Response[] array
118 //  start : Starting address
119 //    num : Number of registers requested
120 byte zerofour(byte rs[],int start, int num)
121 {
122   byte rq[8];  //Always 8 bytes
123   //int addr;
124   //int len;
125   short c; //CRC value
126   byte i, j,sz;  //Counters, size
127   
128   rq[0]=DEVICE; //Global device IO
129   rq[1]=0x04;
130   rq[3]=byte(start&0x00FF);
131   rq[2]=byte((start&0xFF00)>>8);
132   rq[5]=byte(num&0x00FF);
133   rq[4]=byte((num&0xFF00)>>8);
134 
135   c = CRC16(rq,6);
136   rq[7]=0x00FF & c;
137   rq[6]=c>>8;
138 
139   for(i=0;i<8;i++)
140   {
141     Serial1.write(rq[i]);
142   }
143   sz = 5+rq[5]*2;
144   if(sz>BUFS)
145   {
146     Serial.println("Buffer overflow prevented in 0x04.  Recompile with BUFS to a higher value");
147     return 0;
148   }
149   for(i=0;i<sz;)
150   {
151     
152     if (Serial1.available())            //Data from the Slave is available
153     {
154       rs[i]=Serial1.read();
155       i++;
156     }
157   }
158   c=CRC16(rs,sz-2);
159   if(c!=short((rs[sz-2]<<8 | rs[sz-1])))
160   {
161     Serial.println("CRC error in 0x04");
162     return 0;
163   }
164 
165   return sz;
166 }
167 
168 short CRC16(byte array[],byte s)
169 {
170   static const byte auchCRCHi[] = {
171         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
172         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
173         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
174         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
175         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
176         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
177         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
178         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
179         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
180         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
181         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
182         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
183         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
184         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
185         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
186         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
187         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
188         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
189         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
190         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
191         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
192         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
193         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
194         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
195         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
196         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
197         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
198         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
199         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
200         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
201         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
202         0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40} ; 
203 
204   static const byte auchCRCLo[] = {
205         0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2,
206         0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04,
207         0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
208         0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8,
209         0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
210         0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
211         0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6,
212         0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10,
213         0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
214         0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
215         0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE,
216         0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
217         0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA,
218         0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
219         0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
220         0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0,
221         0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62,
222         0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
223         0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE,
224         0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
225         0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
226         0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C,
227         0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76,
228         0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
229         0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
230         0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54,
231         0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
232         0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
233         0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A,
234         0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
235         0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86,
236         0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40} ;
237 
238   byte uchCRCHi = 0xFF;
239   byte uchCRCLo = 0xFF;
240   byte uIndex;
241 
242   while(s--)
243   {
244     uIndex = uchCRCHi ^ *array++ ;
245     uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
246     uchCRCLo = auchCRCLo[uIndex] ;
247   }
248   return short(uchCRCHi << 8 | uchCRCLo);
249 }