serial sync server not working

2,074 views
Skip to first unread message

anana

unread,
Oct 6, 2011, 12:18:08 PM10/6/11
to pymodbus
I started the server as follows and I am polling it with Modbus Poll:
serial port, ASCII mode, 9600, 8N1. But the server will not answer
(function code 04, Slave ID 1, Address init 0, 10 registers)
Did I miss initializing any parameter? I also don't see any logging.


The unit ID used by the client does not meter, if our context is
single, isn't it? If I wanted to use an ID for my server, I would have
to initialize it as you explained in post "
Synchronous Modbus TCP Server":
(slaves={0:c1, 1:c2}, single=False)

In this case, with ID=0 the client would access context c1. Is that
right?

Thanks


#!/usr/bin/env python

#---------------------------------------------------------------------------
#
# the various server implementations
#---------------------------------------------------------------------------
#
#from pymodbus.server.sync import StartTcpServer
#from pymodbus.server.sync import StartUdpServer
from pymodbus.server.sync import StartSerialServer

from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
import serial

#---------------------------------------------------------------------------
#
# configure the service logging
#---------------------------------------------------------------------------
#
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------
#
# initialize your data store
#---------------------------------------------------------------------------
#
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [17]*100),
co = ModbusSequentialDataBlock(0, [17]*100),
hr = ModbusSequentialDataBlock(0, [17]*100),
ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------
#
# run the server you want
#---------------------------------------------------------------------------
#
#StartTcpServer(context)
#StartUdpServer(context)
print "start serial server"
StartSerialServer(context, port="/dev/ttyS4",
bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE,
baudrate=9600, parity=serial.PARITY_NONE)

anana

unread,
Oct 11, 2011, 11:34:39 AM10/11/11
to pymodbus
Hello again, I came forward but still does not work.

I initialised the UnitID and use RTU framing. (ModbusRTU, 9600 8N1)

The server is not answering. I am polling the server (serial port /dev/
ttyUSB1) with Modbus Poll in another computer. I sniffed the data
received by the server on that port:
2011-10-11 14:19:20.762784:
01 03 00 00 00 0A C5 CD


python modbus_serial_sync_server.py
StartSerialServer {'parity': 'N', 'baudrate': 9600, 'bytesize': 8,
'timeout': 10.0, 'stopbits': 1, 'port': '/dev/ttyUSB1'}
DEBUG:root:Started thread to serve client
DEBUG:pymodbus.server.sync:Client Connected [127.0.0.1:0]
DEBUG:pymodbus.server.sync:Client Disconnected [127.0.0.1:0]




What does this mean? Why is the ModbusRequestHandler not running any
more?


modbus_serial_sync_server.py

#!/usr/bin/env python

from pymodbus.server.sync import ModbusSerialServer, StartSerialServer


from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
import serial, socket
from pymodbus.constants import Defaults
from pymodbus import transaction

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------
#
# initialize your data store
#---------------------------------------------------------------------------
#
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [17]*100),
co = ModbusSequentialDataBlock(0, [17]*100),
hr = ModbusSequentialDataBlock(0, [17]*100),
ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)


class Server(ModbusSerialServer):

def _connect(self):
''' Connect to the serial server

:returns: True if connection succeeded, False otherwise
'''
if self.socket: return True
try:
self.socket = serial.Serial(port=self.device,
timeout=self.timeout,
bytesize=self.bytesize, stopbits=self.stopbits,
baudrate=self.baudrate, parity=self.parity)
except serial.SerialException, msg:
log.error(msg)
raise
self.close()
return self.socket != None

def serve_forever(self):
''' Callback for connecting a new client thread

:param request: The request to handle
:param client: The address of the client
'''
log.debug("Started thread to serve client")
handler = self._build_handler()
while True: handler.handle()

def _build_handler(self):
''' A helper method to create and monkeypatch
a serial handler.

:returns: A patched handler
'''
request = self.socket
request.send = request.write
request.recv = request.read
handler = ModbusRequestHandler(request, ('127.0.0.1',
self.device), self)
return handler


def StartSerialServer(context=None, identity=None, **kwargs):
''' A factory to start and run a udp modbus server

:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
'''
print "StartSerialServer", kwargs
framer = transaction.ModbusRtuFramer
server = Server(context, framer, identity, **kwargs)
server.serve_forever()


Defaults.UnitId = 0x01
StartSerialServer(context, port="/dev/ttyUSB1",
bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE,
baudrate=9600, parity=serial.PARITY_NONE,
timeout=10.0)

anana

unread,
Oct 12, 2011, 11:05:09 AM10/12/11
to pymodbus, bash...@gmail.com
Did anybody manage to use the serial synchronous server? Could you
give me some advice?

I tried to poll it with pymodbus serial synchronous client and the
same configuration but it is still not working:

