UNO + W5500 + Ethernet2 зависает при отключение интернет шнурка

int2246
Offline
Зарегистрирован: 25.01.2017

Доброго времени суток. Столкнулся с такой проблемой:

имеется UNO в которую вставлен w5500 ethernet shield

задача ардуино вести подсчет импульсов и передавайть подсчитанное число через modbus TCP slave на OPC сервер.

с этой задачей она справляется на ура. проблема возникает при отключение провода интернет или присто если пропадет линк на W5500 ардуина зависает, причем зависает именно она посколько W5500 отвечает на ping.

помагите может у кого было чего то подобное и как с этим боротца?

P.S. дописал код чтобы отпрашивать ком порт чтобы выяслить что виснит именно ардуино и получилось так:

отключаешь интернет кабель зависает, включаешь монитор порта начианется опрос и она отвисает.

#include <Ethernet2.h>
#include <SPI.h>
#include <EEPROM.h>
struct _savTWstr {uint16_t w1; uint16_t w2;};
_savTWstr _savTWstrTemp;
int _modbusSlaveDataTable_4[29];
int _modbusSlaveAddresTable_4[29] = {0, 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};
byte _modbusSlaveBufferSize = 0;
byte _modbusSlaveBuffer[64];
const unsigned char _modbusSlave_fctsupported[] = {3, 6, 16};
EthernetServer _modbusSlaveTCPServer(808);
EthernetClient _modbusSlaveTCPClient;
byte _modbusSlaveMBAPBuffer[6];
byte ethernet_mac [] = {0x78, 0x78, 0x78, 0x78, 0x78, 0x20};
IPAddress ethernet_ip(192, 168, 12, 20);
byte ethernet_dns [] = {192, 168, 12, 1};
byte ethernet_gateway [] = {192, 168, 12, 1};
byte ethernet_subnet [] = {255, 255, 255, 0};
bool _ByteToBit_1_outArray[8];
bool _ByteToBit_2_outArray[8];
bool _ByteToBit_3_outArray[8];
bool _ByteToBit_4_outArray[8];
bool _ByteToBit_5_outArray[8];
bool _ByteToBit_6_outArray[8];
int _PWDC = 0;
bool _count1I = 0;
unsigned long _count1P = 0UL;
bool _SEEPR4OSN = 0;
bool _tim4I = 0;
bool _tim4O = 0;
unsigned long _tim4P = 0UL;
float _swi2;
float _swi3;
bool _trgrt3 = 0;
bool _trgrt3I = 0;
bool _trgrt5 = 0;
bool _trgrt5I = 0;
bool _SEEPR1OSN = 0;
bool _trgrt17 = 0;
bool _trgrt17I = 0;
bool _bounseInputD5S = 0;
bool _bounseInputD5O = 0;
unsigned long _bounseInputD5P = 0UL;
bool _bounseInputD6S = 0;
bool _bounseInputD6O = 0;
unsigned long _bounseInputD6P = 0UL;
bool _count2I = 0;
unsigned long _count2P = 0UL;
bool _trgrt13 = 0;
bool _trgrt13I = 0;
bool _trgrt16 = 0;
bool _trgrt16I = 0;
bool _trgrt6 = 0;
bool _trgrt6I = 0;
bool _tim10I = 0;
bool _tim10O = 0;
unsigned long _tim10P = 0UL;
bool _trgrt9 = 0;
bool _trgrt9I = 0;
float _swi1;
bool _trgrt10 = 0;
bool _trgrt10I = 0;
bool _tim8I = 0;
bool _tim8O = 0;
unsigned long _tim8P = 0UL;
bool _tim2I = 0;
bool _tim2O = 0;
unsigned long _tim2P = 0UL;
bool _SEEPR5OSN = 0;
bool _SEEPR3OSN = 0;
bool _trgrt2 = 0;
bool _trgrt2I = 0;
bool _tim17I = 0;
bool _tim17O = 0;
unsigned long _tim17P = 0UL;
bool _trgrt1 = 0;
bool _trgrt1I = 0;
float _swi5;
float _swi6;
bool _trgrt14 = 0;
bool _trgrt14I = 0;
bool _trgrt8 = 0;
bool _trgrt8I = 0;
bool _tim1I = 0;
bool _tim1O = 0;
unsigned long _tim1P = 0UL;
float _swi4;
bool _trgrt4 = 0;
bool _trgrt4I = 0;
bool _count5I = 0;
unsigned long _count5P = 0UL;
bool _trgrt7 = 0;
bool _trgrt7I = 0;
bool _count6I = 0;
unsigned long _count6P = 0UL;
bool _trgrt11 = 0;
bool _trgrt11I = 0;
bool _count3I = 0;
unsigned long _count3P = 0UL;
bool _tim16I = 0;
bool _tim16O = 0;
unsigned long _tim16P = 0UL;
bool _count4I = 0;
unsigned long _count4P = 0UL;
bool _tim18I = 0;
bool _tim18O = 0;
unsigned long _tim18P = 0UL;
bool _tim14I = 0;
bool _tim14O = 0;
unsigned long _tim14P = 0UL;
bool _SEEPR2OSN = 0;
bool _trgrt15 = 0;
bool _trgrt15I = 0;
bool _tim15I = 0;
bool _tim15O = 0;
unsigned long _tim15P = 0UL;
bool _tim12I = 0;
bool _tim12O = 0;
unsigned long _tim12P = 0UL;
bool _trgrt18 = 0;
bool _trgrt18I = 0;
bool _tim13I = 0;
bool _tim13O = 0;
unsigned long _tim13P = 0UL;
bool _tim9I = 0;
bool _tim9O = 0;
unsigned long _tim9P = 0UL;
bool _tim5I = 0;
bool _tim5O = 0;
unsigned long _tim5P = 0UL;
bool _tim3I = 0;
bool _tim3O = 0;
unsigned long _tim3P = 0UL;
bool _SEEPR6OSN = 0;
bool _tim11I = 0;
bool _tim11O = 0;
unsigned long _tim11P = 0UL;
bool _tim6I = 0;
bool _tim6O = 0;
unsigned long _tim6P = 0UL;
bool _tim7I = 0;
bool _tim7O = 0;
unsigned long _tim7P = 0UL;
bool _trgrt12 = 0;
bool _trgrt12I = 0;
void setup()
{
  Ethernet.begin(ethernet_mac, ethernet_ip, ethernet_dns, ethernet_gateway, ethernet_subnet);
delay(1000);
_modbusSlaveTCPServer.begin();
TCCR2A = 0x00;
TCCR2B =  0x07;
TIMSK2=0x01;
TCNT2=100;
pinMode(5, INPUT_PULLUP); 
pinMode(6, INPUT_PULLUP); 

_bounseInputD5O =  digitalRead(5);
_bounseInputD6O =  digitalRead(6);
}
void loop()
{byte _tempVariable_byte;
float _tempVariable_float;
_modbusSlavePoll();
_PWDC = 0;

bool  _bounceInputTmpD5 =  (digitalRead (5));

if (_bounseInputD5S) 
    {
     if (millis() >= (_bounseInputD5P + 40)) 
         {_bounseInputD5O= _bounceInputTmpD5; _bounseInputD5S=0;}
     }
else
    {
     if (_bounceInputTmpD5 != _bounseInputD5O )
         {_bounseInputD5S=1; _bounseInputD5P = millis();} 
      } 
bool  _bounceInputTmpD6 =  (digitalRead (6));

if (_bounseInputD6S) 
    {
     if (millis() >= (_bounseInputD6P + 40)) 
         {_bounseInputD6O= _bounceInputTmpD6; _bounseInputD6S=0;}
     }
else
    {
     if (_bounceInputTmpD6 != _bounseInputD6O )
         {_bounseInputD6S=1; _bounseInputD6P = millis();} 
      } 




//Плата:1
//Наименование:фильтр 1
if (1) { if (_trgrt1I) { _trgrt1 = 0;} else {_trgrt1 = 1; _trgrt1I = 1;} } else {_trgrt1 = 0; _trgrt1I = 0;}; 
 _tempVariable_byte = (_modbusSlaveDataTable_4[24]);
for (int i=0; i < 8; i++){ _ByteToBit_1_outArray[i] = bitRead( _tempVariable_byte, i);}
if (_ByteToBit_1_outArray[0]) { if (_trgrt2I) { _trgrt2 = 0;} else {_trgrt2 = 1; _trgrt2I = 1;} } else {_trgrt2 = 0; _trgrt2I = 0;}; 
 _tempVariable_float = (readFloatFromEEPROM(0, 0, 0x0));
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[25] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[26] = _savTWstrTemp.w2;

if(_trgrt2)
{_swi1=(float(_readFloatFromModbus(_modbusSlaveDataTable_4[2], _modbusSlaveDataTable_4[3], 1)));}
else
{_swi1=(readFloatFromEEPROM(0, 0, 0x0));}
if(( (_trgrt1) || (_trgrt2) )) _count1P = ((_swi1)/(0.006666));
if (!(_bounseInputD5O)) 
   { 
   if (! _count1I)  
      {
       _count1P = _count1P+1;
       _count1I = 1;
      }
   }
else
   {
   _count1I=0;
   }
if (!(_bounseInputD5O))
{ if (_tim8I) { if (_isTimer(_tim8P, 20000)) {_tim8O = 1;}} else {_tim8I =1; _tim8P = millis();}} else {_tim8O = 0; _tim8I = 0;}
if (_bounseInputD5O)
{ if (_tim7I) { if (_isTimer(_tim7P, 20000)) {_tim7O = 1;}} else {_tim7I =1; _tim7P = millis();}} else {_tim7O = 0; _tim7I = 0;}
if (( (!(((readFloatFromEEPROM(0, 0, 0x0))) == ((_count1P)*(0.006666)))) && (( (_tim7O) || (_tim8O) )) ))
{ if (_tim1I) { if (_isTimer(_tim1P, 10750000)) {_tim1O = 1;}} else {_tim1I =1; _tim1P = millis();}} else {_tim1O = 0; _tim1I = 0;}
if (_tim1O) { if (_trgrt3I) { _trgrt3 = 0;} else {_trgrt3 = 1; _trgrt3I = 1;} } else {_trgrt3 = 0; _trgrt3I = 0;}; 
 if(( (_trgrt2) || (_trgrt3) )){ if(!_SEEPR1OSN){(updateFloatToEEPROM(0, 0, 0x0, ((_count1P)*(0.006666))));
_SEEPR1OSN=1;} }else{ if(_SEEPR1OSN){_SEEPR1OSN=0;}}
_tempVariable_float = (_count1P)*(0.006666);
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[0] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[1] = _savTWstrTemp.w2;


//Плата:2
//Наименование:фильтр 2
if (1) { if (_trgrt4I) { _trgrt4 = 0;} else {_trgrt4 = 1; _trgrt4I = 1;} } else {_trgrt4 = 0; _trgrt4I = 0;}; 
 _tempVariable_byte = (_modbusSlaveDataTable_4[24]);
for (int i=0; i < 8; i++){ _ByteToBit_2_outArray[i] = bitRead( _tempVariable_byte, i);}
if (_ByteToBit_2_outArray[1]) { if (_trgrt5I) { _trgrt5 = 0;} else {_trgrt5 = 1; _trgrt5I = 1;} } else {_trgrt5 = 0; _trgrt5I = 0;}; 
 if(_trgrt5)
{_swi2=(float(_readFloatFromModbus(_modbusSlaveDataTable_4[6], _modbusSlaveDataTable_4[7], 1)));}
else
{_swi2=(readFloatFromEEPROM(4, 0, 0x0));}
if(( (_trgrt4) || (_trgrt5) )) _count2P = ((_swi2)/(0.006666));
if (!(_bounseInputD5O)) 
   { 
   if (! _count2I)  
      {
       _count2P = _count2P+1;
       _count2I = 1;
      }
   }
else
   {
   _count2I=0;
   }
_tempVariable_float = (_count2P)*(0.006666);
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[4] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[5] = _savTWstrTemp.w2;

if (!(_bounseInputD5O))
{ if (_tim10I) { if (_isTimer(_tim10P, 20000)) {_tim10O = 1;}} else {_tim10I =1; _tim10P = millis();}} else {_tim10O = 0; _tim10I = 0;}
if (_bounseInputD5O)
{ if (_tim9I) { if (_isTimer(_tim9P, 20000)) {_tim9O = 1;}} else {_tim9I =1; _tim9P = millis();}} else {_tim9O = 0; _tim9I = 0;}
if (( (!(((readFloatFromEEPROM(4, 0, 0x0))) == ((_count2P)*(0.006666)))) && (( (_tim9O) || (_tim10O) )) ))
{ if (_tim2I) { if (_isTimer(_tim2P, 10760000)) {_tim2O = 1;}} else {_tim2I =1; _tim2P = millis();}} else {_tim2O = 0; _tim2I = 0;}
if (_tim2O) { if (_trgrt6I) { _trgrt6 = 0;} else {_trgrt6 = 1; _trgrt6I = 1;} } else {_trgrt6 = 0; _trgrt6I = 0;}; 
 if(( (_trgrt5) || (_trgrt6) )){ if(!_SEEPR2OSN){(updateFloatToEEPROM(4, 0, 0x0, ((_count2P)*(0.006666))));
_SEEPR2OSN=1;} }else{ if(_SEEPR2OSN){_SEEPR2OSN=0;}}

//Плата:3
//Наименование:фильтр 3
if (1) { if (_trgrt7I) { _trgrt7 = 0;} else {_trgrt7 = 1; _trgrt7I = 1;} } else {_trgrt7 = 0; _trgrt7I = 0;}; 
 _tempVariable_byte = (_modbusSlaveDataTable_4[24]);
for (int i=0; i < 8; i++){ _ByteToBit_3_outArray[i] = bitRead( _tempVariable_byte, i);}
if (_ByteToBit_3_outArray[2]) { if (_trgrt8I) { _trgrt8 = 0;} else {_trgrt8 = 1; _trgrt8I = 1;} } else {_trgrt8 = 0; _trgrt8I = 0;}; 
 if(_trgrt8)
{_swi3=(float(_readFloatFromModbus(_modbusSlaveDataTable_4[10], _modbusSlaveDataTable_4[11], 1)));}
else
{_swi3=(readFloatFromEEPROM(8, 0, 0x0));}
if(( (_trgrt7) || (_trgrt8) )) _count3P = ((_swi3)/(0.006666));
if (!(_bounseInputD5O)) 
   { 
   if (! _count3I)  
      {
       _count3P = _count3P+1;
       _count3I = 1;
      }
   }
else
   {
   _count3I=0;
   }
if (!(_bounseInputD5O))
{ if (_tim12I) { if (_isTimer(_tim12P, 20000)) {_tim12O = 1;}} else {_tim12I =1; _tim12P = millis();}} else {_tim12O = 0; _tim12I = 0;}
if (_bounseInputD5O)
{ if (_tim11I) { if (_isTimer(_tim11P, 20000)) {_tim11O = 1;}} else {_tim11I =1; _tim11P = millis();}} else {_tim11O = 0; _tim11I = 0;}
if (( (!(((readFloatFromEEPROM(8, 0, 0x0))) == ((_count3P)*(0.006666)))) && (( (_tim11O) || (_tim12O) )) ))
{ if (_tim3I) { if (_isTimer(_tim3P, 10770000)) {_tim3O = 1;}} else {_tim3I =1; _tim3P = millis();}} else {_tim3O = 0; _tim3I = 0;}
if (_tim3O) { if (_trgrt9I) { _trgrt9 = 0;} else {_trgrt9 = 1; _trgrt9I = 1;} } else {_trgrt9 = 0; _trgrt9I = 0;}; 
 if(( (_trgrt8) || (_trgrt9) )){ if(!_SEEPR3OSN){(updateFloatToEEPROM(8, 0, 0x0, ((_count3P)*(0.006666))));
_SEEPR3OSN=1;} }else{ if(_SEEPR3OSN){_SEEPR3OSN=0;}}
_tempVariable_float = (_count3P)*(0.006666);
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[8] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[9] = _savTWstrTemp.w2;


//Плата:4
//Наименование:фильтр 4
if (1) { if (_trgrt10I) { _trgrt10 = 0;} else {_trgrt10 = 1; _trgrt10I = 1;} } else {_trgrt10 = 0; _trgrt10I = 0;}; 
 _tempVariable_byte = (_modbusSlaveDataTable_4[24]);
for (int i=0; i < 8; i++){ _ByteToBit_4_outArray[i] = bitRead( _tempVariable_byte, i);}
if (_ByteToBit_4_outArray[3]) { if (_trgrt11I) { _trgrt11 = 0;} else {_trgrt11 = 1; _trgrt11I = 1;} } else {_trgrt11 = 0; _trgrt11I = 0;}; 
 _tempVariable_float = (readFloatFromEEPROM(12, 0, 0x0));
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[27] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[28] = _savTWstrTemp.w2;

if(_trgrt11)
{_swi4=(float(_readFloatFromModbus(_modbusSlaveDataTable_4[14], _modbusSlaveDataTable_4[15], 1)));}
else
{_swi4=(readFloatFromEEPROM(12, 0, 0x0));}
if(( (_trgrt10) || (_trgrt11) )) _count4P = ((_swi4)/(0.006666));
if (!(_bounseInputD6O)) 
   { 
   if (! _count4I)  
      {
       _count4P = _count4P+1;
       _count4I = 1;
      }
   }
else
   {
   _count4I=0;
   }
_tempVariable_float = (_count4P)*(0.006666);
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[12] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[13] = _savTWstrTemp.w2;

if (!(_bounseInputD6O))
{ if (_tim14I) { if (_isTimer(_tim14P, 20000)) {_tim14O = 1;}} else {_tim14I =1; _tim14P = millis();}} else {_tim14O = 0; _tim14I = 0;}
if (_bounseInputD6O)
{ if (_tim13I) { if (_isTimer(_tim13P, 20000)) {_tim13O = 1;}} else {_tim13I =1; _tim13P = millis();}} else {_tim13O = 0; _tim13I = 0;}
if (( (!(((readFloatFromEEPROM(12, 0, 0x0))) == ((_count4P)*(0.006666)))) && (( (_tim13O) || (_tim14O) )) ))
{ if (_tim4I) { if (_isTimer(_tim4P, 10780000)) {_tim4O = 1;}} else {_tim4I =1; _tim4P = millis();}} else {_tim4O = 0; _tim4I = 0;}
if (_tim4O) { if (_trgrt12I) { _trgrt12 = 0;} else {_trgrt12 = 1; _trgrt12I = 1;} } else {_trgrt12 = 0; _trgrt12I = 0;}; 
 if(( (_trgrt11) || (_trgrt12) )){ if(!_SEEPR4OSN){(updateFloatToEEPROM(12, 0, 0x0, ((_count4P)*(0.006666))));
_SEEPR4OSN=1;} }else{ if(_SEEPR4OSN){_SEEPR4OSN=0;}}

//Плата:5
//Наименование:фильтр 5
if (1) { if (_trgrt13I) { _trgrt13 = 0;} else {_trgrt13 = 1; _trgrt13I = 1;} } else {_trgrt13 = 0; _trgrt13I = 0;}; 
 _tempVariable_byte = (_modbusSlaveDataTable_4[24]);
for (int i=0; i < 8; i++){ _ByteToBit_5_outArray[i] = bitRead( _tempVariable_byte, i);}
if (_ByteToBit_5_outArray[4]) { if (_trgrt14I) { _trgrt14 = 0;} else {_trgrt14 = 1; _trgrt14I = 1;} } else {_trgrt14 = 0; _trgrt14I = 0;}; 
 if(_trgrt14)
{_swi5=(float(_readFloatFromModbus(_modbusSlaveDataTable_4[18], _modbusSlaveDataTable_4[19], 1)));}
else
{_swi5=(readFloatFromEEPROM(16, 0, 0x0));}
if(( (_trgrt13) || (_trgrt14) )) _count5P = ((_swi5)/(0.006666));
if (!(_bounseInputD6O)) 
   { 
   if (! _count5I)  
      {
       _count5P = _count5P+1;
       _count5I = 1;
      }
   }
else
   {
   _count5I=0;
   }
_tempVariable_float = (_count5P)*(0.006666);
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[16] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[17] = _savTWstrTemp.w2;

if (!(_bounseInputD6O))
{ if (_tim16I) { if (_isTimer(_tim16P, 20000)) {_tim16O = 1;}} else {_tim16I =1; _tim16P = millis();}} else {_tim16O = 0; _tim16I = 0;}
if (_bounseInputD6O)
{ if (_tim15I) { if (_isTimer(_tim15P, 20000)) {_tim15O = 1;}} else {_tim15I =1; _tim15P = millis();}} else {_tim15O = 0; _tim15I = 0;}
if (( (!(((readFloatFromEEPROM(16, 0, 0x0))) == ((_count5P)*(0.006666)))) && (( (_tim15O) || (_tim16O) )) ))
{ if (_tim5I) { if (_isTimer(_tim5P, 10790000)) {_tim5O = 1;}} else {_tim5I =1; _tim5P = millis();}} else {_tim5O = 0; _tim5I = 0;}
if (_tim5O) { if (_trgrt15I) { _trgrt15 = 0;} else {_trgrt15 = 1; _trgrt15I = 1;} } else {_trgrt15 = 0; _trgrt15I = 0;}; 
 if(( (_trgrt14) || (_trgrt15) )){ if(!_SEEPR5OSN){(updateFloatToEEPROM(16, 0, 0x0, ((_count5P)*(0.006666))));
_SEEPR5OSN=1;} }else{ if(_SEEPR5OSN){_SEEPR5OSN=0;}}

//Плата:6
//Наименование:фильтр 6
if (1) { if (_trgrt16I) { _trgrt16 = 0;} else {_trgrt16 = 1; _trgrt16I = 1;} } else {_trgrt16 = 0; _trgrt16I = 0;}; 
 _tempVariable_byte = (_modbusSlaveDataTable_4[24]);
for (int i=0; i < 8; i++){ _ByteToBit_6_outArray[i] = bitRead( _tempVariable_byte, i);}
if (_ByteToBit_6_outArray[5]) { if (_trgrt17I) { _trgrt17 = 0;} else {_trgrt17 = 1; _trgrt17I = 1;} } else {_trgrt17 = 0; _trgrt17I = 0;}; 
 if(_trgrt17)
{_swi6=(float(_readFloatFromModbus(_modbusSlaveDataTable_4[22], _modbusSlaveDataTable_4[23], 1)));}
else
{_swi6=(readFloatFromEEPROM(20, 0, 0x0));}
if(( (_trgrt16) || (_trgrt17) )) _count6P = ((_swi6)/(0.006666));
if (!(_bounseInputD6O)) 
   { 
   if (! _count6I)  
      {
       _count6P = _count6P+1;
       _count6I = 1;
      }
   }
else
   {
   _count6I=0;
   }
_tempVariable_float = (_count6P)*(0.006666);
_savTWstrTemp = _saveFloatToModbus(_tempVariable_float, 1);
_modbusSlaveDataTable_4[20] = _savTWstrTemp.w1;
_modbusSlaveDataTable_4[21] = _savTWstrTemp.w2;

if (!(_bounseInputD6O))
{ if (_tim18I) { if (_isTimer(_tim18P, 20000)) {_tim18O = 1;}} else {_tim18I =1; _tim18P = millis();}} else {_tim18O = 0; _tim18I = 0;}
if (_bounseInputD6O)
{ if (_tim17I) { if (_isTimer(_tim17P, 20000)) {_tim17O = 1;}} else {_tim17I =1; _tim17P = millis();}} else {_tim17O = 0; _tim17I = 0;}
if (( (!(((readFloatFromEEPROM(20, 0, 0x0))) == ((_count6P)*(0.006666)))) && (( (_tim17O) || (_tim18O) )) ))
{ if (_tim6I) { if (_isTimer(_tim6P, 10800000)) {_tim6O = 1;}} else {_tim6I =1; _tim6P = millis();}} else {_tim6O = 0; _tim6I = 0;}
if (_tim6O) { if (_trgrt18I) { _trgrt18 = 0;} else {_trgrt18 = 1; _trgrt18I = 1;} } else {_trgrt18 = 0; _trgrt18I = 0;}; 
 if(( (_trgrt17) || (_trgrt18) )){ if(!_SEEPR6OSN){(updateFloatToEEPROM(20, 0, 0x0, ((_count6P)*(0.006666))));
_SEEPR6OSN=1;} }else{ if(_SEEPR6OSN){_SEEPR6OSN=0;}}




}
bool _isTimer(unsigned long startTime, unsigned long period )
  {
  unsigned long currentTime;
currentTime = millis();
if (currentTime>= startTime) {return (currentTime>=(startTime + period));} else {return (currentTime >=(4294967295-startTime+period));}
  }

