Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 typedef struct { int len; char api_identifier; long DH; long DL; int source_addr_16bit; char options; char frame_id; char AT_com[2]; char status; int data_len=10; // stores the length of the data char data[20]; // also stores the "value" for at command response char checksum; } RxPacket; . . . . . . char buff[ RxPacket.data_len];
I'd taken structure of few data, when i'm trying to access data_len by it's pointer through array then IDE shows error
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 #include<lpc214x.h> //Includes LPC2148 register definitions #include<stdio.h> #include<stddef.h> #include<stdlib.h> #include<string.h> #include<math.h> //************************************************************************************************************* typedef struct { int len; char api_identifier; long DH; long DL; int source_addr_16bit; char options; char frame_id; char AT_com[2]; char status; char data_len; // stores the length of the data char data[20]; // also stores the "value" for at command response char checksum; } RxPacket; typedef struct { long sl; long sh; }addr64; //************************************************************************************************************* #define Fosc 12000000 #define Fcclk (Fosc * 5) #define Fcco (Fcclk * 4) #define Fpclk (Fcclk / 4) * 1 #define UART_BPS 9600 //Set Baud Rate here #define TX_BUFFER_IS_FULL() ((U0LSR & (1 << U0THR)) == 0) #define RX_BUFFER_IS_FULL() ((U0LSR & (1 << U0RBR)) == 0) #define XBEE_UDR U0RBR #define UART0_CH_NUM 6 #define VIC_UART0_CH (0x01 << UART0_CH_NUM) #define VIC_INT_CH_EN (0x01 << 5) #define TOGGLE_LED() IO0SET = (1 << 2) #define TOGGLE_LED0(); IO0CLR=(1<<2) #define hex rx_data.data_len //****************************************************************************************************************** void Delay_Ticks(unsigned int Delay); void Init_UART0(void); void UART0_SendByte(unsigned char data); void UART0_SendStr(unsigned char msg[]); int XbeeInit(); void ZigBee_TX_Request(char Frame_ID, long DH, long DL, int _16bitAddr, char Hops, char Options, char *RF_Data, int len ); void send_Msg(char *data, int len); void setNewPacketCB(void (*funptr)()); void gas(); void receive_Msg(RxPacket *rx_data); RxPacket getPacket(); //************************************************************************************************************ RxPacket rx_pkt; // Global Variables char RSSI; int MY; int cb; // callback flag -- set if a callback function for new packets has been specified void (*cbFunPtr)(void); // callback function pointer char test[] = {'T', 'E', 'S', 'T'}; //************************************************************************************************************************* void UART0_ISR(void) __irq { if(XBEE_UDR == 0x7E) { RxPacket pkt; //TOGGLE_LED(); receive_Msg(&pkt); rx_pkt = pkt; // Call function for responding to new packet if(cb) (*cbFunPtr)(); //TOGGLE_LED0(); gas(); } VICVectAddr = 0x00; /* ACK the VIC */ } int main(void) { PINSEL0 = 0x00000005; // Enable UART0 Rx and Tx pins PINSEL1 = 0x00000000; PINSEL2 = 0x00000000; XbeeInit(); ZigBee_TX_Request(0x10, 0x13A200, 0x40B0E9FE, 0xFFFE, 0x01, 0x01, test, 4); //flexsize(10); VICVectAddr8 = (unsigned int) UART0_ISR; // setNewPacketCB(gas); while(1) { } return(0); } //**************************************************************************************************************** void Delay_Ticks(unsigned int Delay) //Function to generate finite delay { unsigned int i; for(; Delay>0; Delay--) for(i=0; i<50000; i++); } void Init_UART0(void) //This function setups UART0 { unsigned int Baud16; U0LCR = 0x83; // DLAB = 1 Baud16 = (Fpclk / 16) / UART_BPS; U0DLM = Baud16 / 256; U0DLL = Baud16 % 256; U0LCR = 0x03; U0FCR |= 0x01; /* Configure the VIC for Interrupt Handling */ VICVectCntl8 |= (VIC_INT_CH_EN | UART0_CH_NUM); VICIntSelect &= (~VIC_UART0_CH); /* This is optional to ensure the IRQ Mode */ VICIntEnable |= VIC_UART0_CH; /* Enabel the Interrupt for listening request */ /* Enable UART Interrupt */ U0IER |= 0x01; } void UART0_SendByte(unsigned char data) //A function to send a byte on UART0 { U0THR = data; while( (U0LSR&0x40)==0 ); } void UART0_SendStr(unsigned char msg[]) //A function to send a string on UART0 { while(*msg!='\0') { UART0_SendByte(*msg); msg++; } } int XbeeInit() { Init_UART0(); return 1; } //******************************* tx frame ************************************************************** void ZigBee_TX_Request(char Frame_ID, long DH, long DL, int _16bitAddr, char Hops, char Options, char *RF_Data, int len ) { int i; // counting variable //char *buff = (char*) malloc(sizeof(char*)*(20)); //temporary buffer for transmitting char buff[20]; buff[0] = 0x10; // Identifies the UART data frame for the host to correlate with a // subsequent ACK (acknowledgment). Setting Frame ID to ‘0' will disable response frame. buff[1] = Frame_ID; // MSB first, LSB last. Broadcast = 0x000000000000FFFF buff[2] = (DH >> 24); buff[3] = (DH >> 16); buff[4] = (DH >> 8); buff[5] = DH; buff[6] = (DL >> 24); buff[7] = (DL >> 16); buff[8] = (DL >> 8); buff[9] = DL; // 16 bit address buff[10] = (_16bitAddr >> 8); buff[11] = _16bitAddr; // Number of hops for message to take buff[12] = Hops; // Options buff[13] = Options; for(i = 0; i < len; i++) buff[14+i] = RF_Data[i]; send_Msg(buff, 14+len); } void send_Msg(char *data, int len) { //Generate checksum char checksum; int counter = 0, sum = 0; for(counter = 0; counter <= len - 1; counter++) sum += data[counter]; //Checksum is calculated by adding the data values together, and subtracting the //last 8 bits from 0xFF. checksum = 0xFF - (sum & 0x00FF); //Transmit data UART0_SendByte(0x7E); //Start delimiter UART0_SendByte(8 >> len); //Length MSB UART0_SendByte(len); //Length LSB for(counter = 0; counter <= len - 1; counter++) //Transmit data UART0_SendByte(data[counter]); UART0_SendByte(checksum); //Transmit checksum } //******************************************** rx packet ************************************************* void setNewPacketCB(void (*funptr)()) { cb = 1; cbFunPtr = funptr; } void receive_Msg(RxPacket *rx_data) { int count; char temp; rx_data->data_len = 0; while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR temp = XBEE_UDR; //next incoming byte is the MSB of the data size while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR rx_data->len = (temp << 8) | XBEE_UDR; //merge LSB and MSB to obtain data length while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR rx_data->api_identifier = XBEE_UDR; switch(rx_data->api_identifier) // Select proper sequence for receiving various packet types(getting api identifier) { case 0x90: // Zigbee Receive Packet (Series 2 only) 0x90 --> receive request for(count = 1; count < rx_data->len; count++) { while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR if(count == 1) rx_data->DH = ((long)XBEE_UDR << 24); else if(count == 2) rx_data->DH |= ((long)XBEE_UDR << 16); else if(count == 3) rx_data->DH |= ((long)XBEE_UDR << 8); else if(count == 4) rx_data->DH |= (long)XBEE_UDR; else if(count == 5) rx_data->DL = ((long)XBEE_UDR << 24); else if(count == 6) rx_data->DL |= ((long)XBEE_UDR << 16); else if(count == 7) rx_data->DL |= ((long)XBEE_UDR << 8); else if(count == 8) rx_data->DL |= (long)XBEE_UDR; else if(count == 9) rx_data->source_addr_16bit = (int)(XBEE_UDR << 8); else if(count == 10) rx_data->source_addr_16bit = (int)XBEE_UDR; else if(count == 11) rx_data->options = XBEE_UDR; else { rx_data->data[count - 12] = XBEE_UDR; rx_data->data_len++; } } UART0_SendByte(rx_data->data_len); while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR rx_data->checksum = XBEE_UDR; //store checksum break; default: break; } } void gas() { int i=0; char temp; RxPacket rx_data = getPacket(); //UART0_SendByte(rx_data.data_len); // ZigBee_TX_Request(0x00, 0, 0x00FFFF, 0, 0x01, 0x01, test, 4); [B]unsigned char buff1[rx_data.data_len];[/B] //----------------->(getting error at this point) for(i=0;i<rx_data.data_len;i++) { temp = (rx_data.data[i]); buff1[i]=temp; } UART0_SendStr(buff1); VICVectAddr8 = (unsigned int) UART0_ISR; } RxPacket getPacket() { return rx_pkt; } //####################################################################
void gas()
{
int i=0;
char temp;
RxPacket rx_data = getPacket();
//UART0_SendByte(rx_data.data_len);
// ZigBee_TX_Request(0x00, 0, 0x00FFFF, 0, 0x01, 0x01, test, 4);
[B]unsigned char buff1[rx_data.data_len];[/B] // -->(getting error at this point)
for(i=0;i<rx_data.data_len;i++)
{
temp = (rx_data.data[i]);
buff1[i]=temp;
}
UART0_SendStr(buff1);
VICVectAddr8 = (unsigned int) UART0_ISR;
}
Yes, thanks for remembering.Variable length arrays are a C99 feature
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?