如何在Arduino上将字符串转换为uint8

编程入门 行业动态 更新时间:2024-10-04 07:20:30
如何在Arduino上将字符串转换为uint8_t数组?(How to convert a string into an uint8_t array on Arduino?)

我有一个包含数字和字符值的字符串,例如“p1200”。 我需要将此字符串转换为uint8_t数组,因为我需要从我的xBee发送它。

我该如何转换?

String dataString = "p1200"

uint8_t dataArray[]

我尝试使用以下代码发送此字符串:

power = ((360 * pulseCount) / 60); String dataString = "p" + power; char dataArray[sizeof(dataString)]; dataString.toCharArray(dataArray, sizeof(dataString)); XBeeAddress64 addr64 = XBeeAddress64(); addr64.setMsb(0x13A200); addr64.setLsb(0x406A42B7); ZBTxRequest zbTx = ZBTxRequest(addr64, (uint8_t *)dataArray, sizeof(dataArray)); xbee.send(zbTx);

并使用以下代码接收字符串:

String incomingData; xbee.readPacket(); if (xbee.getResponse().isAvailable()) { Serial.println(xbee.getResponse().getApiId()); if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) { xbee.getResponse().getZBRxResponse(rx); for (int i = 0; i < rx.getDataLength(); i++) { incomingData += (char)rx.getData(i); } } }

当我打印incomingData ,我得到一个奇怪的输出...我认为它是由字符串转换为uint8_t引起的

I have a string that contains both numbers and character values like "p1200" for example. I need to convert this string into a uint8_t array, because I need to send it from my xBee.

How can I convert

String dataString = "p1200"

into

uint8_t dataArray[]

?

I tried to send this string using the following code:

power = ((360 * pulseCount) / 60); String dataString = "p" + power; char dataArray[sizeof(dataString)]; dataString.toCharArray(dataArray, sizeof(dataString)); XBeeAddress64 addr64 = XBeeAddress64(); addr64.setMsb(0x13A200); addr64.setLsb(0x406A42B7); ZBTxRequest zbTx = ZBTxRequest(addr64, (uint8_t *)dataArray, sizeof(dataArray)); xbee.send(zbTx);

And receive the string using the following code:

String incomingData; xbee.readPacket(); if (xbee.getResponse().isAvailable()) { Serial.println(xbee.getResponse().getApiId()); if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) { xbee.getResponse().getZBRxResponse(rx); for (int i = 0; i < rx.getDataLength(); i++) { incomingData += (char)rx.getData(i); } } }

When I print incomingData, I get a strange ouput... I thought it was caused by the conversion from string to uint8_t

最满意答案

使用getBytes ( https://www.arduino.cc/en/Reference/StringGetBytes )将字符串中的字节复制到数组中。 期望一个byte[] ,除非https://www.arduino.cc/en/Reference/byte主动误导,应该与uint8_t[]相同。

您将需要阵列已存在且足够大。 您可以使用length方法找到字符串的length 。

(我认为getBytes优于toCharArray如果它绝对是你想要的uint8_t[] 。)

对您的代码有一些评论

以上内容是在Engo发布一些代码之前编写的,并试图回答这个问题。 但这里有一些关于代码的评论。

在String上调用sizeof几乎肯定不是你想要的。 String对象可以包括其他内容(例如,长度信息),并且可以包括实际字节(其可以例如在指针后面)。 有一种length方法; 用它。 (记住,由于终止空字符,你的缓冲区需要大一个字节。) 我不知道addr64的魔术数字是addr64 (我对xBee一无所知)但会假设你在那里做的事情是有道理的 - 但它看起来像是值得仔细检查的东西。 你说你“得到一个奇怪的输出”,但是如果你告诉我们究竟是什么类型的奇怪输出,或者当你改变你想要传输的字符串时它是如何(如果有的话)改变的话会更有用。 你的代码引用了我所做的变量,称为rx ,但你没有告诉我们它是如何声明的。 ZBRxResponse似乎有一个名为getDataOffset的方法。 再一次,我对这些东西一无所知,但是你实际上试图从响应中取出的东西可能不是从偏移量0开始,而是在调用getDataOffset给出的偏移量getDataOffset ?

Use getBytes (https://www.arduino.cc/en/Reference/StringGetBytes) to copy the bytes from your string into an array. That expects a byte[], and unless https://www.arduino.cc/en/Reference/byte is actively misleading that should be the same as a uint8_t[].

You will need the array to exist already and be large enough. You can find the length of a string with its length method.

(I think getBytes is preferred over toCharArray if it's definitely a uint8_t[] you're wanting.)

Some comments on your code

The above was written before Engo posted some code, and attempts simply to answer the question. But here are some comments on the code.

Calling sizeof on the String is almost certainly not what you want. A String object may include other things (e.g., length information) and may not include the actual bytes (which might e.g. be behind a pointer). There's a length method; use it. (Remembering that your buffer will need to be one byte bigger because of the terminating null character.) I've no idea what's going on with the magic numbers in addr64 (I don't know anything about xBee) but will assume what you're doing there makes sense -- but it looks like the kind of thing that's worth checking really carefully. You say you "get a strange output", but it would be more useful if you told us exactly what sort of strange output, and perhaps how (if at all) it changes when you change the string you're trying to transmit. Your code references what I take to be a variable, called rx, but you haven't shown us how it's declared. ZBRxResponse appears to have a method called getDataOffset. Again, I don't know anything about this stuff, but is the stuff you're actually trying to pull out of the response perhaps not starting at offset 0 but at the offset given by calling getDataOffset?

更多推荐

本文发布于:2023-08-07 11:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上将   字符串   转换为   如何在   Arduino

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!