我想使用 XBee 和 Arduino 将数据从终端设备发送到协调器。但是,Arduino 在发送数据时重新启动(发送数据被中止)。可能是什么问题?
/* Transmit */
#include <SoftwareSerial.h>
#include <XBee.h>
int end = 1;
int alim_XbeeRS = A7;
int RX_XBee = 14;
int TX_XBee = 15;
XBee xbee = XBee();
//Allume le périphérique
void powerOn(SoftwareSerial portcom)
{
portcom.begin(57600);
digitalWrite(alim_XbeeRS, HIGH);
}
void setup ()
{
SoftwareSerial XbeeRS(RX_XBee,TX_XBee);
Serial.begin(57600);
XbeeRS.begin(57600);
pinMode(RX_XBee, INPUT); // Rx
pinMode(TX_XBee, OUTPUT); // Tx
pinMode(alim_XbeeRS, OUTPUT);
powerOn(XbeeRS);
xbee.setSerial(XbeeRS);
delay(5000);
Serial.println("XBee OP");
}
void loop()
{
if (end == 1)
{
Serial.println("sending");
ZBTxRequest _zbTx;
uint8_t payload[] = {'Y','E','S','\0'};
XBeeAddress64 address = XBeeAddress64 (0x13A200,0x4081B77C );
_zbTx = ZBTxRequest(address, payload, sizeof(payload));
Serial.println("sending");
xbee.send(_zbTx); // The program blocks here
}
else
{
Serial.println("waiting");
xbee.readPacket(100);
if (xbee.getResponse().isAvailable())
{
Serial.println("waiting 1");
if( xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
{
Serial.println("waiting 2");
ZBRxResponse _rx;
xbee.getResponse().getZBRxResponse(_rx);
uint8_t* response= new uint8_t[50];
for(int i=0; i<_rx.getDataLength(); i++)
{
response[i] = _rx.getData()[i];
Serial.println(char(response[i]));
}
}
}
}
}
编辑(附加信息):
如果我更改负载中的值类型,它不会改变任何内容。关于波特率,两个 XBees 都配置为 57600 波特。这是 XBee 的配置:
端设备
协调员
native 串口的结果为:
最后,我使用 Arduino ATmega 1284P .我真的不知道什么样的问题可以做到这一点。
最佳答案
有一些麻烦:/
首先,默认协调器ADD是0x0 0x0,所以行where
XBeeAddress64 address = XBeeAddress64 (0x13A200,0x4081B77C );
应该是
XBeeAddress64 address = XBeeAddress64 (0x0,0x0 );
那么,Xbee 也是 57600 波特率吗?
要获得 ACK,您可以使用:
if (xbee.readPacket(1000))
{
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
{
xbee.getResponse().getZBTxStatusResponse(txStatus);
if (txStatus.getDeliveryStatus() == SUCCESS)
{
//It's sent
}
}
它也可能来自您的有效负载。您最好使用十六进制值或 int 来确定您发送的内容。
编辑:
我看到你没有使用最新版本的 Xctu。试一试,测试他们之间的直接通信,看看是否可以让 Coordinator 和 Routeur/End 设备直接联系。
关于c++ - Arduino XBee 发送数据 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27722306/