我正在尝试通过 Arduino 无线控制机器人(使用计算机上的 X360 控制器),这需要非常低的延迟。出于这个原因,我选择了 Wifi(事实上我将流式传输视频),经过一些测试,结果发现我在使用 TCP 时有很大的延迟。这正常吗(54Mbits/s,不应该!)?我怎样才能减少它是可控的?
服务器代码(Arduino 草图):
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x48, 0x0D };
byte ip[] = { 192, 168, 0, 11 };
byte gateway[] = { 192, 168, 0, 254 };
byte subnet[] = { 255, 255, 255, 0 };
byte localPort = 99;
EthernetServer server = EthernetServer(localPort);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
// start listening for clients
server.begin();
Serial.println("Server ready");
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
Serial.println("Received:");
byte received = 0;
while((received = client.read()) != -1) {
Serial.println(received);
server.write(received);
}
Serial.println("Over\n");
}
}
客户端代码(PC、QtCreator):
#include <QTextStream>
#include <QTCPSocket>
QString arduinoIP = "192.168.0.11";
char arduinoPort = 99;
int main(void)
{
QTcpSocket socket;
QTextStream in(stdin);
QTextStream out(stdout);
out << "Connection... "; out.flush();
socket.connectToHost(arduinoIP, arduinoPort);
if(!socket.waitForConnected(5000)) {
out << socket.errorString() << "\n";
}
else {
out << "OK\n"; out.flush(); //I don't know why \n doesn't flush
out << "Type a message to send to the Arduino or quit to exit\n"; out.flush();
QString command;
in >> command;
while(command != "quit") {
QByteArray bufOut = command.toUtf8();
socket.write(bufOut);
socket.waitForReadyRead(1000); //Wait for answer (temp)
out << "Answer: " << socket.readAll() << "\n";
}
}
return 0;
}
预先感谢您的帮助。
问候,Mystère 先生