Pymodbus and async serial client - python

I'm trying to read/write some registers to some modbus devices. My software uses Python 3.4 and Twisted, so I wanted a library that integrates with such stack and allows async communications.
I'm trying to use pymodbus to implement a modbus serial client, but the library doesn't seem to offer a ModbusSerialClient anymore?
The following code:
from pymodbus.client.async import ModbusSerialClient as ModbusClient
Will raise an ImportError on Python 3.4 with pymodbus 1.4.0.
Standard examples use ModbusClient with connectTCP, but Twisted doesn't offer a serial endpoint yet.
I've seen there's a StartSerialServer, but it's not clear to me whether and how I could use it.
I'd like to either get a syntax for reading/writing registers via pymodbus, or have suggestion for another working library, as long as it works on Linux with a tty, Python 3.x and Twisted.

You can connect to a serial port using Twisted like this:
from twisted.internet.serialport import SerialPort
from twisted.internet import reactor
port = SerialPort(protocol, deviceName, reactor)
pymodbus offers a modbus protocol. So in the above, protocol should be:
from pymodbus.client.async import ModbusClientProtocol
protocol = ModbusClientProtocol()

Related

Twisted python - socket to bind to path on the filesystem

I am trying to implement a socket server that receives rsyslog messages from windows or linux os. Ryslog uses omuxsock to output logs to socket server. I want to use twisted python to implement this socket that receives windows event/linux syslog messages. All the examples i've come across documentation only shows how to bind to IP and port. I want twisted socket to bind to a socket file not ip or port. eg., file "/var/syslog.sock"
in python without using twisted this can be done using:
s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
s.bind("/var/syslog.sock")
has anyone implemented socket in twisted python using socket file?
With a reactor that supports IReactorUNIX you can do this either with the lower-level listenUNIX or the higher level UNIXServerEndpoint.
For example
from twisted.internet import reactor
from twisted.internet.endpoints import UNIXServerEndpoint
from twisted.internet.protocols import Factory, Protocol
e = UNIXServerEndpoint(reactor, "/var/syslog.sock")
d = e.listen(Factory.forProtocol(Protocol))
reactor.run()

Inter-Process Communications between Python and C++ using local file sockets?

I have a c++ program which creates a local socket bound to "/tmp/.mysocket" and waits to receive data from that socket. The way it is set up, raw binary data would be sent to the socket and loaded into the following C++ structure:
struct StateVariable
{
char Name[64];
int E[8];
};
The C++ program listens for input using recvfrom:
int nBytes = recvfrom(SD,&DataReceived,sizeof(StateVariable)/sizeof(char),0,(sockaddr*)&SentFrom,&size);
The server is just using the standard AF_UNIX and SOCK_DGRAM to create the socket.
My questions is relating to python: How do I send data to the local socket bound to /tmp/.mysocket using python? I am not using AF_INET or opening a specific port for this.
I can use python's socket library to describe the socket, but I can't find any resource that discusses binding a socket to a file in python and sending data to that socket. The documentation for the socket library only discusses using AF_INET and SOCK_DGRAM for local sockets bound to a port number at 127.0.0.1, but I'm not doing that.
How do I get python to send data to a socket bound to a file? Is there an example python program that does just that (maybe a client/server pair that demonstrates this functionality)? As long as I can get python to send data to a local file socket, I can figure out the rest.
The Python socket library is a fairly thin wrapper around the socket interface you're familiar with from C++. See socket documentation.
It's going to look something like this:
import socket
import struct
s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
s.connect('/tmp/...')
s.send(struct.pack('64s8i', ...))
You just change AF_INET to AF_UNIX and then .connect(address) and/or .bind(address) methods accept path to a file (instead of (host, port) pair). Other then that everything else works pretty much the same.

Write modbus register with pymodbus

I'm new to programming.
I need to write modbus registers using python and I have no experience with pymodbus.
What is the way to actually start and communicate with a modbus server?
Im able to read the registers from this modbus server using a software I found on the internet.
How can I read the registers and write to them using pymodbus module? I literally dont know where to start.
Thanks for the answers!
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
try:
client = ModbusClient(host='IP',port='502')
request = client.read_holding_registers(0x00,5,unit=1)

Inter-process communication between Python and Scala programs

I have a website that is backed by CherryPy web framework and a scala program that runs on a same machine and contains an actor system inside. OS is Ubuntu 12.04.
What I want is this: once user fills out and submits a form I send the data from CherryPy backend to scala program as a JSON string. How can this be done? What should I use in my python and scala programs to implement this functionality?
Instead of using raw sockets, you could consider a message broker like RabbitMQ. It supports both Scala and Python.
http://www.rabbitmq.com/tutorials/tutorial-one-python.html
On the Scala side, Akka has an AMQP module which abstracts AMQP Connection, Producer and Consumer as Actors.
http://doc.akka.io/docs/akka-modules/1.3.1/modules/amqp.html
As you are on a *nix system, you may want to look into Unix domain sockets (that link contains a very clear example use).
You can use the python socket module to easily create a Unix socket using:
import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

Twisted and PyBluez working together?

I want to use twisted and bluetooth together. At the moment I am doing this with PyBluez running in an twisted thread.
PyBluez just creates some socket (or socket-like? it has a file descriptor like a normal socket) object, basically you do:
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((device_id,1))
Can't I just insert that socket into the twisted reactor somehow and connect it with a Protocol?
You can write a class implementing IReadDescriptor (or IWriteDescriptor) and connect it to reactor, like in this example.
I found this project which combines pybluez with twisted: http://pydoc.net/airi/0.1.1/airi.twisted_bluetooth
This piece of code actually helped me a lot. I now have a working implementation in Twisted.

Categories

Resources