TCP客户端在TCP客户端node.js中整体接收数据

问题描述 投票:0回答:1

[我是从Arduino服务器向我的node.js客户端发送数据的,当我收到它时,我并没有将它作为一个完整的String来获取,而是以char形式获取,在我的控制台中我得到了类似的东西

接收:h收到:e收到:l收到:l收到:o

而不是接收收到:'hello'

请任何帮助

下面是我的node.js接收数据客户端,而我的Arduino发送数据

client.on('data', function(data) {
    console.log('Received: ' + data);
});
 // listen for incoming clients
      EthernetClient clientA = serverA.available();
      if (clientA) {
          Serial.println("Client A connected.");

          while(clientA.available() > 0) {
              char dataA = clientA.read(); // 
              Serial.print(dataA);
              //clientA.write(dataA); // echo
              serverB.write(dataA); // forward
          }
      }

客户端A是另一个node.js客户端,将其发送到Arduino,而Arduino重新发送数据。

node.js arduino tcpclient
1个回答
0
投票

问题是您按字符读取字符:

    char dataA = clientA.read(); // 
    Serial.print(dataA);

您可以进行循环,将所有接收到的字符放入缓冲区,然后触发清空/打印缓冲区。一些伪代码tpo可以帮助您入门:

    char dataA;
    char buffer [32] = '\0'; // Buffer for 31 chars and the null terminator
    uint8_t i = 0;        
    while(clientA.available() > 0) {
          dataA = clientA.read();  
          buffer [i] = dataA; // we put the char into the buffer
         if (dataA != '/0') i++; // put the "pointer" to the next space in buffer
         else {               
          Serial.print(buffer);

         ... do something else with the buffer ....

          }
        }

阅读有关串行通信的概念,并学习如何将其用作Asa启动器:https://www.oreilly.com/library/view/arduino-cookbook/9781449399368/ch04.html

© www.soinside.com 2019 - 2024. All rights reserved.