We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modbus extension
#5
Hi,
Still trying to get more details, before moving towards a solution...

I assume you have the IP:port address for the TCP connection and details of the registers you want to get data from.  You would create a small server program (LDServer could probably do that), send a request and get the message back.  Both would be a relatively short sequency of bytes.  Something like what is described on this https://www.simplymodbus.ca/FC04.htm

The main difficulty looks like calculating the CRC as Scout mentioned - there is an algorithm for this, but a bit of work, probably doable in SB, but not trivial (e.g. https://ctlsys.com/support/how_to_comput...essage_crc).

Don't discount doing this in C# using a Modbus Nuget package either.

We can help with either, probably using C# is easier (a bit higher level - not handling bytes big/little endian, hand calculating CRC etc).

EDIT

The Modbus system does look pretty simple with many implementations in C# available (reinvingting can be fun, but mostly only if you are doing it for fun, otherwise I recommend just using what's already there), using NModbus4 for example something like:

Code:
        public static void ModbusTcpMasterReadInputs()
        {
            using (TcpClient client = new TcpClient("127.0.0.1", 502))
            {
                ModbusIpMaster master = ModbusIpMaster.CreateIp(client);

                // read five input values
                ushort startAddress = 100;
                ushort numInputs = 5;
                bool[] inputs = master.ReadInputs(startAddress, numInputs);

                for (int i = 0; i < numInputs; i++)
                {
                    Console.WriteLine($"Input {(startAddress + i)}={(inputs[i] ? 1 : 0)}");
                }
            }

            // output:
            // Input 100=0
            // Input 101=0
            // Input 102=0
            // Input 103=0
            // Input 104=0
        }

If you really wanted the data back in SB, then easy to write a small extension for this - again we can help.

EDIT 2

Looks like CRC is only required for the serial RTU mode
Reply


Messages In This Thread
Modbus extension - by letsrock - 08-17-2025, 02:52 PM
RE: Modbus extension - by litdev - 08-17-2025, 04:53 PM
RE: Modbus extension - by Scout - 08-17-2025, 05:58 PM
RE: Modbus extension - by letsrock - 08-17-2025, 07:40 PM
RE: Modbus extension - by litdev - 08-17-2025, 09:17 PM
RE: Modbus extension - by litdev - 08-18-2025, 11:17 AM
RE: Modbus extension - by letsrock - 08-19-2025, 10:13 AM
RE: Modbus extension - by litdev - 08-19-2025, 07:36 PM
RE: Modbus extension - by Scout - 08-19-2025, 09:13 PM
RE: Modbus extension - by litdev - 08-19-2025, 09:36 PM
RE: Modbus extension - by letsrock - 08-22-2025, 10:04 AM
RE: Modbus extension - by AbsoluteBeginner - 08-22-2025, 02:46 PM
RE: Modbus extension - by litdev - 08-22-2025, 03:58 PM
RE: Modbus extension - by letsrock - 08-22-2025, 05:10 PM
RE: Modbus extension - by litdev - 08-22-2025, 07:58 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)