2016-07-28 61 views
0

我目前有我的树莓派3作为我的本地互联网上的文件共享设备工作。一个5'显示器连接到它,我希望能够通过udp数据包发送命令。我知道很多关于c#的知识,但我完全不熟悉python。我在Visual Studio中进行编程。 我已经发送程序编码工作在C#中python树莓pi从spesefic端口接收udp数据Visual Studio

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聊天辛勤工作。 问题是,我如何通过python在我的树莓派上收到它? 我想:

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) 

但我得到这个消息显示时做“蟒蛇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,把它放在同一个文件夹并运行它。简单的打印“你好世界!”,并且它像预期的那样工作。 我在这里做错了吗?我需要为我的RPi3安装额外的东西吗?请帮忙。

回答

0

我想通了。我改变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 = “”,它工作。那小小的空间破坏了密码。