如何使用套接字将Arduino连接到C++?

3
我的C++客户端套接字代码只收集由Arduino套接字服务器提供的输出的第一行(我猜我可以这样称呼它)。请告诉我我的C++代码出了什么问题,如何修复?如果问题太冗长,请跳到底部的C++代码。
硬件设置:带有以太网卡(盾牌)的Arduino Mega和带有Ubuntu 16.04的Intel NUC。两个设备使用电缆和未管理的交换机连接。

enter image description here

Arduino端:我从Arduino Ethernet库中的一个Web服务器示例开始,修改了代码,直到我能够收集所有I/O的状态,处理I/O数据,并向Web客户端提供结果。下面的图片显示了我的Arduino输出的HTML快照。 enter image description here

处理套接字服务器端的Arduino代码如下(一切正常,但考虑到显示所有内容而包括):

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xBA, 0xDA, 0x55, 0x12, 0x34, 0x56};

IPAddress ip(192, 168, 0, 21);

EthernetServer server(80);

void setup() 
{
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) 
  {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware.");
   while (true) 
   {
      delay(1); // do nothing, no point running without Ethernet hardware
   }
}

if (Ethernet.linkStatus() == LinkOFF) 
{
    Serial.println("Ethernet cable is not connected.");
}

server.begin();
}

