Dan

Ответы в темах

Просмотр 1 сообщения - с 1 по 1 (всего 1)
  • Автор
    Записи
  • в ответ на: Modbus TCP & arduino #15063
    Dan
    Участник

    Здравствуйте, хочу реализовать ModbusTCP на Arduino, пытался запустить на на библиотеке ArduinoModbus и OpenScada, но при соединении и отправке coil сразу терялось соединение. Возможно попросить вас поделиться скетчем и библиотеками именно для ардуины, чтобы быть в них уверенными и вести отладку уже в OpenScada…

    ВОТ МОЙ КОД:

    #include <SPI.h>
    #include <Ethernet2.h>
    byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    };
    IPAddress ip(192, 168, 1, 10);
    EthernetServer server(502);

    #include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
    #include <ArduinoModbus.h>

    boolean testCoil = 0; // your network key Index number (needed only for WEP)

    const int ledPin = LED_BUILTIN;
    unsigned long time_now = 0;

    ModbusTCPServer modbusTCPServer;

    void setup() {
    //Initialize serial and wait for port to open:
    Serial.begin(115200);

    Serial.println(«Modbus TCP Server LED»);
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print(«server is at «);
    Serial.println(Ethernet.localIP());

    // start the Modbus TCP server
    if (!modbusTCPServer.begin()) {
    Serial.println(«Failed to start Modbus TCP Server!»);
    while (1);
    }

    // configure the LED
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);

    // configure a single coil at address 0x00
    modbusTCPServer.configureCoils(0x00, 1);
    }

    void loop() {
    // listen for incoming clients
    EthernetClient client = server.available();
    if (client) {
    // a new client connected
    Serial.println(«new client»);

    boolean currentLineIsBlank = true;
    while (client.connected()) {
    modbusTCPServer.poll();
    if(millis() — time_now > 1000){
    time_now = millis();
    Serial.println(«+»);
    }
    // update the LED
    updateLED();
    }

    }
    if(millis() — time_now > 1000){
    time_now = millis();
    if (!client) {
    Serial.println(«client disconnected»);
    }
    if (testCoil){
    modbusTCPServer.coilWrite(0x00, 0);
    testCoil = false;
    }
    else{
    modbusTCPServer.coilWrite(0x00, 1);
    testCoil = true;
    }
    Serial.print(«Coil1 = «);
    Serial.println(modbusTCPServer.coilRead(0x00));
    }

    }

    void updateLED() {
    // read the current value of the coil
    int coilValue = modbusTCPServer.coilRead(0x00);

    if (coilValue) {
    // coil value set, turn LED on
    digitalWrite(ledPin, HIGH);
    } else {
    // coild value clear, turn LED off
    digitalWrite(ledPin, LOW);
    }
    }

    • Ответ изменён 6 лет, 5 месяцев назад пользователем Dan.
Просмотр 1 сообщения - с 1 по 1 (всего 1)