![]() |
Modbus extension - Printable Version +- Small Basic Forum (https://litdev.uk/mybb) +-- Forum: Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=1) +--- Forum: Extensions (https://litdev.uk/mybb/forumdisplay.php?fid=3) +--- Thread: Modbus extension (/showthread.php?tid=486) Pages:
1
2
|
Modbus extension - letsrock - 08-17-2025 Hey there, I am looking for a way to communicate with the photovoltaic system on my roof via modbus with SmallBasic. Any ideas? RE: Modbus extension - litdev - 08-17-2025 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. RE: Modbus extension - Scout - 08-17-2025 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 RE: Modbus extension - letsrock - 08-17-2025 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 RE: Modbus extension - litdev - 08-17-2025 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_compute_the_modbus_rtu_message_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() 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 RE: Modbus extension - litdev - 08-18-2025 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 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; RE: Modbus extension - letsrock - 08-19-2025 Thank you, works like a charm! What are the 3 parameters for the function "LDModbus.ReadInputRegisters" ? RE: Modbus extension - litdev - 08-19-2025 You should see them with intellisence in SB IDE. Maybe my descriptions are not good, if not I can describe further. RE: Modbus extension - Scout - 08-19-2025 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). RE: Modbus extension - litdev - 08-19-2025 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. |