-1

I have one client and many servers and I need to let the client read the values of different servers. Only modbus-tcp protocol. What I need to ask are as follows:

  1. Is there other solution to solve the communication except Windows socket? If not, how can I create a socket and use it?

  2. how to read the value of multiple discrete registers?

  3. how can one client communicate with many servers? Create multiple sockets?

I'm running on Windows 7, Visual Studio 2015, MFC. I've tried to create a socket but failed.

I expect some useful documents or direct guidance. I'm not particularly clear about the whole technical solution.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
qjwzmy
  • 1
  • 1

2 Answers2

0

You could write your own Modbus stack but unless your goal is to learn I don't think that is necessary.

For MFC you can use libmodbus. It's written in C but you should be able to integrate it with Visual Studio, see here.

You have a ton of examples with the client and server side and all functions already implemented.

One client can connect with as many servers as you want, you just have to use a different port on each server and point your client to them. You could also have as many servers as you want using different IP addresses.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16
  • Thank you.I have researched the libmodbus and chose to use it.To achieve communication among one client and many servers,I want to create many "modbus_t" struct.My goal is to use the client to collect data from servers,so I could set server different IP address and same port such as 502,and just let client send request to different IP address and receive data from servers.Thanks for your help. – qjwzmy Jul 21 '19 at 07:15
  • Great, good luck with your project. As I said above you can have both: multiple servers with different IP addresses and multiple instances of a sever listening on different ports. – Marcos G. Jul 21 '19 at 07:24
0

Is there other solution to solve the communication except Windows socket?

Yes, you use sockets to work with TCP connections. If you don't want to work with sockets directly, then use a pre-existing wrapper, such as libmodbus:

"libmodbus is a free software library to send/receive data according to the Modbus protocol. This library is written in C and supports RTU (serial) and TCP (Ethernet) communications."

If not, how can I create a socket and use it?

Detailed tutorials for how to use Window's socket library, Winsock, are on Microsoft's MSDN site:

https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-start-page-2

how can one client communicate with many servers? Create multiple sockets?

Yes. TCP uses a 1:1 relationship between client and server. So, to communicate with multiple TCP servers, you need multiple socket connections.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I have searched the libmodbus and found it very suitable for my work.I choose the libmodbus.Thanks for your help. – qjwzmy Jul 21 '19 at 07:08