#!/usr/bin/env python
'''
Pymodbus Synchrnonous Client Examples
--------------------------------------------------------------------------

The following is an example of how to use the synchronous modbus
client
implementation from pymodbus.

It should be noted that the client can also be used with
the guard construct that is available in python 2.5 and up::

with ModbusClient('127.0.0.1') as client:
result = client.read_coils(1,10)
print result
'''
#---------------------------------------------------------------------------
#
# import the various server implementations
#---------------------------------------------------------------------------
#
#from pymodbus.client.sync import ModbusTcpClient as ModbusClient
#from pymodbus.client.sync import ModbusUdpClient as ModbusClient
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import serial

#---------------------------------------------------------------------------
#
# configure the client logging
#---------------------------------------------------------------------------
#
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------
#
# choose the client you want
#---------------------------------------------------------------------------
#
# make sure to start an implementation to hit against. For this
# you can use an existing device, the reference implementation in the
tools
# directory, or start a pymodbus server.
#---------------------------------------------------------------------------
#
client = ModbusClient(method='rtu', port="/dev/ttyUSB2",
bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE,
baudrate=9600, parity=serial.PARITY_NONE)

#---------------------------------------------------------------------------
#
# example requests
#---------------------------------------------------------------------------
#
# simply call the methods that you would like to use. An example
session
# is displayed below along with some assert checks.
#---------------------------------------------------------------------------
#
rq = client.write_coil(1, True)
rr = client.read_coils(1,1)
assert(rq.function_code < 0x80) # test that we are not an error
assert(rr.bits[0] == True) # test the expected value

rq = client.write_coils(1, [True]*8)
rr = client.read_coils(1,8)
assert(rq.function_code < 0x80) # test that we are not an error
assert(rr.bits == [True]*8) # test the expected value

rq = client.write_coils(1, [False]*8)
rr = client.read_discrete_inputs(1,8)
assert(rq.function_code < 0x80) # test that we are not an error
assert(rr.bits == [False]*8) # test the expected value

rq = client.write_register(1, 10)
rr = client.read_holding_registers(1,1)
assert(rq.function_code < 0x80) # test that we are not an error
assert(rr.registers[0] == 10) # test the expected value

rq = client.write_registers(1, [10]*8)
rr = client.read_input_registers(1,8)
assert(rq.function_code < 0x80) # test that we are not an error
assert(rr.registers == [10]*8) # test the expected value

rq = client.readwrite_registers(1, [20]*8)
rr = client.read_input_registers(1,8)
assert(rq.function_code < 0x80) # test that we are not an error
assert(rr.registers == [20]*8) # test the expected value

#---------------------------------------------------------------------------
#
# close the client
#---------------------------------------------------------------------------
#
client.close()




thank you,

ana

Albert Brandl

unread,
Oct 18, 2011, 7:15:24 AM10/18/11
to pymo...@googlegroups.com
Hi!


On Wed, Oct 12, 2011 at 08:05:09AM -0700, anana wrote:
> Did anybody manage to use the serial synchronous server? Could you
> give me some advice?

I 've been using the trunk version of the _asynchronous_ serial server
without any problems for quite some time now. Which version of pymodbus
did you install?

Best regards,
--
Albert Brandl
Weiermayer Solutions GmbH      | Abteistraße 12, A-4813 Altmünster
phone: +43 (0) 720 70 30 14    | fax: +43 (0) 7612 20 3 56
web: http://www.weiermayer.com




A-4813 Altmünster - Abteistraße 12 - web: www.weiermayer.com - phone: +43761287677 - fax: +43761220356 - mobile: +436642442777

anana

unread,
Oct 18, 2011, 9:48:29 AM10/18/11
to pymodbus
Hello Albert,

I would need the synchronous serial server, not the asynchronous. Did
you also use it?

Thank you anyway,

ana

On Oct 18, 1:15 pm, Albert Brandl <albert.bra...@weiermayer.com>
wrote:
> Hi!
>
> On Wed, Oct 12, 2011 at 08:05:09AM -0700, anana wrote:
> > Did anybody manage to use the serial synchronous server? Could you
> > give me some advice?
>
> I 've been using the trunk version of the _asynchronous_ serial server
> without any problems for quite some time now. Which version of pymodbus
> did you install?
>
> Best regards,
> --
> Albert Brandl
> Weiermayer Solutions GmbH      | Abteistraße 12, A-4813 Altmünster
> phone: +43 (0) 720 70 30 14    | fax: +43 (0) 7612 20 3 56
> web:http://www.weiermayer.com
>
>
> A-4813 Altmünster - Abteistraße 12 - web:www.weiermayer.com- phone: +43761287677 - fax: +43761220356 - mobile: +436642442777
>
>  image_jpeg_part
> 57KViewDownload

Albert Brandl

unread,
Oct 19, 2011, 3:24:44 AM10/19/11
to pymo...@googlegroups.com
Hi!


On Tue, Oct 18, 2011 at 06:48:29AM -0700, anana wrote:
> I would need the synchronous serial server, not the asynchronous. Did
> you also use it?

