How to integrate different modbus devices on a web system? - python

I have two PLC modbus devices. These two are different companies.
The first one is that A will return the temperature and humidity. The second is that B will return five values. In both devices, the connection is modbus TCP.
One problem is that the same test software can be connected but the other cannot be connected. One device can be connected using pyModbus, but the other device is not working.
I also tested some software and python libraries. List is as follows
Device A
Software
modbus Poll (OK)
ModScan32 (NO)
Python Lib
pyModbus (OK)
EasyModbus (NO)
Device B
Software
modbus Poll (NO)
ModScan32 (OK)
Python Lib
pyModbus (NO)
EasyModbus (OK)
I don't know the difference between the two devices.
I want to integrate these two into the web system for monitoring, and new devices can be added in the future.
If there is a third device, I have to retest the connection to see which Python lib is available.
How can I implement this?
Learn more about the communication format of PLC devices?
Write a set of Python Libs for all devices?
Thank You.

Use one library for each, save the data somewhere centralized and view it with a different app. I don't see the problem. Also, have you tried re-trying the connection or reading after a few seconds? In my experience, modbus devices fail every so often to answer (or maybe is a library problem, I don't know) and a retry usually works.

For this, and it sounds like any good IOT framework would solve it, you should send the data from each device separately to one central point, like an IOT cloud solution, then you can build a webapp to view whatever data you have on the IOT framework, regardless of when or if it came in. Most frameworks will show you the old data as well if its not connected, so you can still display data, indicating it is stale or something to your user.

Related

Socket over internet Python Embedded

I have an embedded system on which I can connect to internet. This embedded system must send sensor data to PC client.
I put a socket client using python on my PC. I put a socket server ( using C++ language on the embedded system because you can only use C++ ).
I can succesfully connect from my PC to the embedded system using the sockets and send and recieve whatever I want.
Now, the problem is I use local IP to connect to the system and both of them must be connected to the same Wifi router.
In the real application, I won't know where the embedded system is in the world. I need to get to it through internet, because it will be connectet to internet through 4g.
My question is, how can I connect to it through internet, if the embedded system is connected to internet using 4G?
Thank you
Realistically in typical situations, neither a PC nor an embedded device hanging off a 4g modem will likely have (or should be allowed) to have externally routable addresses.
What this practically means is that you need to bounce your traffic through a mutually visible relay in the cloud.
One very common way of doing that for IoT devices (which is basically to say, connected embedded devices) is to use MQTT. You'll find support in one form or another for most computing platforms with any sort of IP networking capability.
Of course there are many other schemes, too - you can do something with a RESTful API, or websockets (perhaps as an alternate mode of an MQTT broker), or various proprietary IoT solutions offered by the big cloud platforms.
It's also going to be really key that you wrap the traffic in SSL, so you'll need support for that in your embedded device, too. And you'll have to think about which CA certs you package, and what you do about time given its formal requirement as an input to SSL validation.
I think your problem is more easily solved if you reverse the roles of your embedded system and PC. If you are communicating to a device using IP protocols across cellular networks, it is much easier to have the device connect to a server on the PC rather than the other way around. Some networks/cellular modems do not allow server sockets and in any case, the IP address is usually dynamically allocated and therefore difficult to know. By having the device connect to a server, it "knows" the domain name (or IP address) and port to which it should make the connection. You just have to make sure that there is indeed a server program running at that host bound to some agreed upon port number. You can wake up the device to form the connection based on a number of criteria, e.g. time or amount of collected data, etc.

Bluez DBus: client server implementation

I need help to setup my project. In particular, my project is a C/C++ project.
This project involves different components that must communicate exploiting Bluetooth Low Energy.
Components:
The components can be classified into two different categories: a mobile component (such as a tablet android or an Ipad) and a raspberry component (a Raspberry Pi W board with a BLE adapter). In the system, there can be 1 mobile component and from 1 to N raspberry components
Desired Behavior:
According to the project requirements, the Raspberry components should notify their presence in order to be detected from the tablet. The tablet has to scan for nearby raspberry and eventually connect to one of the scanned devices. Once the connection is established the tablet has to send at least two strings to the raspberry.
I downloaded the last version of Bluez and I saw the python scripts in the test folder: example-gatt-server and example-advertising. Unfortunately, I have no experience with python. So I looked for an equivalent C code but didn't find anything.
Can anyone help me to find a GATT server in C able to advertise and receive connection request?
Is it correct for my requirements to implement the code for the Raspberry as the one in the gatt-server-example?
Thanks
[EDIT]:
My boss agrees with me to use these python scripts instead of using some C/C++ equivalent. As said before I have no experience using python, so I need some help.
Which types of data are accepted from the gatt-server? Can I pass a string to it?
If I want to modify two of the implemented services (the ones identified using UUID 12345678-....) to pass a string can I do this?
In the gatt-server example, there is a service that seems to deal with the encryption mechanism. If I try to send the string "goofy" is this encrypted, sent and decrypted once received by the gatt-server?
Thanks

How to connect raspberry to plc via Ethernet?

