python Raspberry pi 从特定端口接收 udp 数据 Visual Studio
python Raspberry pi receive udp data from spesefic port Visual Studio
我目前让我的 Raspberry Pi 3 在本地互联网上用作文件共享设备。一个 5' 显示器连接到它,我希望能够通过 udp 数据包向它发送命令。
我对 C# 了解很多,但我对 python 完全陌生。我正在 Visual Studio 编程。
我已经有了用 c#
编码的发送程序
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse(IPADDRESS);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
byte[] send_buffer = UTF8Encoding.UTF8.GetBytes(TEXT);
sock.SendTo(send_buffer, endPoint);
我知道它是有效的,因为我的 UDP-Chat 正在工作。
问题是,如何通过 python 在 raspberry pi 上接收它?
我试过了:
import socket
UDP_IP = "192.168.1.11" #Which is my local ip for my computer
UDP_PORT = 2522
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print (data)
但是我在执行 'python Receiver.py':
时显示了这条消息
Traceback (most recent call last):
File "Receiver.py", line 7, in <module>
sock.bind(("192.168.1.11", UDP_PORT))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
我创建了一个新文件,将其命名为 test.py,并将其放在同一文件夹中并 运行。简单的打印 "hello world!",它按预期工作。
我在这里做错了什么吗?我需要为我的 RPi3 安装额外的东西吗?请帮忙。
我明白了。我将 UDP_IP = "192.168.1.11" 更改为 UDP_IP = ""
像这样:
import socket
UDP_PORT = 2522
UDP_IP = ""
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print (data)
我之前试过 UDP_IP = " ",但是它给了 Visual Studio 红线..所以我把它改成了 UDP_IP = "",它成功了。那个小小的space破坏了代码。
我目前让我的 Raspberry Pi 3 在本地互联网上用作文件共享设备。一个 5' 显示器连接到它,我希望能够通过 udp 数据包向它发送命令。 我对 C# 了解很多,但我对 python 完全陌生。我正在 Visual Studio 编程。 我已经有了用 c#
编码的发送程序Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse(IPADDRESS);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
byte[] send_buffer = UTF8Encoding.UTF8.GetBytes(TEXT);
sock.SendTo(send_buffer, endPoint);
我知道它是有效的,因为我的 UDP-Chat 正在工作。 问题是,如何通过 python 在 raspberry pi 上接收它? 我试过了:
import socket
UDP_IP = "192.168.1.11" #Which is my local ip for my computer
UDP_PORT = 2522
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print (data)
但是我在执行 'python Receiver.py':
时显示了这条消息Traceback (most recent call last):
File "Receiver.py", line 7, in <module>
sock.bind(("192.168.1.11", UDP_PORT))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
我创建了一个新文件,将其命名为 test.py,并将其放在同一文件夹中并 运行。简单的打印 "hello world!",它按预期工作。 我在这里做错了什么吗?我需要为我的 RPi3 安装额外的东西吗?请帮忙。
我明白了。我将 UDP_IP = "192.168.1.11" 更改为 UDP_IP = "" 像这样:
import socket
UDP_PORT = 2522
UDP_IP = ""
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print (data)
我之前试过 UDP_IP = " ",但是它给了 Visual Studio 红线..所以我把它改成了 UDP_IP = "",它成功了。那个小小的space破坏了代码。