ISR(TIMER2_OVF_vect)
{ TCNT2=100;
if(_PWDC >= 1000){asm volatile ("jmp 0x0000");}else{_PWDC = _PWDC+1;};
}
struct  _savTWstr _saveFloatToModbus( float value, byte order)
{ _savTWstr result;
byte orderArray[4] ;
 _modbusOrderAray(orderArray,  order);
byte b;
 b = *(((byte*)&value) + orderArray[0] );
  for (byte i = 0; i < 8; i++) bitWrite(result.w2, i, bitRead(b, i));
  b = *(((byte*)&value) + orderArray[1] );
  for (byte i = 0; i < 8; i++) bitWrite(result.w2, i + 8, bitRead(b, i));
  b = *(((byte*)&value) + orderArray[2] );
  for (byte i = 0; i < 8; i++) bitWrite(result.w1, i, bitRead(b, i));
  b = *(((byte*)&value) + orderArray[3] );
  for (byte i = 0; i < 8; i++) bitWrite(result.w1, i + 8, bitRead(b, i));
 return result ;}
float _readFloatFromModbus(uint16_t w1,  uint16_t w2, byte order)
{
 byte b[4];
  byte orderArray[4] ;
  _modbusOrderAray(orderArray,  order);
  for (byte i = 0; i < 8; i++) bitWrite(b[orderArray[0]], i, bitRead(w2, i));
  for (byte i = 0; i < 8; i++) bitWrite(b[orderArray[1]], i, bitRead(w2, (i + 8)));
  for (byte i = 0; i < 8; i++) bitWrite(b[orderArray[2]], i, bitRead(w1, i));
  for (byte i = 0; i < 8; i++) bitWrite(b[orderArray[3]], i, bitRead(w1, (i + 8)));
return  *((float *)&b);
}
void _modbusOrderAray(byte orderArray[], byte order)
{
  if (order == 1) {
    orderArray[0] = 0;
    orderArray[1] = 1;
    orderArray[2] = 2;
    orderArray[3] = 3;
  }
 if (order == 2) {
    orderArray[0] = 2;
    orderArray[1] = 3;
    orderArray[2] = 0;
    orderArray[3] = 1;
  }
  if (order == 3) {
    orderArray[0] = 1;
    orderArray[1] = 0;
    orderArray[2] = 3;
    orderArray[3] = 2;
  }
  if (order == 4) {
    orderArray[0] = 3;
    orderArray[1] = 2;
    orderArray[2] = 1;
    orderArray[3] = 0;
  } 
}
byte _modbusSlavePoll()
{
if( !_modbusSlaveTCPClient.connected()) {_modbusSlaveTCPClient = _modbusSlaveTCPServer.available();}
if (_modbusSlaveTCPClient) { _modbusGetSlaveRxBuffer();} else{return 0;}
if(_modbusSlaveBufferSize ==0) {return 0;}
if ((_modbusSlaveBuffer[0] != 20) && (_modbusSlaveBuffer[0] != 0)) return 0;
  byte exception =  _modbusValidateRequest();
  if (exception > 0) {

    if (exception != 255) { _modbusSlaveBuildException( exception );
      _modbusSlaveSendTxBuffer();
    }  
    return exception;
  }

switch ( _modbusSlaveBuffer[1] ) {
 case 3 :
      return process_modbus_FC3(4);
      break;
  case 6 :
      return process_modbus_FC6();
      break;
 case 16 :
      return process_modbus_FC16();
      break;
default:
      break;
  }
  return 25;
}
byte _modbusValidateRequest() {
  boolean isSupported = false;
  for (uint8_t i = 0; i < sizeof( _modbusSlave_fctsupported ); i++) {
    if (_modbusSlave_fctsupported[i] == _modbusSlaveBuffer[1]) {
      isSupported = 1;
      break;
    }
  }
 if (!isSupported) { return 1;}
int intRegs = 0;
  byte byteRegs;
    switch ( _modbusSlaveBuffer[1] ) {
case 6 :
if(!(checkModbusAddres(( word( _modbusSlaveBuffer[2], _modbusSlaveBuffer[3]) ),4))){return 2;}     
        break;
case 3 :
case 16 :
 if(!(checkModbusRange((word( _modbusSlaveBuffer[2], _modbusSlaveBuffer[3])), (word( _modbusSlaveBuffer[4], _modbusSlaveBuffer[5])),4))){return 2;}    
 break;
}
  return 0; // OK, no exception code thrown
}
bool checkModbusAddres(int addr, byte table)
{
return (!(( modbusSlaveIndexForAddres(addr,table)) == -1));
}
int modbusSlaveIndexForAddres(int addr, byte table)
{
int tableSize = 0;
switch (table) {
case 4:
tableSize = 29;
break;
}
for (byte i = 0; i < tableSize; i++) {if((modbusSlaveAddresFromIndex(i,table)) == addr){return  i;}}
return -1;
}
int modbusSlaveAddresFromIndex(byte index, byte table)
{
switch (table) {
case 4:
return _modbusSlaveAddresTable_4[index];
break;
}
return -1;
}
bool checkModbusRange(int startAddr, int addrNumber, byte table)
{
for (int i=0; i < addrNumber; i++) {if(!(checkModbusAddres((startAddr+i),table))){return false;}}
return true;
}
void  _modbusSlaveBuildException( byte exception ) {
  byte func = _modbusSlaveBuffer[1];  
  _modbusSlaveBuffer[0] = 20;
  _modbusSlaveBuffer[1] = func + 0x80;
  _modbusSlaveBuffer[ 2 ] = exception;
  _modbusSlaveBufferSize = 3;}
