Posts: 10
Threads: 3
Joined: Mar 2017
Mar-30-2017, 01:18 PM
(This post was last modified: Mar-30-2017, 01:48 PM by zivoni.)
Hello All,
I would like to start of by saying that I am new to Python.
I have a Zynq board connected to my system and I would like to transmit and receive messages (echo) between my computer and board using Python Network Program.
I don't know how to proceed on this issue as I tried a few and nothing has yielded me any results.
I tried the loop back program and it is working(the "127.0.0.1" program), but this, i am not sure.
I'm open to all suggestions.
Please help
Moderator zivoni: moved to more appropriate forum
Posts: 3,458
Threads: 101
Joined: Sep 2016
What's a zynq board? Is it similar to raspberry pi or arduino? Do you have a link to the actual board you have? Without knowing what it's capable of, we'd be taking guesses as to how to help.
That said, how is it connected to the system? If you just want to communicate with the computer you're already plugged in to, there might be easier ways than networking.
Posts: 10
Threads: 3
Joined: Mar 2017
Forget all that.
My Board is connected to the System. It's IP address is "192.168.1.10" and port number 7. I know this because thats how I echoed the data from the system to the board using telnet.
What I want to do is, read that port using Python so that instead of just echoing, I want to read the data using the python program as well.
Please help me with this
Posts: 3,458
Threads: 101
Joined: Sep 2016
Cool. Have you tried sockets? https://docs.python.org/3.6/library/socket.html
Something like: import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("192.168.1.10", 7))
listening = True
while listening:
data = sock.recv(1024)
if data:
print(data)
else:
listening = False
sock.close()
Posts: 12,040
Threads: 487
Joined: Sep 2016
Mar-30-2017, 05:44 PM
(This post was last modified: Mar-30-2017, 05:44 PM by Larz60+.)
If it's just an Ethernet connection, you should be able to hook it up through your
OS network software. Then it looks like just another drive.
Otherwise sockets can be used
Posts: 10
Threads: 3
Joined: Mar 2017
Mar-31-2017, 10:49 AM
(This post was last modified: Mar-31-2017, 11:19 AM by adithyakrish.)
Hey,
Thank you so much for your reply.
I tried the program. The program contains no errors, unlike my previous programs.
But it does not display the result at all.
As in, its either not listening, or its not displaying :/
Please help :(
Truly yours,
Adithya Krish
(Mar-30-2017, 05:44 PM)Larz60+ Wrote: If it's just an Ethernet connection, you should be able to hook it up through your
OS network software. Then it looks like just another drive.
Otherwise sockets can be used
Oh. But mine should be in python using the Sockets.
Can you tell me how to "hook" it through OS Network software?
That could be helpful as well.
Posts: 2,953
Threads: 48
Joined: Sep 2016
import asyncio
import socket
host = '0.0.0.0'
port = 8888
loop = asyncio.get_event_loop()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setblocking(False)
s.bind((host, port))
s.listen(10)
async def handler(conn):
while True:
data = await loop.sock_recv(conn, 65535)
if not data:
break
await loop.sock_sendall(conn, data)
conn.close()
async def server():
while True:
conn, addr = await loop.sock_accept(s)
loop.create_task(handler(conn))
loop.create_task(server())
loop.run_forever()
loop.close() The code is not mine. I've found it in my Python_tutorials folder. I was digging for async/await a lot and still don't know how to use it.
Posts: 10
Threads: 3
Joined: Mar 2017
(Mar-31-2017, 11:56 AM)wavic Wrote: import asyncio
import socket
host = '0.0.0.0'
port = 8888
loop = asyncio.get_event_loop()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setblocking(False)
s.bind((host, port))
s.listen(10)
async def handler(conn):
while True:
data = await loop.sock_recv(conn, 65535)
if not data:
break
await loop.sock_sendall(conn, data)
conn.close()
async def server():
while True:
conn, addr = await loop.sock_accept(s)
loop.create_task(handler(conn))
loop.create_task(server())
loop.run_forever()
loop.close() The code is not mine. I've found it in my Python_tutorials folder. I was digging for async/await a lot and still don't know how to use it.
Will this work to read data that is being transferred through the Ethernet Cable??
Posts: 3,458
Threads: 101
Joined: Sep 2016
If that data is being passed on port 8888, sure.
It's not going to get data on any other port. But that's probably fine, you wouldn't want to consume all ports anyway.
Posts: 10
Threads: 3
Joined: Mar 2017
(May-11-2017, 05:03 PM)nilamo Wrote: If that data is being passed on port 8888, sure.
It's not going to get data on any other port. But that's probably fine, you wouldn't want to consume all ports anyway.
Nooo.. I want it to read at a specific IP!
192.168.1.10 and port 7..
Will it work if I change them?
|