2016-07-28 62 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 = 「」,它工作。那小小的空間破壞了密碼。