Обмен данными по Ethernet между Arduino и Processing

Тема в разделе "Arduino & Shields", создана пользователем kipman, 7 фев 2019.

  1. kipman

    kipman Нуб

    Уважаемые форумчане, помогите решить следующую проблему.

    Нужно организовать обмен массивами данных по Ethernet между Arduino с Ethernet Shield W5100 и оболочкой на ПК, написанной в Processing. Нужно чтобы данные обновлялись раз в секунду. Упрощенный вариант скетчей привожу ниже. Значение счетчика заносится в массив и пересылается клиенту, клиент пересылает обратно значения своего счетчика.

    Ардуино
    Код (C++):

    #include <SPI.h>;
    #include <Ethernet.h>;


    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
    IPAddress ip(10, 100, 0, 72);
    EthernetServer server(80);

    byte sendBytes[] = {0, 15};
    byte receiveBytes[] = {0,0};
    long lastTime = 0;
    byte count = 0;
    void setup()
    {
      Serial.begin(9600);
      Ethernet.begin(mac, ip);
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
    }

    void loop() {
    EthernetClient client = server.available();
      if (client)
      {
        Serial.println("New Client Connected:");
        while (client.connected())
        {
          if (client.available())
          {
            byte dataSize = client.available();
            Serial.print("Data size is ");
            Serial.println(dataSize);
            for (byte m = 0; m <= 2; m++)
            {
             receiveBytes[m] = client.read();
             if (m==1)
            {
            Serial.println("DATA RECEIVED");
           
            client.write(sendBytes,HEX);
            client.stop();
            break;
            }
           }
        }
      }
      }

    Serial.print("Receive Bytes = ");
    for (byte i = 0; i<2; i++)
    {
      Serial.print(receiveBytes[i]);
    }
    Serial.println(" | ");
      Serial.print("Send Bytes = ");
    for (byte j = 0; j<2; j++)
    {
    Serial.print(sendBytes[j]);
    }
    Serial.println(" | ");
    delay(500);
    long now = millis();

    if (now > (lastTime + 1000))
    {
      count = count +1;
      lastTime = now;
    }
    if (count == 127)
    count = 0;

    sendBytes[1] = count;

    }
    Processing
    Код (Java):
    import processing.net.*;
    byte sendByte[] ={0,8};
    byte[] receiveByte = new byte[2];
    int count = 0;
    long lastTime = 0;

    Client MyClient;

    void setup()
    {
      size(800,600);
      textSize(20);
    }

    void draw()
    {
      background(0);
      MyClient = new Client(this, "10.100.0.72",80);
      MyClient.write(sendByte);
      delay(100);
      if (MyClient.available() > 0)
       {
         for (int i = 0; i<2; i++)
         receiveByte[i] = byte(MyClient.read());
         fill(100);
         text("MyClient Available",100,400);
       }
       delay(100);
       MyClient.stop();
       fill(250);
       text("sendBytes =", 50,100);
       text("receiveBytes =", 50,200);
       fill(250,250,0);
       for (int i = 0; i<2; i++)
       {
        text(sendByte[i], 200+i*30,100);
        text(receiveByte[i], 200+i*30,200);
       }
    long now = millis();
    if (now > (lastTime + 1000))
    {
      count = count +1;
      lastTime = now;
    }
    if (count==127)
    count = 0;
    sendByte[1] = byte(count);
    }
     


    Проблема заключается в следующем - данные с ПК на Ардуино передаются без проблем, а вот в обратном направлении массив обновляется раз в 5-30 секунд, и промежутки времени все время разные. Бъюсь уже неделю, перепробовал разные варианты, но пока прогресса не добился. Помогите, пожалуйста, советом.