Struct 在RF24库的结构中发送时转换的字符串

Struct 在RF24库的结构中发送时转换的字符串,struct,arduino,Struct,Arduino,我将RF24库的数据结构从一个节点发送到另一个节点。此结构包含两个字符串和一个浮点 当我用变送器发送结构并将其打印到串行输出时,我得到: El sensor PIR situat en Menjador te el valor 1.00 当我将其打印到串行输出时,我在接收器中看到: El sensor œ>U situat en Üߧŋ>r te el valor 1.00 为什么这两个字符串被更改(“PIR”和“Menjador”) 结构如下: struct sensorD

我将RF24库的数据结构从一个节点发送到另一个节点。此结构包含两个字符串和一个浮点

当我用变送器发送结构并将其打印到串行输出时,我得到:

El sensor PIR situat en Menjador te el valor 1.00
当我将其打印到串行输出时,我在接收器中看到:

El sensor œ>U situat en Üߧŋ>r te el valor 1.00
为什么这两个字符串被更改(“PIR”和“Menjador”)

结构如下:

struct sensorData {
  String  sensorType;
  String  sensorLocation;
  float   sensorValue;
} PIR1, sensor;
我是这样在收音机里写的:

radio.write( &PIR1, sizeof(PIR1) ));
radio.read( &sensor, sizeof(sensor)); 
这是我从收音机里读到的:

radio.write( &PIR1, sizeof(PIR1) ));
radio.read( &sensor, sizeof(sensor)); 
这就是我如何将它打印到接收器的串行输出:

  void printOutData(sensorData* data) {    
    Serial.print("El sensor ");
    Serial.print(data->sensorType);
    Serial.print(" situat en ");
    Serial.print(data->sensorLocation);
    Serial.print(" te el valor ");
    Serial.println(data->sensorValue);
  }

谢谢

字符串类型实际上不包含传感器类型和位置的字节;它指向字节。您主要发送的是这些字节的内存地址,而不是字节本身

您需要更改数据结构,使其能够包含结构中的字节,并使用“C字符串”或“字符数组”:

当然,您必须更改设置这些成员值的方式。如果没有您的完整草图,我只能提供以下几种可能性:

void setup()
{
  Serial.begin( 9600 );

  // Set to a constant value (i.e., a double-quoted string literal)

  strncpy( PIR1.sensorType, "TMP", sizeof(PIR.sensorType)-1 );
  PIR1.sensorType[ sizeof(PIR.sensorType)-1 ] = '\0'; // NUL-terminate

  // Set to characters received over Serial, up to size-1 chars *or* a newline
  //   (This blocks until the line is entered!  Nothing else will happen
  //    until the line has been completely received.)

  size_t count = Serial.readBytesUntil( '\n', PIR1.sensorType, sizeof(PIR.sensorType)-1 );
  PIR.sensorType[ count ] = '\0'; // NUL-terminate
}

void loop()
{
  // Non-blocking receive of a line.  Other things can be performed
  //    while we're waiting for the newline to arrive.

  if (Serial.available()) {

    //  A char finally came in!
    char c = Serial.read();

    // end-of-line?
    if (c == '\n') {

      // Pressed ENTER, send what we have now
      PIR.sensorLocation[ count ] = '\0'; // NUL-terminate the C string
      radio.write( &PIR1, sizeof(PIR1) ));

      //  Reset for next time
      count = 0;

    } else { // not end-of-line, save another char (if there's room)

      if (count < sizeof(PIR.sensorLocation)-1) {
        PIR.sensorLocation[ count++ ] = c;
      }
    }
  }

  // Do some other things here... maybe check some buttons?
}
void setup()
{
Serial.begin(9600);
//设置为常量值(即双引号字符串文字)
strncpy(PIR1.sensorType,“TMP”,sizeof(PIR.sensorType)-1);
PIR1.sensorType[sizeof(PIR.sensorType)-1]='\0';//num终止
//设置为通过串行接收的字符,最大为1个字符*或*换行符
//(这会一直阻止,直到输入该行为止!不会发生任何其他情况。)
//直到线路完全接收完毕。)
size\u t count=Serial.readBytesUntil('\n',PIR1.sensorType,sizeof(PIR.sensorType)-1);
PIR.sensorType[count]='\0';//num终止
}
void循环()
{
//线路的非阻塞接收。可以执行其他操作
//在我们等待新线到达的时候。
if(Serial.available()){
//终于来了一个木炭!
char c=Serial.read();
//下线?
如果(c=='\n'){
//按ENTER键,发送我们现在拥有的内容
PIR.sensorLocation[count]='\0';//NUL终止C字符串
radio.write(&PIR1,sizeof(PIR1));
//重新设置以备下次使用
计数=0;
}否则{//不是行尾,保存另一个字符(如果有空间)
if(计数

避免使用
String
,有很多原因,因此从长远来看,切换到
char
数组实际上会有回报。

谢谢!!我不知道在这种情况下字符串是如何工作的。如果我使用枚举来定义这两个变量中的值,那么理解代码可能就不那么困难了……是的,枚举可以作为单个字节发送,而不是作为类型/位置的字符“名称”发送多个字节。您也不必解释接收器中的名称——枚举值已准备就绪。