Go Down

Topic: Modbus TCP master library (Read 6615 times) previous topic - next topic

Watcher

This should be 502. I changed it to 23 to test Telnet on an arduino based Telnet server.

Juraj

and did you try then again the inverter address and modbus port after telnet connection to inverter from computer connected?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Watcher

#47
Jan 13, 2018, 06:14 pm Last Edit: Jan 13, 2018, 06:41 pm by Watcher
Something really weird is going on :

1. I can telnet to the inverter on port 502 from the computer from a client like putty and from windows telnet.
2. I cannot telnet connect from arduino with a standard telnet client sketch loaded. I tried this with two different arduino mega's each with its own ethernet shield and connected to different points  of the network with the same result.
3. I can telnet from PC to both arduinos .
4. While trying all the above once or twice I did manage to get the arduino to successfully telnet the inverter but 98% of the times it fails

I think I am pretty confident that the possibility of a hardware issue in the arduino has been eliminated since both arduino setups easily telnet each other and from/to the computer.

So the question is why I can telnet the inverter from PC but not from arduino..

Edit :
I am thinking this could be some kinda timing issue. What if the timeout after connection attempt from the arduino is shorter that it is from the PC? Anyway to change it?

Juraj

The telnet port 23 is enabled on inverter? You can connect from computer to inverter on port 23?

Yes the timing. I copy/pasted a wrong version

Code: [Select]

int modbusRequest(byte uid, unsigned int addr, byte len, int *regs) {

  static NetClient modbus;
  if (!modbus.connected()) {
//    modbus.stop();
    modbus.connect(symoAddress, 502);
    if (!modbus.connected()) {
      modbus.stop();
      return MODBUS_CONNECT_ERROR;
    }
  }
  byte request[] = {0, 1, 0, 0, 0, 6, uid, 3, (byte) (addr / 256), (byte) (addr % 256), 0, len};
  modbus.write(request, sizeof(request));
  modbus.flush();
  if (!available(modbus)) {
    modbus.stop();
    return MODBUS_NO_RESPONSE;
  }
  for (int i = 0; i < 7; i++) { // skip 7
    if (modbus.read() == -1)
      return -3;
    if (!available(modbus))
      return -1;
  }
  switch (modbus.read()) { // code
    case 3:
      break;
    case -1:
      return -4;
    case 0x83:
      return modbus.read(); // 0x01, 0x02, 0x03 or 0x11
    default:
      return -6;
  }
  if (!available(modbus))
    return -1;
  int l = modbus.read() / 2;
  int i = 0;
  while (true) {
    if (!available(modbus))
      return -1;
    byte hi = modbus.read();
    if (!available(modbus))
      return -1;
    byte lo = modbus.read();
    regs[i++] = hi * 256 + lo;
    if (i == len || i == l)
      break;
  }
  return 0;
}

boolean available(Stream& client) {
  for (int i = 0; i < 5000; i++) {
    if (client.available())
      return true;
    delay(1);
  }
  return client.available();
}

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Watcher

#49
Jan 13, 2018, 08:28 pm Last Edit: Jan 13, 2018, 08:28 pm by Watcher
No, port 23 doesn't  connect on inverter.  When i said i can telnet the inverter, i meant on port 502, not on 23.

I didnt understand. What is the last  snippet  for? Any different from  the one i used?

Juraj

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Watcher

#51
Jan 14, 2018, 08:39 am Last Edit: Jan 14, 2018, 08:39 am by Watcher
I have tried the above sketch with a modbus server simulator on my android device and it works fine.

However ,for some reason, it still refuses to connect with the Inverter.

The same happens, that is, it doesn't connect  when i try to connect with a telnet client (at port 502) from the arduino, but it connects straight away from a telnet client on the computer which I can not explain.




Juraj

check the IP address of the inverter in the sketch digit by digit.

are all devices in the same network?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Watcher

Yes.. and Yes... !

Code: [Select]
byte subnet[] = { 255, 255, 255, 0 }; 
byte mac[] = {0xDE, 0xFE, 0xBE, 0xFF, 0xFE, 0xED };
byte DNS[]={ 192, 168, 10, 254 };
byte gateway[] = { 192, 168, 10, 254 };     


IPAddress local_IP (192,168,10,180);
IPAddress SMA_Address(192,168,10,5);

#define SMA_PORT 502

Watcher

Decided to try out different Ethernet timing settings in case this affects the connection.

added :
Code: [Select]

#include <utility/w5100.h>


and in setup()
Code: [Select]

W5100.setRetransmissionTime(2000);   //2000 is the default value, equivalent to 200ms
W5100.setRetransmissionCount(8);     //8 is the default value





Trying different values for re-transmit and count.. No luck so far...

Juraj

in inverter modbus settings is access from all IP addresses enabled? not only from your computer
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Watcher

in inverter modbus settings is access from all IP addresses enabled? not only from your computer
On the Inverter , the only available settings to change are

TCP Enable/disable,
UDP Enable/disable

and Port Number (default 502)


Watcher

#57
Jan 14, 2018, 09:42 am Last Edit: Jan 14, 2018, 09:45 am by Watcher
Trying different timing settings as in post #57 above, I managed to successfully connect read the value of a modbus register with

W5100.setRetransmissionTime(1500);   and
W5100.setRetransmissionCount(9); 


But connection is not consistent. If I reset the arduino and try again, it doesnt connect even with the above settings and I need to try a few times. Once the connection is achieved, then subsequent reads are done ok.

 



Watcher

#58
Jan 14, 2018, 09:56 am Last Edit: Jan 14, 2018, 03:37 pm by Watcher
Running out of ideas here...

With the tolerance  (!) Of the forum moderators, i might cross-post this issue in the peogramming section of the forum...:)

Watcher

Posted the issue in a new thread in the programming section of the forum here:

https://forum.arduino.cc/index.php?topic=522730.new#new

Go Up