【问题标题】:buf[i] returns the 8 bit max, ie 255, instead of character sentbuf[i] 返回最大 8 位,即 255,而不是发送的字符
【发布时间】:2015-08-17 00:51:52
【问题描述】:

我无法理解我在 Arduino 设置中得到了什么。

场景是: 我使用运行 virtualwire 的芯片组将字符 A(可以是任何字符)从 TX 板发送到 RX 板。 RX 正在接收消息,缓冲区长度为 1。当我在串行监视器上打印出 buf[i] 时,我得到 255(这是我假设的最大 8 位整数)而不是字符 A。这是相关代码:

TX 文件 [代码] 无效设置(){

c = 'A'; //仅供测试

// 无线代码...TX 上的数据引脚连接到端口 7...vw 代表“virtualwire”

vw_setup(2000); // 初始化 tx 传输速率 vw_set_tx_pin(7); // 声明用于发送数据的 arduino 引脚 ...

空循环() {

... vw_send((uint8_t *)c, 1); // 打开蜂鸣器...发送字符

RX 文件

// RX数据引脚为8

空循环() {

Serial.println("Looping");
delay(2000);

uint8_t buflen = VW_MAX_MESSAGE_LEN;// Defines maximum length of message
uint8_t buf[buflen]; // Create array to hold the data; defines buffer that holds the message

if(vw_have_message() == 1) // Satement added by virtualwire library recommendation as one of three statements to use before the get_message statement below// not in original HumanHardDrive code
{
  Serial.println("Message has been received");
}

if(vw_get_message(buf, &buflen)) // &buflen is the actual length of the received message; the message is placed in the buffer 'buf'

{
  Serial.print("buflen from &buflen = ");
  Serial.println(buflen); // Print out the buffer length derived from the &buflen above

  for(int i = 0;i < buflen;i++)
  {
    Serial.print("i = ");
    Serial.println(i);             <--prints 0
    Serial.print(" buf[0] = ");    
    Serial.print(buf[0]);          <--prints 255  
    Serial.print("   buf[i] = ");
    Serial.println(buf[i]);        <--prints 255


    if(buf[i] == 'A')  <-- Does not recognize A since buf[i] comes out as 255

[/代码]

感谢您的任何建议!

【问题讨论】:

    标签: arduino buffer wireless transmission


    【解决方案1】:

    问题大概是这样的:

    vw_send((uint8_t *)c, 1);

    c 不是指针,您将'A' 的值作为指针位置传递,然后由vw_send 读取。您需要地址操作符:

    vw_send((uint8_t *) &amp;c, 1);


    还有一个多余的 if: if(vw_get_message(buf, &amp;buflen)) 没有嵌套在 if(vw_have_message() == 1) 中,因此不管vw_have_message() 可能并不总是返回1,它都会运行。

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 2020-12-09
      • 1970-01-01
      • 2019-02-27
      • 2019-09-06
      • 2016-09-29
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多