2013-10-17 27 views
0

Iim編程一個tcp/ip pc/arduino項目。 Arduino有一個ethernetshield並作爲客戶端。 PC運行提升並利用asio庫並作爲客戶端。adruino與ethernetshield和boost服務器無法連接

當我嘗試連接到服務器的連接不能建立。該服務器有一個網絡適配器,其靜態地址爲192.168.1.1。而Arduino的IP地址爲192.168.1.2。兩者直接與UTP電纜連接。兩者都使用端口5000.

對於Arduino代碼,我使用一個示例程序進行測試,但是失敗。該設置是這樣的:

// Enter the IP address of the server you're connecting to: 
IPAddress server(192,168,1,1); 

// Initialize the Ethernet client library 
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet; 
// if you're using Processing's ChatServer, use port 10002): 
EthernetClient client; 

void setup() { 
    // start the Ethernet connection: 
    Ethernet.begin(mac, ip); 
    // Open serial communications and wait for port to open: 
Serial.begin(9600); 
while (!Serial) { 
    ; // wait for serial port to connect. Needed for Leonardo only 
} 


// give the Ethernet shield a second to initialize: 
delay(1000); 
Serial.println("connecting..."); 

// if you get a connection, report back via serial: 
if (client.connect(server, 5000)) { 
    Serial.println("connected"); 
} 
else { 
    // if you didn't get a connection to the server: 
    Serial.println("connection failed"); 
} 
} 

PC服務器代碼也相當簡單,在類的構造函數我做到以下幾點:

cout << "Setting up server" << endl; 
    // Protocol and port 
    boost::asio::ip::tcp::endpoint Endpoint(boost::asio::ip::tcp::v4(), 5000); 

    // Create acceptor 
    boost::asio::ip::tcp::acceptor Acceptor(IOService, Endpoint); 

    // Create socket 
    SmartSocket Sock(new boost::asio::ip::tcp::socket(IOService)); 

    cout << "Before accept..." << endl; 

    // Waiting for client 
     Acceptor.accept(*Sock); 

    cout << "Server set up" << endl; 

的SmartSocket是typdef:

typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SmartSocket; 

我啓動服務器控制檯打印「接受前」並等待接受函數爲一個無聊的客戶端。但是,當我運行我的Arduino代碼,然後我得到連接失敗(在阿爾多尼串行監視器)。

有沒有人有一些想法是什麼問題?看起來服務器和客戶端看不到對方。我也放下了防火牆,但這沒有幫助。任何評論都是有用的!

+0

是否有可能使用boost通過ethernetshield連接到arduino? – RSNL

+0

如果「帶有ethernetshield的arduino」接受TCP連接,則可以使用Boost.Asio建立它們。使用一些嗅探器(如Wireshark)來查看您的客戶端和服務器之間發生了什麼。 –

回答

2

服務器和客戶端可能有多種不通信的原因。從代碼中的錯誤,阻止消息的防火牆,NIC設置或設備根本不連接。這比你想象的要普遍得多!

爲確保您的連接正常,請嘗試使用來自Ardunio的電腦ping

確保您的asio PC服務器代碼正常。嘗試像HTTP請求一樣進行本地TCP連接。例如。在電腦的網頁瀏覽器網址中輸入http://127.0.0.1:5000/hello。這將在本地主機上發送一個GET請求到端口5000上的服務器,它至少應該在您的PC控制檯上打印「服務器設置」。

如果沒有,請嘗試在您的PC服務器代碼和請求中使用端口80(標準TCP端口)而不是5000。如果有效,那麼防火牆可能會阻止您的端口,或者您的Adruino代碼中存在一個錯誤

但是,如果這樣做不起作用,那麼您需要密切關注您的服務器代碼。 asio Daytime.2示例應該適合您。

還請@ @伊戈爾的建議,並在您的電腦上安裝Wireshark。它可能是免費的,但在調試網絡問題時它是無價的。