write_register.py 997 B

123456789101112131415161718192021
  1. #!/usr/bin/env python
  2. import minimalmodbus
  3. instrument = minimalmodbus.Instrument('/dev/ttyAMA0', 1) # port name, slave address (in decimal)
  4. #instrument = minimalmodbus.Instrument('/dev/serial0', 1) # port name, slave address (in decimal)
  5. instrument.debug = True
  6. #instrument.serial.port # this is the serial port name
  7. instrument.serial.baudrate = 2400 # Baud
  8. instrument.serial.bytesize = 8
  9. instrument.serial.parity = minimalmodbus.serial.PARITY_EVEN
  10. instrument.serial.stopbits = 1
  11. instrument.serial.timeout = 0.5 # seconds
  12. #instrument.address # this is the slave address number
  13. instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
  14. # It needs to long press the button on meter last for 3 seconds to enter the set-up interface firstly,
  15. # then the set-up can be realized via RS485 communication. After the set-up is finished,
  16. # long press last for 3 seconds to exit the set-up interface
  17. instrument.write_float(28, 2, 2) #Write Address=28, baudrate 2=9600, 2 registers)