buf[i] 返回最大 8 位,即 255,而不是发送的字符

问题描述 投票:0回答:1

无法理解我在 Arduino 设置中得到的内容。

场景是: 我使用运行 virtualwire 的芯片组将字符 A(可以是任何字符)从 TX 板发送到 RX 板。 RX正在接收消息,缓冲区长度为1。当我在串行监视器上打印出

buf[i]
时,我得到255(这是我假设的8位最大整数)而不是字符A。这是相关的代码:

TX 文件

void setup() {

 c = 'A'; //for testing only

  // Wireless code...Data pin on TX connected to port 7...vw stands for 'virtualwire'

  vw_setup(2000); // Initialize tx transmission rate
  vw_set_tx_pin(7); // Declare arduino pin for data to transmitter

...

void loop() {

...

vw_send((uint8_t *)c, 1); // Turn buzzer on...Transmit the character

RX 文件

// RX 数据引脚为 8

void loop() {

    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

感谢您的建议!

c++ arduino buffer radio-transmission
1个回答
0
投票

问题大概是这样的:

vw_send((uint8_t *)c, 1);

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

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


还有一个多余的 if:

if(vw_get_message(buf, &buflen))
未嵌套在
if(vw_have_message() == 1)
内,因此无论
vw_have_message()
是否总是返回
1
,它都会运行。

© www.soinside.com 2019 - 2024. All rights reserved.