0

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.

Mike
  • 35
  • 1
  • 1
  • 7

2 Answers2

0

Looks like the address is in Hexadecimal: 0x0104 = 256 + 4 = 260

Marker
  • 972
  • 6
  • 9
  • Yes, pymodbus takes what I provided as the address and converts to Hex. What I am trying to figure out is what is the format of the address value. Integer is not working. The address values I received from the supplier we both ascii and hex. What I show here is the ascii. If I switch to RTU mode and provide the hex value, the conversion of the hex looks like elvish. – Mike Feb 21 '18 at 01:18
  • I tried a test and changed the address value to a string. The error code said it was looking for an integer. Now I am really confused on why the translation to hex is messed up. If anybody has used pymodbus I could really use some guidance. – Mike Feb 21 '18 at 01:34
0

I added this as another answer so I could the the formatting correct.

Based on your statement "What I show here is the ascii". The Debug output appears to be an ASCII Modbus packet which is hexadecimal 0–9, A–F (ASCII coded). Decoding it below, everything looks correct, you wanted to write address 260, and that's what it did.

:           start
00          device address = 0 (broadcast)
05          write single coil function (5)
0104        address 0104 (hex) = 1x256 + 4 = 260 (this is what you wanted)
FF00        output value FF00 (hex) = ON
F7          LRC (longitudinal redundancy check)

To me, it all looks correct.

Marker
  • 972
  • 6
  • 9
  • Ok, so if this is ascii coded hex, then what do I use for the address in the call to write to the coil. In the call I use (260, 1, unit=0x00). I get an error if I add anything other than an integer (260). – Mike Feb 21 '18 at 14:28
  • If you need to write to address 0260 (hex) you have two options (1) 0260(hex) = 2*256 + 6*16 + 0 = 608(decimal) OR (2) in python you should just be able to use 0x0260 which is how you represent a number in hex. – Marker Feb 21 '18 at 14:52
  • So you would do (1) client.write_coil(608,1,unit=0x00) OR (2) client.write_coil(0x0260,1,unit=0x00) – Marker Feb 21 '18 at 14:53
  • Marker-You ROCK!!!!! Thank you very much. Nothing in the manuals mentioned converting hex to decimal. I don't have enough "reputation" to give you credit for your help, but please know that I really appreciate it! – Mike Feb 21 '18 at 21:05
  • Mike glad I could help. It is all a learning curve.. the longer you do this stuff the more insight you get and the educated guesses become easier. Never used pymodbus, but I have done a lot with Modbus on C, C++, and C#. – Marker Feb 21 '18 at 21:22