gpt4 book ai didi

c++ - C 中的数据类型问题

转载 作者:行者123 更新时间:2023-11-28 06:37:05 29 4
gpt4 key购买 nike

我正在尝试 Arduino 终极 GPS 突破,我想从 GPS 获取经度和纬度。然后我想通过 RF 无线发送这两个变量。如下图所示: enter image description here

我为名为 panstamp 的射频模块使用了一个库,以便能够从 Arduino 1 发送经度和纬度,并在 Arduino 2 中接收它们。如下面的代码所示:

传输:

    void send_data() {
CCPACKET data;
data.length=2;

float lon=26.533255;
float lat=27.533463;


data.data[0]=lon;
data.data[1]=lat;
if(cc1101.sendData(data)){
Serial.println(data.data[0]);
Serial.println(data.data[1]);

Serial.println(" sent ok ");
return true;
}else{
Serial.println("sent failed ");
return false;
}

}

接收:

void loop(){
float j = 0;
lon = packet.data[j];
Serial.print(lon);
Serial.print(" ");
float k = 1;
lat = packet.data[k];
Serial.print(lat);
Serial.println(".");
}

它在发送和接收时完美地工作:)

问题是当我收到这两个变量时,我只收到 lon 26.00 和 lat 27.00 但没有收到 lon 26.533255 lat 27.533463 如我所料。

我假设的数据类型存在一些错误。我调查了 panstamp 库以找到可以更改类型但没有成功的东西。

这是 CCPACKET 的头文件:

#ifndef _CCPACKET_H
#define _CCPACKET_H

#include "Arduino.h"

/**
* Buffer and data lengths
*/
#define CC1101_BUFFER_LEN 64
#define CC1101_DATA_LEN CC1101_BUFFER_LEN - 3

/**
* Class: CCPACKET
*
* Description:
* CC1101 data packet class
*/
class CCPACKET
{
public:
/**
* Data length
*/
byte length;

/**
* Data buffer
*/
byte data[CC1101_DATA_LEN];

/**
* CRC OK flag
*/
boolean crc_ok;

/**
* Received Strength Signal Indication
*/
byte rssi;

/**
* Link Quality Index
*/
byte lqi;
};

#endif

以及发送数据/接收数据的源码:

boolean CC1101::sendData(CCPACKET packet)
{
byte marcState;
bool res = false;

// Declare to be in Tx state. This will avoid receiving packets whilst
// transmitting
rfState = RFSTATE_TX;

// Enter RX state
setRxState();

// Check that the RX state has been entered
while (((marcState = readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x0D)
{
if (marcState == 0x11) // RX_OVERFLOW
flushRxFifo(); // flush receive queue
}

delayMicroseconds(500);

// Set data length at the first position of the TX FIFO
writeReg(CC1101_TXFIFO, packet.length);
// Write data into the TX FIFO
writeBurstReg(CC1101_TXFIFO, packet.data, packet.length);

// CCA enabled: will enter TX state only if the channel is clear
setTxState();

// Check that TX state is being entered (state = RXTX_SETTLING)
marcState = readStatusReg(CC1101_MARCSTATE) & 0x1F;
if((marcState != 0x13) && (marcState != 0x14) && (marcState != 0x15))
{
setIdleState(); // Enter IDLE state
flushTxFifo(); // Flush Tx FIFO
setRxState(); // Back to RX state

// Declare to be in Rx state
rfState = RFSTATE_RX;
return false;
}

// Wait for the sync word to be transmitted
wait_GDO0_high();

// Wait until the end of the packet transmission
wait_GDO0_low();

// Check that the TX FIFO is empty
if((readStatusReg(CC1101_TXBYTES) & 0x7F) == 0)
res = true;

setIdleState(); // Enter IDLE state
flushTxFifo(); // Flush Tx FIFO

// Enter back into RX state
setRxState();

// Declare to be in Rx state
rfState = RFSTATE_RX;

return res;
}


byte CC1101::receiveData(CCPACKET * packet)
{
byte val;
byte rxBytes = readStatusReg(CC1101_RXBYTES);

// Any byte waiting to be read and no overflow?
if (rxBytes & 0x7F && !(rxBytes & 0x80))
{
// Read data length
packet->length = readConfigReg(CC1101_RXFIFO);
// If packet is too long
if (packet->length > CC1101_DATA_LEN)
packet->length = 0; // Discard packet
else
{
// Read data packet
readBurstReg(packet->data, CC1101_RXFIFO, packet->length);
// Read RSSI
packet->rssi = readConfigReg(CC1101_RXFIFO);
// Read LQI and CRC_OK
val = readConfigReg(CC1101_RXFIFO);
packet->lqi = val & 0x7F;
packet->crc_ok = bitRead(val, 7);
}
}
else
packet->length = 0;

setIdleState(); // Enter IDLE state
flushRxFifo(); // Flush Rx FIFO
//cmdStrobe(CC1101_SCAL);

// Back to RX state
setRxState();

return packet->length;
}

请有人帮助我:)

Panstamp 库的链接:PanStamp Library

最佳答案

据我所知,你在这里失去了你的预测:

float lon=26.533255;
float lat=27.533463;
data.data[0]=lon;
data.data[1]=lat;

因为数据是一个字节数组,根据这个:

/**
* Data buffer
*/
byte data[CC1101_DATA_LEN];

您需要正确缓冲数据。

关于c++ - C 中的数据类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606792/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com