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代碼,然後我得到連接失敗(在阿爾多尼串行監視器)。
有沒有人有一些想法是什麼問題?看起來服務器和客戶端看不到對方。我也放下了防火牆,但這沒有幫助。任何評論都是有用的!
是否有可能使用boost通過ethernetshield連接到arduino? – RSNL
如果「帶有ethernetshield的arduino」接受TCP連接,則可以使用Boost.Asio建立它們。使用一些嗅探器(如Wireshark)來查看您的客戶端和服務器之間發生了什麼。 –