问题描述
我编写的代码如下
I have write code as follow
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11); // RX, TX
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
softSerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
at_command(01,'S','L',"");
if(softSerial.available()){
Serial.print(softSerial.read());
}
}
// Sending AT command to Connected radio
void at_command(char id, char c1, char c2, char *cmdData) {
// Start delimiter
softSerial.write(0x7E); // [Fixed]
// paylode length [Depend on length of frame minimum 5]
softSerial.write((byte)0x00); // MSB
softSerial.write(0x05); // LSB
// Frame specifid data (Pay load)
softSerial.write(0x88); // Frame type [Fixed]
softSerial.write(id); // Frame ID [Unique ID to veryfy response. if =0 will not get any response]
// AT Command
softSerial.write(c1); // First Charecter
softSerial.write(c2); // Second Charecter
// Command data
long cmdDataSum = 0;
if(sizeof(cmdData) > 0) {
for(int i=0; i<= sizeof(cmdData)-1; i++) {
softSerial.write(i); // [maximum 256 charecters]
cmdDataSum = cmdDataSum + i;
}
} else { // do nothing
;
}
// Checksum
long sum = 0x88 + id + c1 + c2 + cmdDataSum;
softSerial.write(0xFF - (sum - 0xFF));
// just for a sake of guard
delay(10);
}
// Receiving AT command response from Radio
void at_response(){
// Received AT comman responce from Coordinator
if(Serial.available() > 21 ) {
if(Serial.read() == 0x7E) {
for (int i=0; i<19; i++) {
byte discard = Serial.read();
}
byte atResponse = Serial.read();
}
}
}
现在假设我将向XBee发送一些命令返回AT命令响应以读取我必须使用循环,就像我在at_response()函数中所做的一样,并将我正在读取的值作为检查,如
Now suppose if i will send some command to XBee that will return AT Command Response to read which i have to use a loop as i have done in at_response() function along with putting values i am reading as check as in
if(Serial.available() > 21 )
。如果XBee发送一些可变长度的响应并且我必须阅读它会怎么样?
我希望问题很清楚!让我再次解释一下,我想通过Zigbee在串行上读取一些可变长度响应,因为我通过
. What if XBee send some variable length response and i have to read that?
I hope question is clear! let me explain once more that i want to read some variable length response through Zigbee on serial as i have done with fixed length response by
if(Serial.available() > 21 )
谁能告诉我怎么做?
推荐答案
这篇关于我想知道如何知道有多少字节Xbee的Zigbee已经发送,以便我可以接收串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!