如何将字符串转换为byte []?

时间:2017-02-22 16:53:34

标签: arduino

我有Arduino + Ethenet盾牌。我想动态更改ip,具体取决于输入com-port的输入。

主要问题是 - 输入字符串具有String类型,Ethernet.begin方法接受字节数组。一般来说,我无法理解如何正确转换此字符串。试图通过移动char数组中的字符串,然后在字节数组中制作自行车(拐杖)。没有成功。如何将字符串转换为byte []?

#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;

void initEthernetConfig(byte ip[])
{

  Ethernet.begin(mac, ip);
  Serial.begin(9600); 
  Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void setup(){
  byte ip[] = { 10, 28, 33, 4 };
 // initEthernetConfig(ip);
}

void loop(){
  // check for serial input

   while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
  byte inArray[4];
  char * tokens;
    int i = 0;    
    tokens = strtok(readString, ".");
    while (tokens != NULL) {
        inArray[i] = atoi(tokens);
        tokens = strtok(NULL, ".");
        i++;
    }
  initEthernetConfig(inArray);
  }

} 

2 个答案:

答案 0 :(得分:1)

试试这个:

while (tokens != NULL) {
    inArray[i++] = atoi(tokens) & 0xFF;
    tokens = strtok(NULL, ".");
}

只获得整数的最后一个字节,但在值0-255的情况下,这就是你想要的。

但是又为什么不把它作为字节读出来......

void loop(){
  byte inArray[4];
  int i = 0;
  // check for serial input

   while (Serial.available()) {
    inArray[i++] = (byte) Serial.read();  //gets one byte from serial buffer
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  initEthernetConfig(inArray);
}

答案 1 :(得分:1)

这是一个可能的解决方案,它解析,转储和设置 IPv4

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;

/**
 * help functions declaration
 */

void get_ip(byte ip[4]);
void dump_ip(byte ip[4]);
void initEthernetConfig(byte ip[]);

/**
 * setup && loop
 */

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

void loop()
{
  byte ip[4];

  get_ip(ip);
  dump_ip(ip);

  initEthernetConfig(ip);
}

/**
 * help function implementation
 */

void get_ip(byte ip[4])
{
  for (byte i = 0; i < 4; ++i) {
    while (Serial.available() <= 0) { };
    ip[i] = (byte) Serial.parseInt();
    if (i < 3) { Serial.read(); } // throw away dot
  }
};

void dump_ip(byte ip[4]) {
  for (byte i = 0; i < 4; ++i) {
    Serial.print(ip[i]);
    if (i < 3) {
      Serial.print('.');
    } else {
      Serial.println();
    }
  }
};

void initEthernetConfig(byte ip[])
{
  Ethernet.begin(mac, ip);
  Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
};

显然,在此测试应用程序之外,您应确保Serial输入已准备好首先发送IPv4,否则该函数可能会返回0.0.0.0。一个想法是使用基于字符串的命令丰富您的通信协议,例如: ip:会发出IPv4输入的开始信号。