Hi Everyone,
In the following code, I have created an array of max index 200 and also a String object of similar maximum length. The array gets populated from GSM module data. This populated array is then copied to the String object. Although both holds same data, but they display different length. Serial1 is interfaced with a GSM module and Serial2 in connected with my PC Serial port. I am using Arduino Mega 2560.
Kindly provide answer asap.
Best regards,
Nick.
#define maxLength 200
String STR_BUFFER = String(maxLength); // allocate a new String
char RX_BUFFER[200];
unsigned int RX_BYTE_COUNT=0;
unsigned int TX_BYTE_COUNT=0;
unsigned int TOTAL_RX_BYTES = 0;
unsigned char RX_SER_BYTE=0;
unsigned char RX_STATE=0;
boolean RX_COMPLETE = false;
boolean START_BUFFERING = false;
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
unsigned int ONBOARD_LED = 13;
unsigned int GSM_START = 7;
void setup()
{
// initialize the digital pin as an output.
pinMode(ONBOARD_LED, OUTPUT);
pinMode(GSM_START, OUTPUT);
digitalWrite(GSM_START, HIGH);
delay(1000);
Serial1.begin(9600);
Serial2.begin(9600);
Serial2.println(" ");
Serial2.println("Starting Communication...");
RX_STATE=0;
RX_COMPLETE = false;
digitalWrite(GSM_START, LOW);
delay(1000);
digitalWrite(GSM_START, HIGH);
}
void loop()
{
// read from port 1, send to port 2:
if (Serial1.available())
{
RX_SER_BYTE = Serial1.read();
RX_BUFFER[RX_BYTE_COUNT]=RX_SER_BYTE;
RX_BYTE_COUNT++;
//RX_COMPLETE=false;
if(RX_BUFFER[RX_BYTE_COUNT-1]==0x0A)
{
if(RX_BUFFER[RX_BYTE_COUNT-2]==0x0D)
{
RX_COMPLETE=true;
TOTAL_RX_BYTES=RX_BYTE_COUNT;
RX_BYTE_COUNT=0;
}
}
}
if(RX_COMPLETE==true && TOTAL_RX_BYTES>2)
{
for(TX_BYTE_COUNT=0;TX_BYTE_COUNT<TOTAL_RX_BYTES-1;TX_BYTE_COUNT++)
{
Serial2.print(RX_BUFFER[TX_BYTE_COUNT]);
}
STR_BUFFER = RX_BUFFER;
Serial2.println(TOTAL_RX_BYTES);
Serial2.println(STR_BUFFER.length());
TOTAL_RX_BYTES=0;
RX_COMPLETE=false;
}
}
output
RDY
5
5
+CFUN: 1
10
10
+CPIN: READY
14
14
Call Ready
12
14