Advertisement
This is comment for paste
Huabang DDS228 Modbus Parser
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Install Dependencies:
- Ensure that you have the pyserial library installed. You can install it using pip:
- pip install pyserial
- Connect the Energy Meter:
- Connect the energy meter to the device (e.g., a computer or a Raspberry Pi) running the Python code using a serial cable (e.g., RS-485 or RS-232).
- Make sure the serial connection settings (baud rate, data bits, parity, and stop bits) on both the energy meter and the device match. According to the documentation, the settings should be:
- Baud rate: 9600
- Data bits: 8
- Parity: Even
- Stop bits: 1
- Configure the Code:
- Open the Python script in a text editor or an IDE.
- Locate the construct_modbus_request function and ensure that the slave ID (device address) is set correctly. In the provided code, the slave ID is set to 0x01 (decimal 1). If your energy meter has a different slave ID, update the line:
- def construct_modbus_request(register_choice, slave_id=0x01):
- Replace 0x01 with the hexadecimal representation of your energy meter's slave ID.
- Run the Code:
- Save the Python script.
- Open a terminal or command prompt and navigate to the directory containing the Python script.
- Run the script by executing the following command:
- python script_name.py
- Replace script_name.py with the actual name of your Python script file.
- Interact with the Script:
- The script will prompt you to enter the COM port (e.g., COM1, /dev/ttyUSB0) to which the energy meter is connected. Enter the appropriate COM port and press Enter.
- The script will then display a menu with options to request different registers from the energy meter:
- Select the register you want to request:
- 1. Total active energy (kWh)
- 2. Reverse active energy (kWh)
- 3. Voltage (V)
- 4. Current (A)
- 5. Active power (kW)
- 6. Power factor
- 7. Grid frequency (Hz)
- 0. Exit
- Enter the corresponding number (1-7) for the register you want to read, and press Enter.
- The script will send the Modbus request to the energy meter and display the received response value.
- After displaying the response, the script will wait for a short period (0.1 seconds) before prompting you for the next register choice. This delay is to avoid flooding the energy meter with requests.
- To exit the script, enter 0 and press Enter.
- Interpreting the Output:
- The script will print the received value from the energy meter for the requested register.
- For 32-bit float values (e.g., voltage, current, active power), the script will display the value with the appropriate number of decimal places based on the expected accuracy.
- For 16-bit unsigned integer values (if any), the script will display the integer value directly.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement