1

I am new to Python and trying to create a modbus server with pymodbus module. I follow tutorial on link, but when I run script I got an error:

$ python2.7 server1.py    
INFO:pymodbus.server.async:Starting Modbus TCP Server on 192.168.1.100:502    
Traceback (most recent call last):    
File "server1.py", line 83, in <module>    
StartTcpServer(context, address=("192.168.1.100", 502))    
File "/home/pi/.local/lib/python2.7/site-packages/pymodbus/server/async.py",     
line 255, in StartTcpServer    
reactor.listenTCP(address[1], factory, interface=address[0])    
File "/home/pi/.local/lib/python2.7/site-     
packages/twisted/internet/posixbase.py", line 495, in listenTCP    
p.startListening()    
File "/home/pi/.local/lib/python2.7/site-packages/twisted/internet/tcp.py",     
line 1363, in startListening    
raise CannotListenError(self.interface, self.port, le)    
twisted.internet.error.CannotListenError: Couldn't listen on     
192.168.1.100:502: [Errno 13] Permission denied.    

Anyone can help me solve my problem, Thanks in advance.


[UPDATE]:

$ sudo python2.7 server1.py    
Traceback (most recent call last):    
File "server1.py", line 12, in <module>     
from pymodbus.server.async import StartTcpServer    
ImportError: No module named pymodbus.server.async    

$ python -V    
Python 2.7.13

$ sudo python server1.py
Traceback (most recent call last):
File "server1.py", line 12, in <module>
from pymodbus.server.async import StartTcpServer
ImportError: No module named pymodbus.server.async    

I tried "sudo" already and got an error, it looks like when I use "sudo" the modules isn't on right spot...

zero323
  • 322,348
  • 103
  • 959
  • 935
  • Please put on the `sudo python -V` result, on your question as an UPDATE – Benyamin Jafari Oct 23 '18 at 17:27
  • Also put on `which python` and `sudo which python` result on your question. – Benyamin Jafari Oct 23 '18 at 17:30
  • You will have to check your `PYTHONPATH` and set it appropriately when using `sudo` or if you are not restricted to use PORT 502, you can edit the file and use another port > 1024 , which doesn't require super user permissions. – Sanju Oct 24 '18 at 03:51
  • Found that my python modules are located at `$/.local/lib/python2.7/site-packages` so i need to change shebang from `#!/usr/bin/env python` to `#!/.local/lib/env python` and move my script to `$/.local/lib/python2.7` – Tilen Špacapan Oct 25 '18 at 10:05
  • @TilenŠpacapan I updated my answer. – Benyamin Jafari Oct 30 '18 at 09:14
  • @TilenŠpacapan I've added [UPDATE 2] in my answer to deal with your new problem: `No module named pymodbus.server.async` which is due to different pymodbus version. – Benyamin Jafari Mar 19 '20 at 09:03

1 Answers1

0

Try with sudo permission:

$ sudo python2.7 server1.py 

Or if your python is linked to the Python 2.7 as the following:

$ python -V
Python 2.7.14

Then try it:

$ sudo python server1.py

[UPDATE]:

The following lines result must be same:

$ which python
/usr/bin/python

$ sudo which python
/usr/bin/python

If your codes run in the same machine, you can use localhost or 127.0.0.1 IP instead of machine IP.


[NOTE]:

If your OS is *nix system based, and you have the ufw firewall, doing the following command:

$ sudo ufw disable

[UPDATE 2]:

To dealing with this error ImportError: No module named pymodbus.server.async

you should use

import pymodbus.server.asynchronous

instead of

import pymodbus.server.async 

in the new version of pymodbus library.

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150