Modbus TCP over wifi on a MKR Wifi1010

Hi.
I have a weird project I am working on.

I am trying to use the wifi on the MKR Wifi1010, to send Modbus TCP messages over.
And i get it to work, but:
1: it only receives every 3rd message.
2: When it receives, it will send the wrong data, and the next time i request, i get the rest of the data. (So i "hacked" the data and increased the message bit and was able to get a good feedback, just to prove that it was possible)

(Please ignore bad coding, this was made to prove the concept and implement into another project)

So my question is, why do i only see every 3rd message?
And why is it not sending all the data? ( I added the first client.write((byte) 0x00), due to it sending it wrong. With that there, the second time I request, the client receives a correct syntax string of data, without the data is missing the first 00)

I am not good at this, and learning as I go along. Most of this is patched together from examples and experimenting.

The client program I am using is made by the company I work for, and what I am trying to fool. Just to prove a point. :wink:

Print out from the RS232 from arduino at the bottom, and a picture of the client program that sends the requests.

//  Wifi
#include <WiFiNINA.h>

//  WiFi
char ssid[] = "TranstecTestBench";        // your network SSID (name)
char pass[] = "eSolution126";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(502);

void setup() {

  // SERIAL FOR DEBUGGING (The text takes ALOT of space, remove or reformat if needed)
  Serial.begin(9600);
  delay(2500);
  Serial.println("Serial RS232 (DEBUGGING PORT) running -----------------------------");
  
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // print the network name (SSID);
  Serial.print("Creating access point named: ");
  Serial.println(ssid);

  // Create open network. Change this line if you want to create an WEP network:
  status = WiFi.beginAP(ssid, pass);
  if (status != WL_AP_LISTENING) {
    Serial.println("Creating access point failed");
    // don't continue
    while (true);
  }
  
  // wait 10? seconds for connection:
  delay(5000);

  // start the web server on port 80
  server.begin();

  // you're connected now, so print out the status
  printWiFiStatus();
  
}

void loop() {
  //  Wifi Test
  // compare the previous status to the current status
  if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }
  
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    int buf[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int ii = 0;
    delay(500);
    Serial.println("What Master sent:");
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        int c = client.read();             // read a byte, then
        Serial.println(c, HEX);                    // print it out the serial monitor
        buf[ii] = c;
        ii++;
      } else {
        Serial.println("What i saw:");
        for(int i = 0; i <= 32; i++){
          Serial.println(buf[i], HEX);
        }
        Serial.println("What i Need:");
        Serial.println("Number of data points (Decimal):");
        Serial.println(buf[5]);
        Serial.println("Adress of Bench (Decimal):");
        Serial.println(buf[6]);
        Serial.println("Function:");
        Serial.println(buf[7]);
        Serial.println("Index (Decimal, RAW):");
        Serial.println(buf[9]);
        Serial.println("Index (Decimal, Actual):");
        Serial.println(buf[9] + 40001);
        
        Serial.println("Data:");
        Serial.println(buf[10]);
        Serial.println(buf[11]);

        client.write((byte) 0x00);
        client.write((byte) 0x00);
        client.write(buf[1]+1);
        client.write(buf[2]);
        client.write(buf[3]);
        client.write(buf[4]);
        client.write(05);
        client.write(buf[6]);
        client.write(buf[7]);
        client.write(2);
        client.write((byte) 0x00);
        client.write(1);

        client.flush();
        break;
      }
    }
  // close the connection:
  client.stop();
  Serial.println("client disconnected");
  }
}

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  WiFi.config(IPAddress(192, 168, 1, 66));
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);

}
9:31:01.363 -> Serial RS232 (DEBUGGING PORT) running -----------------------------
09:31:02.119 -> Please upgrade the firmware
09:31:02.119 -> Creating access point named: TranstecTestBench
09:31:09.122 -> SSID: TranstecTestBench
09:31:09.122 -> IP Address: 192.168.1.66
09:31:09.122 -> To see this page in action, open a browser to http://192.168.1.66
09:31:12.384 -> Device connected to AP
09:31:28.042 -> new client
09:31:28.511 -> What Master sent:
09:31:28.511 -> 0
09:31:28.511 -> 32
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 6
09:31:28.511 -> 67
09:31:28.511 -> 3
09:31:28.511 -> 0
09:31:28.511 -> 63
09:31:28.511 -> 0
09:31:28.511 -> 1
09:31:28.511 -> What i saw:
09:31:28.511 -> 0
09:31:28.511 -> 32
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 6
09:31:28.511 -> 67
09:31:28.511 -> 3
09:31:28.511 -> 0
09:31:28.511 -> 63
09:31:28.511 -> 0
09:31:28.511 -> 1
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 0
09:31:28.511 -> 7060
09:31:28.511 -> What i Need:
09:31:28.511 -> Number of data points (Decimal):
09:31:28.511 -> 6
09:31:28.511 -> Adress of Bench (Decimal):
09:31:28.511 -> 103
09:31:28.559 -> Function:
09:31:28.559 -> 3
09:31:28.559 -> Index (Decimal, RAW):
09:31:28.559 -> 99
09:31:28.559 -> Index (Decimal, Actual):
09:31:28.559 -> 40100
09:31:28.559 -> Data:
09:31:28.559 -> 0
09:31:28.559 -> 1
09:31:28.559 -> client disconnected
09:32:43.110 -> Device disconnected from AP

you could use a library. for example ArduinoModbus library.

or I have some simple functions

1 Like

Thank you. This works.
But i see problems with my code so far.

And you are correct, I should use a modbus lib.
I still have problems receiving data into the arduino. It throws alot of buggy messages.
But it works.... ish. :stuck_out_tongue: just have to make it better, :smiley:

Look like I got somthing.
When calling the client.stop(); in my program on my arduino, the system ignores data sendt due to my program on my computer still thinks its session is on. So there is a mix match of data.
Removed that and i get all data all the time. :smiley:

The problem i am having polling in Modbus is there is no callback that i can find.
I want to do commands on the incoming data, instead of reading through all the holding registers to see if something has changed.

So I am still not experienced enough. :wink:

Thankx for the snip of code Juraj, thats what I needed. :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.