No, I didn't. I did some tests with the synchronous serial client, as far as
I can remember. There have been several changes to the RTU implementation -
it would be a good idea to use the trunk version of the code.


Best regards,
--
Albert Brandl
Weiermayer Solutions GmbH      | Abteistraße 12, A-4813 Altmünster
phone: +43 (0) 720 70 30 14    | fax: +43 (0) 7612 20 3 56
web: http://www.weiermayer.com




A-4813 Altmünster - Abteistraße 12 - web: www.weiermayer.com - phone: +43761287677 - fax: +43761220356 - mobile: +436642442777

Bashwork

unread,
Oct 19, 2011, 2:35:33 PM10/19/11
to pymodbus
Ana,

Okay after testing it appears that there is a bug with the synchronous
version, the asynchronous is good to go however and I will add some
functional tests to automate the testing. As for the synchronous, I
will have to do a little work. The problem stems from me trying to
reuse the multi-client connected server TCP implementation with the
disconnected single-client serial server. Long story short, it isn't
working correctly. Sorry to leave you waiting for so long, but I will
see if I can get correct this in short order.

Galen

On Oct 18, 8:48 am, anana <anro...@hotmail.com> wrote:
> Hello Albert,
>
> I would need the synchronous serial server, not the asynchronous. Did
> you also use it?
>
> Thank you anyway,
>
> ana
>
> On Oct 18, 1:15 pm, Albert Brandl <albert.bra...@weiermayer.com>
> wrote:
>
>
>
>
>
>
>
> > Hi!
>
> > On Wed, Oct 12, 2011 at 08:05:09AM -0700, anana wrote:
> > > Did anybody manage to use the serial synchronous server? Could you
> > > give me some advice?
>
> > I 've been using the trunk version of the _asynchronous_ serial server
> > without any problems for quite some time now. Which version of pymodbus
> > did you install?
>
> > Best regards,
> > --
> > Albert Brandl
> > Weiermayer Solutions GmbH      | Abteistraße 12, A-4813 Altmünster
> > phone: +43 (0) 720 70 30 14    | fax: +43 (0) 7612 20 3 56
> > web:http://www.weiermayer.com
>
> > A-4813 Altmünster - Abteistraße 12 - web:www.weiermayer.com-phone: +43761287677 - fax: +43761220356 - mobile: +436642442777
>
> >  image_jpeg_part
> > 57KViewDownload

Bashwork

unread,
Oct 19, 2011, 4:06:36 PM10/19/11
to pymodbus
Just updating the thread: this has been fixed in the trunk version of
the code. A little bit more work needs to be done to do this in a
better way (see Issue 54).

Galen

anana

unread,
Oct 24, 2011, 11:34:40 AM10/24/11
to pymodbus
Testing the new version. Until now working fine!

Thanks again,

Ana
> > > > A-4813 Altmünster - Abteistraße 12 - web:www.weiermayer.com-phone:+43761287677- fax: +43761220356 - mobile: +436642442777
>
> > > >  image_jpeg_part
> > > > 57KViewDownload
>
>

Mario Victoria

unread,
Nov 4, 2012, 6:39:07 PM11/4/12
to pymo...@googlegroups.com
Hello,

Do you have some simple example using the asyn version, i have install the python 2.7 on windows 7.

I use the sync version very well, but i need some sample of async version.

Bashwork

unread,
Nov 5, 2012, 3:15:53 PM11/5/12
to pymo...@googlegroups.com
This can be adapted into a simple async serial server, just choose the StartSerialServer method at the bottom:

mubdi

unread,
Mar 13, 2018, 7:44:14 AM3/13/18
to pymodbus
hi i am newbie. try to setup serial rtu server. no error but when i request from another client. no respon. anyone have working version?

mubdi

unread,
Mar 13, 2018, 9:57:11 AM3/13/18
to pymodbus
SYNC SERVER

DEBUG:pymodbus.server.sync:Client Connected [/dev/tty.usbserial-AL030T6E:/dev/tty.usbserial-AL030T6E]
DEBUG:pymodbus.server.sync:Started thread to serve client
DEBUG:pymodbus.server.sync:recv: 0x1 0x3 0x0 0x0 0x0 0x4 0x44 0x9

SYNC CLIENT

DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:send: 0x1 0x3 0x0 0x0 0x0 0x4 0x44 0x9
DEBUG:pymodbus.client.sync:will sleep to wait for 3.5 char
DEBUG:pymodbus.transaction:recv: 
DEBUG:pymodbus.transaction:getting transaction 1
Traceback (most recent call last):
  File "XXX/modbus client.py", line 64, in <module>
    main()
  File "XXX/modbus client.py", line 58, in main
    print result.registers
AttributeError: 'ModbusIOException' object has no attribute 'registers'

Anyone have the same problem? I am using USB-TTL-RS485-RS485-TTL-USB. Check with ping pong serial work and communicate. The same module / setup can query any Modbus RTU such as EASTRON 630 power meter but not pymodbus server
Reply all
Reply to author
Forward
0 new messages