Page 1 of 1
modbus-server-tcpip
Posted: Sat Feb 21, 2026 1:08 pm
by mk-soft
After a request from the German forum, I wrote a new basic for a Modbus Server.
Link:
https://github.com/mk-soft-65/modbus-server-tcpip
Modbus Server and Data Source Basis
The Modbus server and data source thread run asynchronously. To ensure data consistency, they must be mutually protected with mutexes during updates.
The data is entered into the input and holding registers in Modbus format
- 16-bit word in high/low byte notation (bswap16)
- 32-bit long/float values in high/low word notation (bswap32).
- * Set "Swap words in 32-bit values" in the modbus client. This is the current standard for most devices.
- 64-bit Quad/Double values the same (bswap64)
32-bit values always require 2 registers and 64-bit values always require 4 registers.
To make it easier to refer your own values to the registers, create a superimposed structure and assign it to the same address as the modbus registers.
Re: modbus-server-tcpip
Posted: Sun Feb 22, 2026 1:47 pm
by mk-soft
Update v1.02.2
- Added string to modbus registers
Re: modbus-server-tcpip
Posted: Sun Feb 22, 2026 11:24 pm
by SMaag
Thanks for sharing!
Re: modbus-server-tcpip
Posted: Mon Feb 23, 2026 2:05 am
by Drone
Hi.
Thank you...
Re: modbus-server-tcpip
Posted: Mon Feb 23, 2026 7:32 am
by Bruce33
Thank you for sharing this code. It's an interesting learning example for me.
I've tried it on Windows (my OS at work - I have no choice) and on Ubuntu 24.04 (my OS at home - for fun!)
I wasted some time on Ubuntu before realizing that I had to run this program with sudo to properly open TCP port 502.
I did not understand the message "Error Timeout Init Modbus Server on Port 502" I thought it was an idle timeout... Maybe 1 minute is too long for this timeout, because if CreateNetworkServer does not work on the first attempt, it may not work on subsequent attempts either.
Re: modbus-server-tcpip
Posted: Mon Feb 23, 2026 9:23 am
by mk-soft
Under Linux, there is a lockout period before the same server port can be used again. One minute may be too long.
However, I believe this has been shortened in the new version of PureBasic.
I will add a note that root privileges are required under Linux.
Under Linux, root privileges are required for all ports below 1024.
Re: modbus-server-tcpip
Posted: Mon Feb 23, 2026 6:50 pm
by Kwai chang caine
I'm not quite sure what to do with it

, but it works here.
Thanks for sharing

Re: modbus-server-tcpip
Posted: Mon Feb 23, 2026 7:13 pm
by SMaag
I'm not quite sure what to do with it
ModBus an industrial protocol to connect I/O Hardware with a PLC. ModBus is an open protocol, so everyone can use it.
I gues originally developed by Schneider Electric.
Devices supporting Modbus:
- industrial devices like inverters
- solar inverters
- measuring instruments
- some smart home devices
- etc.
Modubs provides 64K 16 Bit registers, 64K Digital Coils (Outputs) and 64K Digital Inputs.
Theoretically you can connect 2 Applicatations on 2 different PC's with Modbus.
Maybe to transfer Maus, Joystick and Keyboard signals.
Re: modbus-server-tcpip
Posted: Wed Feb 25, 2026 7:16 am
by Kwai chang caine
Thanks a lot for your explanation SMAAG

It's surprising all this new or not protocols, often come to electronic component

Fortunately when i having passed my little electronic diploma, the DEL just become and we learn the behavior of transistor

Now... No need to learn a transistor... It's completely replaced by a full computer, to simply control a solenoid valve, a computer for each thing

Re: modbus-server-tcpip
Posted: Wed Feb 25, 2026 5:08 pm
by Axolotl
Well, the Modbus Protocol is a messaging structure developed by Modicon in 1979 and is not industry specific but is used across a wide range of industries
Modicon, today Schneider Electric, assisted in the development of an independent developer and user community organization: The Modbus Organization.
The Modbus protocol was transferred from Schneider Electric to the Modbus Organization in April 2004, signaling a commitment to openness. The specification is available free of charge for download, and there are no subsequent licensing fees required for using Modbus or Modbus TCP/IP protocols. Additional sample code, implementation examples, and diagnostics are available on the Modbus TCP toolkit, a free benefit to Modbus Organization members and available for purchase by nonmembers
To learn more look at there web-pages:
Modbus Organization.
Re: modbus-server-tcpip
Posted: Wed Feb 25, 2026 6:14 pm
by Kwai chang caine
Thanks for the link and explanations
