【问题标题】:write 1-wire rom addres to variable将 1 线从地址写入变量
【发布时间】:2020-04-05 18:58:32
【问题描述】:

我想从一堆 1-wire 设备中将设备的 rom 地址写入一个数组。 我尝试了很多选择,但显然我不知道如何正确地做到这一点。

在下面的代码中,在设备搜索循环中,我得到了按预期打印在串行线上的地址。 但是主循环的打印输出表明我可以在我的方法中将此地址存储在一个数组中......

#include <OneWire.h>
// http://www.pjrc.com/teensy/td_libs_OneWire.html
OneWire  ds(2);

void setup(void) {
   Serial.begin(9600);
   while (!Serial) {   
  }
}

unsigned char UIDs[12];
int indx=0;

void getDeviceAddresses(void)
{
 int i=0;
 byte present = 0;
 byte done = 0;
 byte data[12];
 byte addr[8];

 while ( !done )
 {
   if ( ds.search(addr) != 1)
   {
     Serial.print("No more addresses.\n");
     ds.reset_search();
     done = 1;
     delay(1000);
     indx=0;
     return;
   }
   else
   {
     Serial.print("Sensors");
     Serial.print(indx);
     Serial.print(" address is:\t");
     indx++;
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      //Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');   
      }
      // print each byte in the address array in hex format
      UIDs[indx]=(UIDs[indx]+(addr[i], HEX));                    // I guess this is not how to do it....
      Serial.print(addr[i], HEX);
    }
  }
  Serial.println();
 }
}

void loop (){
  getDeviceAddresses();
  int i=0;
  while (true) {
    for ( indx = 0; indx < 13; indx++) {
      Serial.println(UIDs[indx]);    
    }
   delay(4000);
  }
}
Sensors0 address is:    106C402502080064
Sensors1 address is:    101E3C25020800DE
Sensors2 address is:    10614C250208000F
Sensors3 address is:    10513325020800E0
Sensors4 address is:    10094B250208003C
Sensors5 address is:    104D342502080097
Sensors6 address is:    10FD4025020800E2
Sensors7 address is:    10534025020800AD
Sensors8 address is:    1047672502080083
No more addresses.
0
128
128
128
128
128
128
128
128
128
0
0
12

【问题讨论】:

  • 您的主要问题是您有一个 unsigned char 数组,并且您试图将字符串插入其中。该数组的每个插槽都可以容纳一个字符,仅此而已。如果你想存储一个字符串数组,那么你需要一个二维数组或一个指针数组。

标签: c arrays arduino 1wire


【解决方案1】:

看起来 addr 是一个包含您的地址的 8 字节数组。

如果你定义为:

unsigned char UIDs[12][8];

然后在每次通过你的函数时,你都有:

{
     Serial.print("Sensors");
     Serial.print(indx);
     Serial.print(" address is:\t");
     indx++;
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      //Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');   
      }
      // print each byte in the address array in hex format
      UIDs[indx][i] = addr[i];         
      Serial.print(addr[i], HEX);
    }
  }

最后在你的循环中打印它们:

for ( indx = 0; indx < 13; indx++) {
      for (int i=0; i<8; i++){
         if(UIDs[indx][i] < 16){
            Serial.print('0');
         }
      Serial.print(UIDs[indx][i], HEX);    
    }

【讨论】:

    【解决方案2】:

    @Delta_G - 谢谢!

    我先尝试了二维数组,但无法正确处理。 认为我搞砸了第二次打印程序。 以供将来参考,以防有人需要,这是一个完整的工作测试代码。

    unsigned char UIDs[12][8];
    byte indx=0;
    byte nr_of_devices=0;
    #include <OneWire.h>
    // http://www.pjrc.com/teensy/td_libs_OneWire.html
    OneWire  ds(2);
    
    void setup(void) {
       Serial.begin(9600);
       while (!Serial) {   
      }
    }
    
    void getDeviceAddresses(void)
    {
     int i=0;
     byte present = 0;
     byte done = 0;
     byte data[12];
     byte addr[8];
    
     while ( !done )
     {
       if ( ds.search(addr) != 1)
       {
         Serial.print("No more addresses.\n");
         ds.reset_search();
         done = 1;
         delay(1000);
         nr_of_devices=indx;
         indx=0;
         return;
       }
       else
       {
         Serial.print("Sensors");
         Serial.print(indx);
         Serial.print(" address is:\t");
         indx++;
        //read each byte in the address array
        for( i = 0; i < 8; i++) {
          //Serial.print("0x");
          if (addr[i] < 16) {
            Serial.print('0');    
          }
          // print each byte in the address array in hex format
          UIDs[indx][i] = addr[i];                    
          Serial.print(addr[i], HEX);
          if (!i >= 1) {
            Serial.print("-");
          }
        }
      }
      Serial.println();
     }
    }
    
    void loop (){
      getDeviceAddresses();
      int i=0;
      while (true) {
      Serial.println("Sensors found:");
      for ( indx = 1; indx < nr_of_devices; indx++) {
        Serial.print(indx);Serial.print(": ");
          for (int i=0; i<8; i++){
             if(UIDs[indx][i] < 16){
                Serial.print('0');
             }
          Serial.print(UIDs[indx][i], HEX);    
    
        }
       Serial.println("");
      }
      delay(4000);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      相关资源
      最近更新 更多