Two Raspi4 different responses on Socket Programming to PC - python

I have 2 Raspberry Pi 4 units.Both have been updated and upgraded and both flashed with rasbian lite. I created a really simply script, on the first raspi which connects to my pc on port 5999 and performs a basic function of grabbing the time and sending that on said port. My pc accepts it, prints it and writes it to a file. The second raspi has the exact same script with same permissions etc and the script does not work. No errors etc returned. I would for example run the server side on my pc, run the client side on the first raspi, works 100%, disconnect and run the same script on second raspi and no response on server side or client side. I have check "netstat -a"command, port listening etc on pc, have changed the port numbers on one and both raspis' but still cannot get the first raspi to send data.
Any similar experiences? Thank you in advance.

Problem solved, sorry guys, thx anyway for reply, omitted the 6 in the LAN IP address in the code on one of the raspberry Pi units.
If anyone wants the code then I will post, client and server working on PI4 units. Just let me know in comments.

Related

pyModbusTCP Raspberry Pi to PC connection eth0

The story so far. I Set up a Server and Client using the very helpful Johannes YouTube tutorial, initially I did this on the Raspberry Pi without any issues using "localhost".
https://github.com/Johannes4Linux/Simple-ModbusTCP-Server/blob/master/Simple_ModbusServer.py
I then attempted to set the Client up on my Windows PC using the Ethernet port and connect to the Server (Pi).
I used the "inet" ip address from the "ifconfig" command within the Pi not the address from the "ipconfig" within windows (they were different).
client = ModbusClient(host="192.168.0.16", port=502, debug=True)
client.open()
connect error
False
I have attempted to ping this address "cmd" from windows but the connection times out.
ping 168.168.0.16
Pinging 168.168.0.16 with 32 bytes of data:
Request timed out.
It feels like I am missing something really obvious. If anyone is able to help me I would really appreciate it.
Ok so it turns out I am a bit of a wally. And many crucial concepts were missing from my implementation.
Debugging the problem using ipconfig/all revealed that the ipV4 = 192.168.4.180(duplicate)
I had set the IP adress on the PC and the Pi, I had forgotten that I had changed the dchpcd.conf file. So the devices were trying to obtain the same address within the network subnet.
The below implementation worked for me:
Setup on Pi
from pyModbusTCP.server import ModbusServer, DataBank
server = ModbusServer(host="192.168.4.181", port=502, no_block=True)
Setup on PC
from pyModbusTCP.client import ModbusClient
client = ModbusClient(host="192.168.4.181", port=502)
client.open()
I found this guide very useful in my networking refresher spell: https://www.ionos.co.uk/digitalguide/server/configuration/provide-raspberry-pi-with-a-static-ip-address/#:~:text=To%20assign%20an%20IP%20address,with%20the%20IPv4%20address%20192.168.
I am happy to delete this but thought I'd leave it here just in case someone had comments or found it useful. As per usual I answer my own question but sometimes just formalising my own ignorance into black and white is useful.

i am sending commands through serial port in python but they are sent multiple times instead of one

