I am trying to write to a PCON controller from a Raspberry Pi 3b using pymodbus. I have python 3.5 and pymodbus 1.4. I verified the communication path through the hardware by connecting a laptop running a modbus programming tool (this is for an application that will control an electric cylinder to clamp a part) and I was able to turn the servo on/off and I was able to jog the cylinder.
I created a very simple script to test talking to the controller and turning on the servo. #!/usr/bin/env python3 # -- coding: utf-8 --
import pymodbus
import serial
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
client = ModbusClient(method="rtu", port="/dev/serial0", stopbits=1,
bytesize=8, parity='N', bauderate = 38400, timeout=.5)
connection=client.connect()
#read/write to coils
client.write_coil(260,1,unit=0x00) #address, value, unit
This is the debug output
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:send: 0x3a 0x30 0x30 0x30 0x36 0x30 0x31 0x30
0x34 0x30 0x30 0x30 0x31 0x46 0x34 0xd 0xa
DEBUG:pymodbus.transaction:recv:
DEBUG:pymodbus.transaction:getting transaction 1
The address to the coil is 260 (from the supplier), the unit is #0, and the data value is 1 (coil in a bit). When you look at the hex values in DEBUG and run it through the Hex to ascii converter you get :00050104FF00F7. the first "00" is the unit number and this is correct, the 05 is the function code and is added by pymodbus, the next four characters is the address - is 0104 and should be 0260, and the next four is the value - in this case FF00 represents ON which is correct. I am using integers for the address, but obviously that is incorrect. I cannot find any info on the format of the address - all examples appear to be integers. If anyone has pymodbus experience, or if you think I should abandon pymodbus and go to minimalmodbus or modbus-tk, I would appreciate any help/advice.