Read Modbus TCP Register from Huawei Wechselrichter

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Laserl
Posts: 4
Joined: Sun Feb 20, 2022 8:12 am

Read Modbus TCP Register from Huawei Wechselrichter

Post by Laserl » Sun Dec 17, 2023 3:42 pm

Dear all

After hour and hour trying may be there is somebody who can help me. I try to read a Modbus TCP register.

questions
1. In which format I have to write the socket?
2. What is the correct read command?
3. How do I have to interpret the "modusResponse"

Any help?

Daniel

open socket "xxx

put "000100000006010436b00001" into modbusQuery
put binaryEncode("H*", modbusQuery) into modbusQuery

write modbusQuery to socket "xxx"

read from socket "xxx" for 8 bytes
put it into modbusResponse

close socket "xxx"

-- Interpretiere die Modbus-Antwort
if length(modbusResponse) = 8 then
put char 10 to -1 of modbusResponse into dataBytes
put binaryEncode("H*", dataBytes) into hexData
answer "Empfangene Daten: " & hexData

else
--answer "Ungueltige Modbus-Antwort - Laenge: "
end if

natacha95
Posts: 1
Joined: Tue Jan 30, 2024 7:29 pm

Re: Read Modbus TCP Register from Huawei Wechselrichter

Post by natacha95 » Tue Jan 30, 2024 7:35 pm

It looks like you are attempting to communicate with a Modbus TCP server using LiveCode. Modbus TCP messages are typically hexadecimal-encoded strings, and you need to send the appropriate Modbus command to read the desired register.

Here is an example of how you might structure your LiveCode script:

livecode
Copy code
-- Open a socket connection to the Modbus TCP server
open socket to "xxx" on port xxx

-- Modbus Query to read a holding register (example: register 1000)
put "000100000006010300041" into modbusQuery
put binaryEncode("H*", modbusQuery) into modbusQuery

-- Send the Modbus query to the server
write modbusQuery to socket "xxx"

-- Read the response from the server (adjust the number of bytes based on your expected response)
read from socket "xxx" for 12 bytes
put it into modbusResponse

-- Close the socket connection
close socket "xxx"

-- Interpret the Modbus response
if length(modbusResponse) = 12 then
put char 10 to -1 of modbusResponse into dataBytes
put binaryEncode("H*", dataBytes) into hexData
answer "Received Data: " & hexData
else
answer "Invalid Modbus Response - Length: " & length(modbusResponse)
end if
A few things to note:

Modbus Query Format: The format of the Modbus query may vary based on your specific needs. In the example above, it's set up to read a holding register (function code 03) from address 1000. You may need to adjust this based on your Modbus device and register.

Response Length: Make sure the number of bytes you are reading from the socket (read from socket "xxx" for 12 bytes) matches the expected length of the Modbus response.

Interpreting Response: The Modbus response typically includes function code, data length, and the actual data. You may need to parse the response according to the Modbus protocol specification for your specific device.

Please consult the documentation of your Modbus device to understand the correct format for Modbus queries and responses for your specific use case. If you have access to the Modbus TCP protocol documentation for your device, it will provide details on the correct commands and response formats.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”