|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Python Socket Between Devices
Happy to help.
Are you running the python client program on the driver station laptop? If not, then I don't think you need to worry. If so, checkout R60, which has been pretty consistent for the past few years. That will tell you what ports you can use for what. |
|
#2
|
||||
|
||||
|
Re: Python Socket Between Devices
I am running it on the Driver Station and I know about R60 but the client picks a random port to respond on. How do I fix that?
Also I know all the IPs it could be but is there a way to know immediately what ip or use s.connect much faster? You are honestly Awesome. |
|
#3
|
||||
|
||||
|
Re: Python Socket Between Devices
Quote:
Code:
ip addr show Quote:
If it's working now (despite taking a time to connect) you should be fine. |
|
#4
|
||||
|
||||
|
Re: Python Socket Between Devices
I can figure out the IP no problem if I have direct access to it. But I was wondering if theirs an easy way to find it on the FMS. I know about 10.17.92.X but the X is the part I dont know. I currently have code to look through all of the possible ones and I assume that it will probably be in the lower numbers but currently it takes a long time to look through the IP addresses.
And then for the Port thing the Server is on port 5800 which is listed on R60 but the client will reply on something around 57869 which isnt open on the FMS will this be an issue? When I did s.bind it solved this issue but wont connect but now it will pick random port and connect that way. |
|
#5
|
||||
|
||||
|
Re: Python Socket Between Devices
Like I said, give your Pi a static IP address, or just replace 10.TE.AM.XX with raspberrypi.local.
That will definitely be an issue. You're right, putting bind() back in the client will fix it. Here's my code that I tested this with. Uses python 3, so might be a little different than yours. Client Code:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 5800)) # Bind to empty IP so it can use anything
s.connect(("192.168.1.3", 5800)) # 192.168.1.3 is the IP of my server
s.send(b"hello")
s.close()
Code:
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 5800)) # Again, bind to any address, but this specific port
# become a server socket
serversocket.listen(1)
(client, addr) = serversocket.accept()
print("Recieved connection from {}".format(addr))
while True:
data = client.recv(1024)
if len(data) == 0: # According to the spec, empty recieve means that the socket is closing
break
print("recv {}".format(data))
client.close()
|
|
#6
|
||||
|
||||
|
Re: Python Socket Between Devices
Thank you So Much for All of your help everything works great now!
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|