mirror of
https://github.com/Show-maket/EthernetMaket.git
synced 2025-05-04 15:20:18 +00:00
150 lines
4.1 KiB
C++
150 lines
4.1 KiB
C++
#include "EthernetMaket.h" // Предполагается, что этот файл подключает Ethernet3.h
|
||
|
||
#include <map>
|
||
|
||
// Настройки для W5500
|
||
#define W5500_CS_PIN PA2 // Пин для CS
|
||
#define SOCKET_NUMBER 0 // Используем сокет 0
|
||
#define SERVER_PORT 1337 // Порт на сервере
|
||
#define CLIENT_PORT 1337 // Порт клиента
|
||
|
||
#define ServerMac {0xDA, 0x7A, 0xFF, 0x00, 0x00, 0x00}
|
||
#define ServerIP {192, 168, 254, 254}
|
||
|
||
#define IniMac {0xDA, 0x7A, 0xF0, 0x00, 0x00, 0x00}
|
||
#define IniIP {192, 168, 254, 253}
|
||
|
||
uint8_t gateway[] = {192, 168, 0, 1}; // Шлюз
|
||
uint8_t subnet[] = {255, 255, 0, 0}; // Маска подсети
|
||
|
||
uint8_t mac[] = IniMac; // MAC-адрес
|
||
uint8_t localIP[] = IniIP; // Локальный IP-адрес
|
||
|
||
uint8_t serverMac[] = ServerMac; // MAC-адрес сервера
|
||
uint8_t serverIP[] = ServerIP; // IP-адрес сервера
|
||
|
||
uint8_t buf[512]; // Буфер для передачи данных
|
||
|
||
uint32_t previousMillis = 0; // Для хранения времени последней отправки
|
||
const long interval = 3750; // Интервал отправки данных (1.75 секунды)
|
||
uint32_t ttt;
|
||
|
||
EthernetServer server(SERVER_PORT);
|
||
|
||
|
||
struct EthernetMaketClientStruct
|
||
{
|
||
EthernetMaketClient clientMember; //
|
||
};
|
||
|
||
std::map<uint16_t, EthernetMaketClientStruct> clientMap;
|
||
|
||
uint32_t packetCounter = 0;
|
||
uint32_t data1;
|
||
uint32_t data2;
|
||
|
||
void setup()
|
||
{
|
||
Serial.begin(115200);
|
||
pinMode(LED_BUILTIN, OUTPUT);
|
||
Ethernet.setCsPin(W5500_CS_PIN);
|
||
Ethernet.init();
|
||
|
||
// Настройка Ethernet с указанием статических параметров
|
||
Ethernet.begin(mac, localIP, subnet, gateway, gateway);
|
||
|
||
|
||
EthernetMaketClientStruct c1;
|
||
EthernetMaketClientStruct c2;
|
||
clientMap.insert({1, c1});
|
||
clientMap.insert({2, c2});
|
||
}
|
||
|
||
void loop()
|
||
{
|
||
EthernetMaketClient::tick();
|
||
uint32_t currentMillis = millis();
|
||
if (currentMillis - ttt > 25)
|
||
{
|
||
digitalToggle(PC13);
|
||
ttt = currentMillis; // Обновляем время
|
||
}
|
||
|
||
if (currentMillis - previousMillis >= interval)
|
||
{
|
||
previousMillis = currentMillis;
|
||
|
||
Serial.println("connectNonBlock");
|
||
clientMap[1].clientMember.startConnection(serverIP, SERVER_PORT);
|
||
clientMap[2].clientMember.startConnection(serverIP, SERVER_PORT);
|
||
|
||
|
||
//* for single packets
|
||
Serial.print("Sending packet ONE: ");
|
||
data1 = packetCounter;
|
||
clientMap[1].clientMember.dataWrite((uint8_t *)&data1, sizeof(data1));
|
||
for (size_t j = 0; j < sizeof(data1); j++)
|
||
{
|
||
Serial.print(((uint8_t *)&data1)[j], HEX);
|
||
Serial.print(" ");
|
||
}
|
||
Serial.println();
|
||
|
||
packetCounter++;
|
||
data2 = packetCounter;
|
||
|
||
Serial.print("Sending packet TWO: ");
|
||
clientMap[2].clientMember.dataWrite((uint8_t *)&data2, sizeof(data2));
|
||
for (size_t j = 0; j < sizeof(data2); j++)
|
||
{
|
||
Serial.print(((uint8_t *)&data2)[j], HEX);
|
||
Serial.print(" ");
|
||
}
|
||
Serial.println();
|
||
|
||
|
||
|
||
|
||
packetCounter++;
|
||
|
||
|
||
}
|
||
|
||
|
||
if(clientMap[1].clientMember.available()>0){
|
||
byte response[16];
|
||
int len = clientMap[1].clientMember.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");
|
||
}
|
||
clientMap[1].clientMember.activityUpdate(); // Обновляем время последней активности
|
||
}
|
||
else
|
||
{
|
||
Serial.println("Received an incomplete response");
|
||
}
|
||
}
|
||
|
||
} |