I have wrote a simple program using Python on RSPPi, that count time between the falling edges, signal is provided by PLC (Rexroth CML40). It works great, but now I want to send more signals from PLC but I don't have any free output spots. So, I think the solution to that is making a connection between Raspberry and PLC through Ethernet. My question is, what should i need to make to receive data from PLC (state of the 3 variables) to Raspberry?
I found this https://wiki.python.org/moin/TcpCommunication but I don't really understand it. Is it Raspberry a server? Right? If yes then how can I make client code work on PLC... It is really incomprehensible to me
You first need to find what Ethernet-based protocols the L40 controller has available. This sales guide shows that some of the IndraMotion software packages include OPC-UA, which is a good option because there is a robust Python OPC-UA library. Your controller may also have the EtherNet/IP add-on module, which would also work. Your distributor or sales rep should be able to point you in the right direction.
The second step is to find a library that allows to you communicate over that protocol. I've personally had success with OPC-UA, many options though. Some Recommendations:
Python OPC-UA
CPPPO — EtherNet/IP

Modbus sniffing using python

I have 2 devices communicating with each other using Modbus and I would like to sniff this communication line using Python for logging purposes. I have connected my computer to the communication bus via a Modbus dongle, but I don't know how I can actually read the data.
I have used Pymodbus before, but I don't think that would work in this case since it only allows for master or slave devices (as far as I know)
Are there any other libraries that I can use for my purpose? Would it be possible for me to implement something like a read only socket on the COM port that doesn't interfere with existing communication on the bus?
Thank you!
You can try a MODBUS simulator like this one: It has an option to see the activity on the bus.
If you want to use another python MODBUS I recommend using modbus_tk. It has an example on how to create an MODBUS simulator. But to be able sniff the packets sent to other devices you will need to do some modifications to disable the automatic response.
If the only thing you want to do is to see the activity on the bus I recommend the first option.
I hope this helps you.
[Edit]:To be more specific you will need to download the following software : Modbus Poll - MODBUS slave simulator(which works on Windows) and plug your MODBUS dongle in the port you intend to use. After you do all the settings for the serial communications go to the Display tab and click on Communication. You will be able to see the traffic on the line.

Stop packets at the network card

This is the problem I'm trying to solve,
I want to write an application that will read outbound http request packets on the same machine's network card. This would then be able to extract the GET url from it.On basis of this information, I want to be able to stop the packet, or redirect it , or let it pass.
However I want my application to be running in promiscuous mode (like wireshark does), and yet be able to eat up (stop) the outbound packet.
I have searched around a bit on this..
libpcap / pcap.h allows to me read packets at the network card, however I haven't yet been able to figure out a way to stop these packets or inject new ones into the network.
Certain stuff like twisted or scapy in python, allows me set up a server that is listening on some local port, I can then configure my browser to connect to it, using proxy configurations. This app can then do the stuff.. but my main purpose of being promiscuous is defeated here..
Any help on how I could achieve this would be greatly appreciated ..
I'd suggest that you approach this at the application layer and use a transparent proxy (e.g. squid) and iptables based interception of outbound port-80 traffic.
The reason I suggest this is that that it will avoid issues with the request being split between packets.
However, if you still want to go ahead with packet interception, you can do it in userspace using netfilters in netlink. I believe there are python wrappers for libnl around.
Essentially you create an iptables rule pointing to "QUEUE" for the traffic you want to intercept and write a program using a netlink library to process the queue, accepting, rejecting and/or modifying packets.
Using pcap you cannot stop the packets, if you are under windows you must go down to the driver level... but you can stop only packets that your machine send.
A solution is act as a pipe to the destination machine: You need two network interfaces (without address possibly), when you get a packet that you does not found interesting on the source network card you simply send it on the destination network card. If the packet is interesting you does not send it, so you act as a filter. I have done it for multimedia performance test (adding jitter, noise, etc.. to video streaming)
You are confusing several things here:
"Promiscuous" usually refers to a mode of a hardware ethernet network card where it delivers all packets in its collision domain up to the kernel network stack and have it sort out delivery (vs. just unicast to given MAC, subscribed multicast, and broadcast in normal operating mode of the card). This is on the receive path.
All the data outbound from your machine will go through (one of) the network cards on the machine, so "promiscuous" does not at all apply here.
You are working on filtering TCP-based protocol (HTTP), but talk in terms of packets. This is wrong. TCP connection is a stream that could be (as far as socket readers and writers are concerned) arbitrarily split into IP datagrams. That URL from HTTP request header could be split across multiple link-layer frames. You would have to stitch them back together and parse the stream anyway. Then you have no chance even at that if SSL is in use.
If you are interested in HTTP filtering then read HTTP RFCs, and read existing open-source code, e.g. squid, nginx, etc.
If you are digging through network stack for better understaning then read W. Richard Stevens books, look into existing code in open-source operating systems, check out BPF and netlink.
Hope this clears it a little.
I have implemented this module in Windows by using two separate NICs and using a socket/pipe(whatever you like) between them in this thread

Categories