void _modbusSlaveSendTxBuffer()
{
 if(_modbusSlaveBuffer[0] == 0) {_modbusSlaveTCPClient.stop(); return;}
String stringBuffer = "";
_modbusSlaveMBAPBuffer[4] = highByte(_modbusSlaveBufferSize);
_modbusSlaveMBAPBuffer[5] = lowByte(_modbusSlaveBufferSize);
 for (int i=0; i <6; i++){stringBuffer.concat(char( _modbusSlaveMBAPBuffer[i] ));}
for (int i=0; i <_modbusSlaveBufferSize; i++){stringBuffer.concat(char( _modbusSlaveBuffer[i]));}
_modbusSlaveTCPClient.print(stringBuffer);
 _modbusSlaveBufferSize = 0;
}
byte _modbusGetSlaveRxBuffer()
{
byte currentByte = 0;
boolean bBuffOverflow = false;
byte currentByteIndex = 0;
 _modbusSlaveBufferSize = 0;
 while (_modbusSlaveTCPClient.available() ) {currentByte = _modbusSlaveTCPClient.read();
if (currentByteIndex < 6) 
	{_modbusSlaveMBAPBuffer[currentByteIndex] = currentByte; } 
else
{
  _modbusSlaveBuffer[ _modbusSlaveBufferSize ] = currentByte; 
  _modbusSlaveBufferSize ++;
 if (_modbusSlaveBufferSize >= 64) bBuffOverflow = true;}
	currentByteIndex++;
  }
  if (bBuffOverflow) {return -3; }
  return _modbusSlaveBufferSize;
}
byte process_modbus_FC3(byte table)
{
  int startAddr = word( _modbusSlaveBuffer[2], _modbusSlaveBuffer[3] );
  int byteRegsno = word( _modbusSlaveBuffer[4], _modbusSlaveBuffer[5] );
  int i;
  int value;
byte index;
  _modbusSlaveBuffer[ 2 ]  = byteRegsno * 2;
  _modbusSlaveBufferSize = 3;
  for (i = startAddr; i < startAddr + byteRegsno; i++) {
index = modbusSlaveIndexForAddres(i, table);
if (table == 4) {value = _modbusSlaveDataTable_4[index]; } 
  _modbusSlaveBuffer[ _modbusSlaveBufferSize ] = highByte(value);
    _modbusSlaveBufferSize++;
    _modbusSlaveBuffer[ _modbusSlaveBufferSize ] = lowByte(value);
    _modbusSlaveBufferSize++;
  }
   _modbusSlaveSendTxBuffer();
  return _modbusSlaveBufferSize + 2;
}
byte process_modbus_FC6()
{
 int addres = word( _modbusSlaveBuffer[2], _modbusSlaveBuffer[3] );
 int index;
index = modbusSlaveIndexForAddres(addres, 4);
_modbusSlaveDataTable_4[index] =word( _modbusSlaveBuffer[4], _modbusSlaveBuffer[5] );
  _modbusSlaveBufferSize = 6;
  _modbusSlaveSendTxBuffer();
  return _modbusSlaveBufferSize + 2;
}
byte process_modbus_FC16(  ) 
{
byte func = _modbusSlaveBuffer[1];
  int startAddr = _modbusSlaveBuffer[2] << 8 | _modbusSlaveBuffer[3];
  int byteRegsno = _modbusSlaveBuffer[4] << 8 | _modbusSlaveBuffer[5];
  int i;
 int index;
  _modbusSlaveBuffer[4]   = 0;
  _modbusSlaveBuffer[5]   = byteRegsno;
  _modbusSlaveBufferSize = 6;
  for (i = 0; i < byteRegsno; i++) {
index = modbusSlaveIndexForAddres((startAddr+i), 4);
_modbusSlaveDataTable_4[index] =word( _modbusSlaveBuffer[ 7 + i * 2 ],  _modbusSlaveBuffer[8 + i * 2 ]);
  }
  _modbusSlaveSendTxBuffer();
  return _modbusSlaveBufferSize +2;
}
 byte readByteFromEEPROM(int addres, byte bitAddres, byte chipAddres)
{
return EEPROM.read(addres);
}
void updateByteToEEPROM(int addres, byte bitAddres, byte chipAddres, byte value)
{
return EEPROM.update(addres, value);
}
 float readFloatFromEEPROM(int addres, byte bitAddres, byte chipAddres)
{
byte x[4];
for(byte i = 0; i < 4; i++) { x[i] = readByteFromEEPROM( (addres+i),  bitAddres,  chipAddres);}
float *y = (float *)&x;
return y[0];
}
 void updateFloatToEEPROM(int addres, byte bitAddres, byte chipAddres, float value)
{
byte *x = (byte *)&value;
for(byte i = 0; i < 4; i++) {updateByteToEEPROM( (addres+i),  bitAddres,  chipAddres, x[i]);}
}

 

sadman41
Offline
Зарегистрирован: 19.10.2016

С проблемами FLProg пусть разбирается его автор.