• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Przesyłanie danych do Arduino UNO po UDP
#1
Witam, jestem początkującym programistą. Nie wiem jak do końca sobie poradzić z następującym problemem. Chciałbym zaprogrmować swoje UNO tak aby pracowało jako rozszeżenie wyjść sterownika PLC. Sterowniki wpięte do sieci, PLC pracuje jako master UDP. PLC wysyła dane w postaci  pakietów Modbus - słów wordowych  (2- bajtowych). Docelowo pakiety powinny trafiać do pamięci sterownika a ich adres startowy określa PLC. Na podstawie tego UNO będzie wyzwalało odpowiednie stany na wyjściach.
 Udało mi się nawiązać połączenie testowe, widze że UNO odbiera dane ale wydaje mi się że nie do końca pełne tzn tylko 8bitowe. Nie wiem co dalej, jak sobie z tym poradzić. Poniżej przedstawiam kod źródłowy, który testuje.

#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x44, 0x8A, 0x5B, 0x8E, 0xC6, 0xAD
};
IPAddress ip(100, 0, 66, 103);

unsigned int localPort =502;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);  // MKR ETH shield
  //Ethernet.init(0);  // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. Sad");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start UDP
  Udp.begin(localPort);
}

void loop() {

  // jeśli odebraliśmy pakiet to go odczytujemy (jeśli rozmiar jest większy od 0) 
  int packetSize = Udp.parsePacket(); 
  if(packetSize) 
  { 
    Serial.print("Received packet of size "); 
    Serial.println(packetSize);//wyświetlamy rozmiar pakietu 
    Serial.print("From ");//adres IP nadawcy 
    IPAddress remote = Udp.remoteIP(); 
    for (int i =0; i < 4; i++) 
    { 
      Serial.print(remote[i], DEC); 
      if (i < 3) 
      { 
        Serial.print("."); 
      } 
    } 
    Serial.print(", port ");//port, z którego nadano pakiet 
    Serial.println(Udp.remotePort()); 
 
    // odczytujemy dane z pakietu 
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); 
    Serial.println(UDP_TX_PACKET_MAX_SIZE); 
    Serial.println("Contents:"); 
    //kończymy je jak ciąg znaków znkiem NUL (0) 
    //packetBuffer[packetSize]=0; 
    //wyświetlamy zawartość pakietu 
 
    for  (int j=0; j<UDP_TX_PACKET_MAX_SIZE-1; j++)
    Serial.println(int(packetBuffer[j]));
    Serial.println(); 
  // Serial.println(int(packetBuffer[0]));
  // Serial.println(packetBuffer[1]);
  // Serial.println(packetBuffer[2]);
  }
  delay(10);
}
 
Odpowiedź
#2
Zastanawia mnie delay(10). Jaka jest jego rola?
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości