small upd

This commit is contained in:
DashyFox 2024-11-21 17:51:59 +03:00
parent e6f2883584
commit a642e913ed
5 changed files with 95 additions and 42 deletions

View File

@ -40,7 +40,11 @@ void onSend(){
#define TEST_1 #define TEST_1
// #define TEST_2 // #define TEST_2
class VAR {
public:
uint16_t var = 1337;
};
VAR vvv;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -55,8 +59,12 @@ void setup()
// Ethernet.setRtCount(3); // Ethernet.setRtCount(3);
// client.setNoDelayedACK(true); // Не ждать ответа от сервера // client.setNoDelayedACK(true); // Не ждать ответа от сервера
client.setOnSendSuccess(onSend); // client.setOnSendSuccess(onSend);
client.setOnSendSuccess(std::bind([](VAR* bind_vvv){
Serial.print("Sending Successfully: ");
Serial.println(bind_vvv->var++);
}, &vvv));
} }
#ifdef TEST_2 #ifdef TEST_2
@ -120,4 +128,41 @@ void loop()
dataReady = false; dataReady = false;
} }
#endif #endif
if(client.available()>0){
byte response[16];
int len = client.read(response, sizeof(response));
if (len == 16)
{
// Проверка контрольной суммы
uint8_t checksum = 0;
for (int i = 0; i < 15; i++)
{
checksum += response[i];
}
Serial.print("RX: ");
for (int i = 0; i < 16; i++)
{
Serial.print(response[i], HEX);
Serial.print(" ");
}
if (response[15] == checksum)
{
Serial.println("Checksum is OK");
}
else
{
Serial.println("Checksum is ERROR");
}
client.activityUpdate(); // Обновляем время последней активности
}
else
{
Serial.println("Received an incomplete response");
}
}
} }

View File

@ -212,44 +212,12 @@ void EthernetMaketClient::tick_(){
} }
//******************************************************** //********************************************************
//? Проверка наличия доступных данных от сервера // //? Проверка наличия доступных данных от сервера
if (available() > 0) // if (available() > 0)
{ // {
byte response[16];
int len = read(response, sizeof(response)); // }
// //? -------------------------------------------------
if (len == 16)
{
// Проверка контрольной суммы
uint8_t checksum = 0;
for (int i = 0; i < 15; i++)
{
checksum += response[i];
}
Serial.print("RX: ");
for (int i = 0; i < 16; i++)
{
Serial.print(response[i], HEX);
Serial.print(" ");
}
if (response[15] == checksum)
{
Serial.println("Checksum is OK");
}
else
{
Serial.println("Checksum is ERROR");
}
lastActivityTime = millis(); // Обновляем время последней активности
}
else
{
Serial.println("Received an incomplete response");
}
}
//? -------------------------------------------------
} }
bool EthernetMaketClient::dataWrite(){ bool EthernetMaketClient::dataWrite(){
@ -330,3 +298,34 @@ void EthernetMaketClient::startConnection(IPAddress ip, uint16_t port, bool nonB
isNonBlocking = nonBlock; isNonBlocking = nonBlock;
connectStatus = CONNECT_START; connectStatus = CONNECT_START;
}; };
void EthernetMaketClient::activityUpdate(){
lastActivityTime = millis();
}
EthernetMaketClient& EthernetMaketClient::operator=(const EthernetMaketClient& other) {
if (this == &other) {
return *this; // Защита от самоприсваивания
}
// Копируем все данные
this->lastActivityTime = other.lastActivityTime;
this->startConnectionTime = other.startConnectionTime;
this->stopStartTime = other.stopStartTime;
this->isNonBlocking = other.isNonBlocking;
this->connectStatus = other.connectStatus;
// this->timeout = other.timeout;
// this->connectionTimeout = other.connectionTimeout;
// this->stopTimeout = other.stopTimeout;
this->dstIP = other.dstIP;
this->dstPort = other.dstPort;
this->dataPtr = other.dataPtr;
this->dataSize = other.dataSize;
this->onSendSuccess = other.onSendSuccess;
// Отключаем исполнение старого клиента
const_cast<EthernetMaketClient&>(other).connectStatus = CONNECT_IDLE;
return *this;
}

View File

@ -28,13 +28,15 @@ protected:
static std::list<EthernetMaketClient*>& getInstances(); // Ленивое создание списка static std::list<EthernetMaketClient*>& getInstances(); // Ленивое создание списка
void tick_(); void tick_();
ConnectionStatusSimple connectStatus = CONNECT_IDLE;
public: public:
ConnectionStatusSimple connectStatus = CONNECT_IDLE;
EthernetMaketClient(); EthernetMaketClient();
EthernetMaketClient(uint8_t sock); EthernetMaketClient(uint8_t sock);
~EthernetMaketClient(); ~EthernetMaketClient();
EthernetMaketClient& operator=(const EthernetMaketClient& other);
ConnectionStatusSimple connectNonBlock(IPAddress ip, uint16_t port); ConnectionStatusSimple connectNonBlock(IPAddress ip, uint16_t port);
void startConnection(IPAddress ip, uint16_t port, bool nonBlock = true); void startConnection(IPAddress ip, uint16_t port, bool nonBlock = true);
@ -50,6 +52,8 @@ public:
void setOnSendSuccess(const std::function<void()>& callback); void setOnSendSuccess(const std::function<void()>& callback);
void resetOnSendSuccess(); void resetOnSendSuccess();
void activityUpdate();
}; };
#endif // __MAKET_TCP_H__ #endif // __MAKET_TCP_H__

View File

@ -60,3 +60,7 @@ void EthernetMaketUDP::endPacketNonBlock()
void EthernetMaketUDP::tick(){ void EthernetMaketUDP::tick(){
sendUDP_tick(_sock, _status); sendUDP_tick(_sock, _status);
} }
ConnectionStatusSimple EthernetMaketUDP::getState() const{
return _status;
}

View File

@ -10,6 +10,7 @@ public:
using EthernetUDP::EthernetUDP; using EthernetUDP::EthernetUDP;
void endPacketNonBlock(); void endPacketNonBlock();
void tick(); void tick();
ConnectionStatusSimple getState() const;
}; };
#endif // __MAKET_UDP_H__ #endif // __MAKET_UDP_H__