Pymodbus (Serial) over a tcp serial connection - python

I will be creating a connection between my Linux server and a cellular modem where the modem will act as a server for serial over TCP.
The modem itself is connected to a modbus device (industrial protocol) via an RS232 connection.
I would like to use pymodbus to facilitate talking to the end modbus device. However, I cannot use the TCP modbus option in PyModbus as the end device speaks serial modbus (Modbus RTU). And I cannot use the serial modbus option in Pymodbus as it expects to open an actual local serial port (tty device) on the linux server.
How can I bridge the serial connection such that the pymodbus library will see the connection as a local serial device?

There is no straightforward solution to trick your linux server into thinking that a MODBUS RTU is actually of MODBUS TCP connection.
In all cases, your modem will have to transfer data from TCP to serial (and the other way around). So I assume that:
1) somehow you can program your modem and instruct it to do whatever you want
2) the manufacturer of the modem has provided a built-in mechanism to do that.
If 1): you should program your modem so that it can replace TCP ADUs by RTU ADUs (and the other way around) when copying data from the TCP connection to the RS link.
If 2): simply provide your RTU frame to whatever API the manufacturer devised.

I actually was working on something similar and decided to make my own Serial/TCP bridge. Using virtual serial ports to handle the communication with each of the modems.
I used the minimalmodbus library although I had to modify it a little in order to handle the virtual serial ports.
I hope you solved your problem and if you didn't I can try to help you out.

Related

python communication MODBUS TCP/IP raspberry pi and HMI - write value

I'm French student and i need your help in Python for my program.
I've got a program in my rapsberry in Python which acquire datas from temperature and hygrometry sensors.
I need to communicate these values to an Human Machine Interface supervisor by the MODBUS TCP/IP PROTOCOL to display and made some graphics of these values in my HMI
The IP adress of the raspberry : 172.16.0.2
The IP adress of the HMI : 172.16.0.10
I think I need to use package like pyModbusTCP or things like that but I don't understand how to use it.
Could you help me to understand how I made the communication between my Rpi and my HMI and for example how I could write the integer value 100 at the address index 1?
Thanks for all!
Antoine
MODBUS is a single-master protocol, meaning that there can only be one master, the rest of attached devices are slaves ( http://www.ni.com/white-paper/52134/en/ , similar to USB protocol, called host and device there ). In addition in MODBUS protocol a slave never starts a communications, slaves only answer to requests. Consequentially the machine on that your HMI runs has to be the MODBUS master /client ( this naming convention is a bit senseless )
For a quick general overview read this https://www.reddit.com/r/PLC/comments/7bqppu/using_raspberry_pi_as_modbus_slave_and/ and http://www.simplymodbus.ca/TCP.htm
pymodbus client / master on HMI machine
From this master / client you can send requests to the RPi ( MODBUS slave / server ) with its sensors using the following code ( if one of the sensors stores its data in a register that is presented to the bus as coil 1 by the pymodbus server that runs on the RPi, see below ). This is only an example there are other data blocks in MODBUS, namely Coils, Discrete Inputs, Input Registers, Holding Registers which of them you use depends on how you configure the MODBUS server on the RPi, normally Discrete Inputs and Input Registers are rarely used :
client = ModbusTcpClient('172.16.0.2')
client.write_coil(1, True)
result = client.read_coils(1,1)
print(result.bits[0])
client.close()
https://github.com/riptideio/pymodbus
pymodbus server / slave on Rpi
For this to work on the RPi has to run software ( pymodbus server ) that enables it as MODBUS slave / server and the sensors have to write their values into specific memory locations that are presented to MODBUS as coils / registers. How this can be done is in https://www.youtube.com/watch?v=p3Dgd0PDjnU and https://jacekhryniewicz.wixsite.com/website/raspberry-pi-with-modbus-tcp ( is a bit outdated )
In https://github.com/riptideio/pymodbus/blob/master/examples/common/asynchronous_server.py is a working example of a MODBUS server that has to run onthe RPi ( read the comments, especially the lines following # initialize your data store )
The word coils has its origin in the past of MODBUS protocol which was developed when electromechanical relays with coils were used in automation technology

Python read modbus over TCP

I have a modbus device and have connected a modbus RTU to ethernet converter ( and not modbus RTU to modbus TCP converter ).
All modules I have come across can read normal Modbus RTU, Modbus TCP, Modbus ASCII. But I haven't seen any module to read modbus through ethernet port.
When I tested using ModScan, I can see the data when I select Remote TELNET Server.
Is there a way I can read this data using python ??
That's a common case, the devices are remote serial/tcp converters. MOXA has tons of then.
You should understand that:
'modbus rtu' - this is serial modbus, contains data+crc16
'modbus tcp' - this is TcpHeader[6 bytes] + data.
'modbus rtu over tcp' - this is YOUR case.
Standard modbus tcp/rtu converting devices change not only physics (ethernet/rs485 eg) but also protocol itself, removing tcp header and adding crc.
Simple serial/tcp converters (like you have) do not modify protocol.
You can use your lovely PyModbus after you manually specify rtu-framer for tcp-client.
client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
https://pymodbus.readthedocs.io/en/latest/source/example/synchronous_client.html

Does a Python 3 TCP Socket have to be connected in order to send data? Or not like UDP?

I want to make a TCP Socket that doesn't connect to the host but instead sends data without connecting... Is that possible with the Python 3 Socket module?
TCP sockets always need to be connected before sending data. Establishing the connection involves an actual packet exchange with the peer, i.e. the TCP 3-way handshake. This is also means that the connect can fail if the target cannot be reached. This is not specific to Python but specific to how TCP sockets work.
With UDP a socket can be connected but does not need to be. Connecting a UDP socket essentially just sets the target on the local socket but does not involve any actual data transfer. This also means that the connect will usually not fail even but a later data transfer might not be able to reach the target.

Facing problems with socket programming for video streaming

I have two transceivers which support serial communication (UART). I am using socket programming on python for generating UDP data grams. Is there any way to port these datagrams to the serial port on the transmitter and then back from serial port to UDP datagram on the receiver. I am new to this so some examples would be highly appreciated.
Use the point-to-point protocol (PPP). That's what it was designed to do.
If you're running on Linux or some other UNIX-like operating system, you'll use pppd on both ends of the serial connection.
Most of the articles on setting this kind of thing up date back to the dial-up internet days. Here's an example: http://www.tldp.org/HOWTO/PPP-HOWTO/
Essentially, PPP creates interfaces with IP addresses on both ends of your serial connection. Send packets to the remote interface's IP address, and it tunnels them over the serial link.

Multiple TCP connections in python

I'm trying to write a server for a chat program. I want the server to have a tcp connection with every chat user. Is there way for the server to have multiple tcp connections at the same time without creating a socket for every connection? And if yes, how?
No. Unlike UDP sockets, TCP sockets work are connection-oriented. Whatever data is written into a socket, "magically" appears to come out of the socket at the other end as a stream of data. For that, both sockets maintain a virtual connection, a state. Among other things, the state defines both endpoints of the conntection - IP and port numbers of both sockets. So a single TCP socket can only talk to a single TCP socket at the other end.
UDP sockets on the other hand operate on a per-packet basis (connectionless), allowing you to send a receive packets to/from any destination using the same socket. However, UDP does not guarantee reliability and in-order delivery.
Btw, your question has nothing to do with python. All sockets (except raw sockets) are operating system sockets that work in the same way in all languages.

Categories

Resources