Posts: 5
Threads: 1
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Aug 2025
Reputation:
0
Hey there,
I am looking for a way to communicate with the photovoltaic system on my roof via modbus with SmallBasic. Any ideas?
Posts: 654
Threads: 41
Likes Received: 482 in 334 posts
Likes Given: 256
Joined: Aug 2023
Reputation:
25
Hi,
As I understand it, ModBus is a protocol that can communication by serial or TCP, i.e. it is effectively a communication language.
The LitDev extension can send data by serial or by TCP, but there is no ModBus facility, therefore in its current state you would have to code the byte data to send and interpret any received data.
I believe this is possible, but it may be tricky to encode/decode the messages correctly. To some extent it depends on what you you want to do with the communication (control/visualisation of data etc), and what documentation you have for the roof device.
Perhaps someone else here has experience with ModBus can also help.
There are .Net libraries for ModBus, that could in principle be incorporated into a Small Basic extension, but the capabilitities of SB extensions will inevitably be a subset of the full capabilities, therefore depending on your coding experience, writing the communication directly in C# may be an option.
With maybe a few more details we can help you progress this project.
Posts: 80
Threads: 9
Likes Received: 53 in 36 posts
Likes Given: 34
Joined: Sep 2023
Reputation:
9
In the case of the serial variant, some thoughts
In theory, it should work with SmallBasic. However, there are no examples yet with binary protocols over the serial interface.
An extension for the serial port is required. I recommend Litdev's LDCommPort (there are others as well).
I assume that the RXAll and TXAll functions don't work with binary strings. However, there are TXByte and RXByte for reading and writing bytes one after the other.
The challenge is generating and decoding the telegrams:
1.Byte: Slave Address
2.Byte: FunctionCode
3-n.Byte: ByteCount & Data
n+1 Byte: 2-Byte CRC Checksum
The difficult part is generating the CRC checksum and converting the binary data into numeric values.
The SB program would be the master, and the PV system would be the slave.
It's also important to ensure that the complete transmission protocol is present before sending the individual bytes, since the Modbus specification requires that the timeout between the individual bytes also be checked.
So, all I can say is, letsrock
Posts: 5
Threads: 1
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Aug 2025
Reputation:
0
Yes, I have to add some details:
* I am using Modbus TCP (not serial) for communication
* All I want to do is read out 4 Input Registers that contain the values that I am interested in
Posts: 654
Threads: 41
Likes Received: 482 in 334 posts
Likes Given: 256
Joined: Aug 2023
Reputation:
25
08-17-2025, 09:17 PM
(This post was last modified: 08-17-2025, 10:34 PM by litdev.)
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
Posts: 654
Threads: 41
Likes Received: 482 in 334 posts
Likes Given: 256
Joined: Aug 2023
Reputation:
25
08-18-2025, 11:17 AM
(This post was last modified: 08-18-2025, 04:39 PM by litdev.)
Hi,
I did a bit more investigation and added modbus to LitDev extension - it could be useful for other devices.
You need updated Beta version of LD extension (1.2.30.8) from Extension Manager or website ( https://litdev.uk/#Extensions).
This is my test example (Serial untested).
Code: 'Tested using EasyModbusTCP Server Simulator, https://sourceforge.net/projects/easymodbustcp
LDModbus.ConnectTcp("127.0.0.1", 502)
LDModbus.WriteCoil(1,3,"True")
result = LDModbus.ReadCoils(1,1,5)
TextWindow.WriteLine(result)
result = LDModbus.ReadInputs(1,1,5)
TextWindow.WriteLine(result)
LDModbus.WriteRegister(1,3,200)
result = LDModbus.ReadHoldingRegisters(1,1,5)
TextWindow.WriteLine(result)
result = LDModbus.ReadInputRegisters(1,1,5)
TextWindow.WriteLine(result)
Output using Modbus simulator:
1=False;2=False;3=True;4=False;5=False;
1=False;2=False;3=False;4=False;5=False;
1=0;2=0;3=200;4=0;5=0;
1=0;2=0;3=0;4=0;5=0;
Posts: 5
Threads: 1
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Aug 2025
Reputation:
0
Thank you, works like a charm!
What are the 3 parameters for the function "LDModbus.ReadInputRegisters" ?
Posts: 654
Threads: 41
Likes Received: 482 in 334 posts
Likes Given: 256
Joined: Aug 2023
Reputation:
25
You should see them with intellisence in SB IDE.
Maybe my descriptions are not good, if not I can describe further.
Posts: 80
Threads: 9
Likes Received: 53 in 36 posts
Likes Given: 34
Joined: Sep 2023
Reputation:
9
I see that the starting address has an address range of 1-9999.
I suspect that Modbus allows a range up to 0xFFFF (2 bytes).
Posts: 654
Threads: 41
Likes Received: 482 in 334 posts
Likes Given: 256
Joined: Aug 2023
Reputation:
25
Yes, the 1 - 9999 was a nominal range I saw describing modbus in a few places. In fact I make no internal restrictions, the value entered will be converted to 16 bit integer (uShort) with no restrictions and passed to modbus, if it fails an error will occur.
|