i am sending some commands having particular response serially using com port..the commands are kept in a file..i am reading each command through the file line by line and sending it serially over the com port..but when i am seeing it from the receiver end using Magic Terminal(Software)..i found that each command is going multiple times..which i am sending only one time..i have made a code in pycharm..and in the console i am seeing that command is going only once but from the uart receiving end the story is something else..i am stuck with this problem..i have maintain the same baudrate and everything but not able to diagnose the issue..
github link for the code is: https://github.com/AkshatPant06/Akshat-Pant/blob/master/cmd%20list
def recvResponse():
ser.write(serial.to_bytes(intCmd))
time.sleep(1)
data_recv=ser.read(2)
return data_recv
this i have used to receive the 2 byte response..
There seems to be nothing wrong with your code. At least to the extent I could reproduce, it only sends the command once (I tried your function after setting up my serial port in loopback).
I cannot say for sure but it might be that the terminal you're using has two windows, one for input and another one for output and somehow you're getting confused with what is in and out of your port.
One easy way to deal with this kind of issue is to use a sniffer on your port. You can do that combining com0com and Termite on Windows, as I recently explained here.
As you can see there is only one window on this terminal, and after setting up the forwarding you'll everything that comes in and out of your port. That should make it easier to see what your code is writing and reading.
To give you a conventional scenario to apply the sniffer trick you can refer to the following screenshot:
In this case, we have two real serial ports on a computer. On the first (COM9) we are running a Modbus server (you can imagine it as a bunch of memory addresses, each of one storing a 16-bit number). On COM10 we have a client that is sending queries asking for the contents of the first 10 addresses (called registers using the Modbus terminology). In a general use case, we have those ports linked with a cable, so we know (theoretically) that the client on COM10 is sending a data frame asking for those ten registers and the server on COM9 is answering with the numbers stored on those registers. But we are only able to see the contents on the server (left side of the picture) and what the client is receiving (right). What we don't see is what is traveling on the bus (yeah, we know what it is, but we don't know exactly how the Modbus protocol looks like on the inside).
If we want to tap on the bus to see what is being sent and received on each side we can create a couple of virtual ports with com0com and a port forwarding connection with Termite, something like the following screenshot:
Now we have moved our Modbus server to one of the virtual serial ports (COM4 in this case). After installing com0com we got (by default, but you can change names or add more port pairs, of course) a pair of forwarded ports (COM4<-->COM5). Now, if we want to see what is circulating through the ports we open Termite (bottom-right side of the picture) and set up another port forwarding scheme, in this case from virtual port COM5 to the real port COM9.
Finally (and exactly the same as before we were sniffing), we have COM9 connected together with COM10 with a cable. But now we are able to see all data going to and fro on the bus (all those HEX values you see on Termite displayed with the green/blue font).
As you can see, this will offer something similar to what you can do with more professional tools.

Python sockets auto close

I have a client (on windows) that runs in the background; the client connects to the server (on linux) and waits for instructions.
The problem is that the client can receive data at a 10 minutes time frame. After that the data isn't received at the client side.
In the server side it look like the client still connected but the message don't reach it.
I don't know why, but I can't find the cause.
Any suggestions?

Can multiple processes accesss a Linux device serial port?

I am using a Raspberry Pi 3 which talks to an Arduino on /dev/ttyAMA0.
I can talk to the Arduino with minicom bidirectionally. However, a Python based server also wants this port. I notice when minicom is running, the python code can write to serial0 but not read from it. At least minicom reports the python server has sent a message.
How does this serial port handles contention, if at all? I notice running two minicom session to the same serial port wrecks both sessions. Is it possible to have multiple writers and readers if they are coordinated not to act at the same time? Or can there be multiple readers?
Since two minicoms can attempt to use the port and there are collisions minicom must not set an advisory lock on local writes to the serial port. I guess that the first app to read received remote serial message clears it, since serial doesn't buffer. When a local app writes to serial, minicom displays this and it gets sent. I'm going to make this assumed summary
when a local process puts a message on the serial port everyone can
see it and it gets sent to remote.
when a remote message arrives on
serial, the first local process to get it, gets it. The others
can't see it.
for some reason, minicom has privilege over arriving
messages. This is why two minicoms break the message.

Trying to create Python chat

i have started working on python chat, using sockets.
I am now having a problem with connecting many clients to the server, because if they connect to the same port they won't be able to communicate live, because each client would wait in line until the port will be free. Now my idea was to choose (on the server side) how many clients I want first, then open that range of ports using a simple for function and threads. Now my problem is that on my client size I am using try, when the "try" point is connecting to the port. At first I thought if somebody already connected to some port it will throw an error so the client will just jump to next port, but I forgot about that line thing. Any ideas?
Never mind i figures it out. My mistake was that i opened new socket with every thread, while should have opened that once in main() func, then do the accept in the thread. Thank you all

Categories