Advertisement
microrobotics
Apr 12th, 2024
61
0
Never
This is comment for paste Huabang DDS228 Modbus Parser
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Install Dependencies:
  2. Ensure that you have the pyserial library installed. You can install it using pip:
  3.  
  4. pip install pyserial
  5.  
  6. Connect the Energy Meter:
  7. 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).
  8. 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:
  9. Baud rate: 9600
  10. Data bits: 8
  11. Parity: Even
  12. Stop bits: 1
  13.  
  14. Configure the Code:
  15. Open the Python script in a text editor or an IDE.
  16. 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:
  17.  
  18. def construct_modbus_request(register_choice, slave_id=0x01):
  19.  
  20. Replace 0x01 with the hexadecimal representation of your energy meter's slave ID.
  21.  
  22. Run the Code:
  23. Save the Python script.
  24. Open a terminal or command prompt and navigate to the directory containing the Python script.
  25. Run the script by executing the following command:
  26.  
  27. python script_name.py
  28.  
  29. Replace script_name.py with the actual name of your Python script file.
  30.  
  31. Interact with the Script:
  32. 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.
  33. The script will then display a menu with options to request different registers from the energy meter:
  34.  
  35. Select the register you want to request:
  36. 1. Total active energy (kWh)
  37. 2. Reverse active energy (kWh)
  38. 3. Voltage (V)
  39. 4. Current (A)
  40. 5. Active power (kW)
  41. 6. Power factor
  42. 7. Grid frequency (Hz)
  43. 0. Exit
  44.  
  45. Enter the corresponding number (1-7) for the register you want to read, and press Enter.
  46.  
  47. The script will send the Modbus request to the energy meter and display the received response value.
  48.  
  49. 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.
  50. To exit the script, enter 0 and press Enter.
  51.  
  52. Interpreting the Output:
  53. The script will print the received value from the energy meter for the requested register.
  54. 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.
  55. 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