void loop()
{
    //true when there is an incoming connection
   if (client) 
   {
     Serial.println("new client");
     // an http request ends with a blank line
     boolean currentLineIsBlank = true;
     while (client.connected()) 
     {
       if (client.available()) 
       {
          //the next two lines print the client HTTP GET request to serial.
          char c = client.read();
          Serial.write(c);
          // if you've gotten to the end of the line (received a newline
          // character) and the line is blank, the http request has ended,
          // so you can send a reply
          if (c == '\n' && currentLineIsBlank) 
          {
             // send a standard http response header
             client.println("HTTP/1.1 200 OK");
             client.println("Content-Type: text/html");
             client.println("Connection: close");  
             client.println("Refresh: 1");  
             client.println();
             client.println("<!DOCTYPE HTML>");
             client.println("<html>");

             //whole bunch of client.println("...."); to dish out a web page.

              client.println("</html>");
              break;
            }

            if (c == '\n') 
            {
               // you're starting a new line
               currentLineIsBlank = true;
            } else if (c != '\r') 
            {
              // you've gotten a character on the current line
              currentLineIsBlank = false;
            }
         }
      }
     // give the web browser time to receive the data
     //delay(1);
     // close the connection:
     client.stop();
     Serial.println("client disconnected");
  }
}
使用Ubuntu 16.04和C++11的Intel NUC:我一直在跟随教程(https://www.binarytides.com/socket-programming-c-linux-tutorial/)学习套接字客户端,并从https://www.learncpp.com学习C++。到目前为止,我能够使用套接字向Google服务器发送请求并收集HTML页面,并将HTML打印到终端上: enter image description here

这是我的C++代码:

#include <iostream> //for std::cout & std::endl
#include<arpa/inet.h> //inet_addr
#include<string.h> // for strlen

int main()
{
    int my_socket = socket(AF_INET , SOCK_STREAM , 0);

    //make sure the socket we got is OK
    if (my_socket == -1)
    {
         std::cout << "problem creating a socket." << std::endl;
    }

    struct sockaddr_in connectionToServer;

    connectionToServer.sin_addr.s_addr = inet_addr("172.217.2.99");//google IP
    connectionToServer.sin_family = AF_INET; //type of IP addres. where AF_INET is IPv4
    connectionToServer.sin_port = htons(80); //port is set via a method


    if (connect(my_socket , (struct sockaddr *)&connectionToServer , sizeof(connectionToServer)) < 0)
    {
        std::cout << "connect error" << std::endl;
        return 1;
    }

    std::cout << "Connected" << std::endl;

    //send a request to get a page
    char *message = "GET / HTTP/1.1\r\n\r\n";
    if( send(my_socket , message , strlen(message) , 0) < 0)
    {
        std::cout << "Send failed" << std::endl;
        return 1;
    }

    std::cout << "Data Sent\n" << std::endl;


    char buffer [20000] = {};
    if( recv(my_socket, buffer , sizeof(buffer) , 0) < 0)
    {
        std::cout << "recv failed" << std::endl;
    }

    for(int i=0; i<20000 ; i++)
    {
        std::cout<< buffer[i];
    }

    return 0;
 }
问题:当我将C++程序中的IP地址从Google更改为我的Arduino时,cpp客户端套接字程序只收集Arduino套接字服务器输出的第一行。我知道它是第一行,因为我修改了Arduino服务器输出的第一行,添加了“..但这并不重要”,并且更改显示在c ++程序的标准输出窗口中。我需要C++程序收集整个输出,而不仅仅是第一行。我真的无法弄清楚如何做到这一点。

enter image description here

请问您能帮我解决以下问题吗:

  1. 如何收集完整的Arduino消息(而不仅仅是第一行)?我需要对我的C++程序进行哪些修改?该系统需要能够将数据从一个设备传递到另一个设备。
  2. 整个设置的目标是从Arduino传递2个浮点数和6个整数到我的C++程序中。很快我将放弃整个HTML内容。在传输数据时,您会推荐使用哪种协议?我正在考虑用字母填充每个值。例如:“Aint1A Bint2B Cint3C Dfloat1D ...”等等。您能否推荐一些后续教程/网页,以建议最佳方式打包和处理通过套接字到达C++程序的数据?

我提前为“修复我的代码”问题道歉,但我读过的所有问题都有点太高级了,因为它们涉及缓冲区溢出、安全性、端点问题、错误处理、畸形消息等等,这远远超出了我的需求(能力可能更准确)。非常感谢您的时间和帮助。


4
recv() 不能保证一次性返回所有数据,您需要在循环中调用它。 - Jonathan Potter
1
你的C++代码实际上没有实现HTTP协议。特别是,没有任何接收HTTP回复的代码。因此,我们的代码无法接收HTTP回复,而是从TCP流中接收到任意数据块,这并不令人惊讶。 - David Schwartz
@Mike,你的HTTP请求格式错误(缺少必需的“Host”头)。但更重要的是,你只调用了一次recv(),所以你最多接收1到20000个字节,这些字节可能可能不会(很可能不会)一次性包含完整的响应。你需要在循环中调用recv(),直到你达到实际响应的结尾。这对于HTTP来说并不简单,因为HTTP是一种具有消息结构的协议,而你根本没有遵循这种结构,甚至没有接近。你必须使用类似于这个伪代码的逻辑 - Remy Lebeau
1个回答

1
非常感谢Jonathan Potter和Remy Lebeau在评论中提供了可行的答案。我按照你们的建议/答案进行了操作,一切都正常工作。我没有触碰Arduino代码,并在CPP代码中做出了以下更改:
在问题的CPP代码中,删除(包括)char buffer [20000] = {};之后的所有内容,并用以下内容替换:
//***********receive the results**************************
//initialize a string
std::string totalResults;

//create a temp array. This will hold a single line recieved from
//the arduino.
char tempBuffer [300] = {};

//the number of lines I want to recieve from the arduino
int magicNumber = 100;

//recieve the arduino response
for(int i = 0; i < magicNumber ; i++)
{
    //call the recv method over and over as it gets a single arduino line with
    //every iteration.
    if( recv(my_socket, tempBuffer , sizeof(tempBuffer) , 0) < 0)
    {
    std::cout << "recv failed" << std::endl;
    }

    //write out the single line we recieved to a string (which grows on the fly)
    for(int i = 0; i < 300; i++ )
    {
        totalResults = totalResults+tempBuffer[i];

        //kill the loop the moment there is a null character. When i created the
        //array i initialized it with NULL characters. so if I am seeing a null
        //character it means that the data I recieved from the arduino has all been
        //given to the string.
        if(tempBuffer[i] == NULL)
        {
            break;
        }
    }

    //empty array - see: https://dev59.com/OHRB5IYBdhLWcg3wZmlz
    std::fill(&tempBuffer[0], &tempBuffer[300], 0);

}

//print the results to the standard output.
std::cout << totalResults << std::endl;

这些更改(我相信可以从这里到月球再回来进行批判)使我能够获得Arduino发送的数据,而不会错过任何一个ASCII字符。谢谢!

enter image description here

在另一个方面,感谢David Schwartz和Remy Lebeau指出我使用的HTTP协议相当糟糕。我之前使用HTTP是因为它是Arduino代码中的一个可行例子;现在的目标是去除HTML并找到更有效的方法将值传递给cpp代码(仅使用套接字)。非常感谢您的评论!
****编辑****
好的,如果你还在阅读这篇文章,你一定正在通过以太网从Arduino传递一些信息到cpp程序。如果是这样,请继续阅读。在我被引导如何接收完整的Arduino响应后,我删除了所有的HTML和HTTP,并简单地发送了我需要的值,同时加上了字母填充(例如:Aint1B,Cint2D,Efloat1F等)。我用~~~字符标记了Arduino的传输结束。很酷。但是,由于某种原因,有时我会得到完整的Arduino响应,有时它会缺少一些消息尾部的内容。以下是我学到的内容:
  1. recv读取的位置(内存或系统调用,我不清楚),有时只有一个字符值。
  2. 有时,recv获取的仅仅是\n字符!
  3. 有时,recv读取的位置可以有多个值!有时recv方法返回6个字符,有时只返回4个字符。这种行为似乎是不可预测的。

考虑到这种行为,我修改了我的cpp代码。该代码接收整个消息,并在完成后停止recv方法的额外循环。希望你会觉得它有用:

//***********receive the results**************************
//initialize a string
std::string totalResults = "";

//create a temp array. This will hold a single line recieved from
//the arduino.
char tempBuffer [300] = {};

//the number of lines I want to receive from the Arduino. This is an unusual
//value for the following reasons (figured out via println):
//(1) sometimes the buffer where the recv method reads from has only one value.
//    ex: letter A only (as per my,*ahem", "protocol".
//(2) sometimes the \n is all a recv fetches!
//(3) sometimes the buffer where the recv method reads has multiple values, so
//    the recv fetches many items that get unpacked in the second loop. This is
//    why sometimes we increase the value by only 1, but get WAY more values. I
//    observed this behaviour to be non repeating. Sometimes it reads 5 values,
//    and sometimes it reads only 3 values.
// At a value of 60 I am always getting the message, and run the recv command
// unnecesserily. For this reason I have implemented the "end transmission"
// characters (~~~), which allow me to kill the for loop once the full message is
// retrieved.
int numberOfTimesRecvRuns = 60;

//number of characters per line. do not reduce as it is needed to be this size to
// get the full insult if the protocol is not followed.
int arduinoNumberOfCharsPerLine = 50;

bool fullResponseRecieved = false;

//recieve the entire arduino response. The magic number is the number of times
// we call the recv method (which reads a line from the socket).
for(int i = 0; i < numberOfTimesRecvRuns; i++)
{
    //call the recv method over and over as it gets a single arduino line with
    //every iteration.
    if( recv(my_socket, tempBuffer , sizeof(tempBuffer) , 0) < 0)
    {
    std::cout << "recv failed" << std::endl;
    }

    //write out the single line we recieved to a string (which grows on the fly). 300 because
    //i dont believe I will have more than 300 characters per line.
    for(int j = 0; j < arduinoNumberOfCharsPerLine; j++ )
    {
        totalResults = totalResults+tempBuffer[j];
        std::cout << "i: " << j << " value recv read: " << tempBuffer[j]<< std::endl;

        //kill the loop the moment there is a null character. When i created the
        //array i initialized it with NULL characters. so if I am seeing a null
        //character it means that the data I recieved from the arduino has all been
        //given to the string.
        if(tempBuffer[j] == NULL )
        {
            std::cout << "I ran... See ya" << std::endl;
            break;
        }

        //end of transmission detected
        if(tempBuffer[j] == '~')
        {
            fullResponseRecieved = true;
        }
    }

    //empty array - see: https://dev59.com/OHRB5IYBdhLWcg3wZmlz
    std::fill(&tempBuffer[0], &tempBuffer[300], 0);

    // A '~' character means the full message has been recieved and there is no
    // need to keep looping for the purpose of running the recv method.
    if(fullResponseRecieved == true)
    {
        //reset the value
        fullResponseRecieved = false;
        std::cout << "killing recv loop" << std::endl;
        break;
    }

}

//print the results to the standard output.
std::cout << totalResults << std::endl;

return 0;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接