Go Down

Topic: Serial data over GSM socket connection (Read 803 times) previous topic - next topic

fbriggs4

I am working with a Telit GSM modem and I have RX/TX and GND connected as required. Communication between the Arduino and Telit work perfectly and I can send SMS and E-mail with no problems. The serial connection transmits and receives all data perfectly under normal conditions. However, I am now doing a socket connection using the AT#SKTD command which opens a connection on a specific port on my server. When I open a socket connection the Telit goes into data mode and requires hardware flow control otherwise it wont pass any of the data to the serial. I bypass this in the software by disabling flow control. When I connect to the GSM modem directly to my computer using an FTDI I can transmit and receive properly with the server so long as I disable flow control first. However, when I connect the Arduino I get some very strange results. The Arduino can transmit perfectly and the server does receive what the Arduino outputs on the serial. I also seem to be able to receive data fro the server but the received bytes do not seem to populate into my char array. At least not in the way I expect.....

If I open a socket connection the server will automatically reply and the following code is how I receive the data. For example if the server replied "Hello World" I would get the output you see at the bottom.

Code: [Select]

char serialData[100];
char inByte;
int idx = 0;

while (Serial.available() > 0){
  inByte = Serial.read();
  serialData[idx] = inByte;
  serialData[idx+1] = '\0';
  idx++;
  mySerial.print(inByte);  //hardware UART is used for GSM modem so I output debug data to software serial.
  mySerial.print("this is serialData ");
  mySerial.println(serialData);
  }
//Output which clearly shows that I get "Hello World" but serialData is empty.

Hthis is serialData
ethis is serialData
lthis is serialData
lthis is serialData
othis is serialData
this is serialData
Wthis is serialData
othis is serialData
rthis is serialData
lthis is serialData
dthis is serialData


Since I see the "Hello World" output I know I am getting it. But for some reason it does not get populated into the array. I ONLY have this issue when I open a socket connection with the server. As soon as the socket connection is closed the Arduino populates the incoming serial data into the array as I expect. I have tried to print serialData inside the loop as you see above and outside the loop with the same results. Any suggestions on what could cause the serial to read the bye but fail to populate it into the array is greatly appreciated. 

PaulS

Printing the value of idx would be a good idea. Posting ALL of your code would be an even better idea.

fbriggs4

printing idx is a very good idea. I tried that and it does increment like it should.

I get the following Arduino output and the server transmitted "VektorTek 0.1".

Code: [Select]

this is idx 1
this is serialData
this is idx 2
this is serialData
Vthis is idx 3
this is serialData
ethis is idx 4
this is serialData
kthis is idx 5
this is serialData
tthis is idx 6
this is serialData
othis is idx 7
this is serialData
rthis is idx 8
this is serialData
Tthis is idx 9
this is serialData
ethis is idx 10
this is serialData
kthis is idx 11
this is serialData
this is idx 12
this is serialData
0this is idx 13
this is serialData
.this is idx 14
this is serialData
1this is idx 15
this is serialData

this is idx 16
this is serialData


Here is the entire code, it requires some explanation though. It is a bread board Arduino running 8mhz internal with a 32.768 crystal which increments the timer variable every 8 seconds.

Code: [Select]

#include <SoftwareSerial.h> //used for the GPS port 5 which is GPS TX
SoftwareSerial mySerial(5, 2); // RX, TX MAKE SURE TO CHANGE THE TX PORT FOR PRODUCTION

int timer = 0;

SIGNAL(TIMER2_OVF_vect){
  timer++;

}

char serialData[100];
char inByte;
int i = 0;
int idx = 0;
boolean reply = false;
char hello[10] = "hello";
boolean check = false;
boolean reg = false;
int test;
boolean IP = false;
boolean banner = false;

void setup(){
  mySerial.begin(9600);
  delay(2000);
  mySerial.println("testing the port");
  Serial.begin(9600);
  pinMode(A2, OUTPUT);
  digitalWrite(A2, LOW); // needs to be LOW to be on, HIGH is off
  delay(2000);
  pinMode(A0, OUTPUT);
  digitalWrite(A0, LOW); // needs to be LOW for 3 seconds to turn on the GSM modem
  delay(2000); //
  pinMode(A0, INPUT);
  digitalWrite(A0, HIGH);
  delay(3000); //wait for the GSM modem to fully wake up before sending any commands.
  mySerial.println("testing the port2");



 
}

void loop(){
 
    signal();
    if (IP == true){
      socket();
    }
    delay(15000); //delay to keep it from looping quickly
   
//       
    }
   

  void socket(){
    banner = false;
    idx = 0;
    timer = 0;
    Serial.println("AT#SKTD=0,4444,46.102.168.139"); //open the socket connection to the server
    mySerial.println("AT#SKTD=0,4444,46.102.168.139");
    delay(1000);
    while (banner == false && timer < 10){
      while (Serial.available() > 0){
        inByte = Serial.read();
        serialData[idx] = inByte;
        serialData[idx+1] = '\0';
        idx++;
        mySerial.print(inByte);
        mySerial.print("this is idx ");
        mySerial.println(idx);
        mySerial.print("this is serialData ");
        mySerial.println(serialData);
        if (inByte == 10){
          idx = 0;
          mySerial.println("got an LF and checking to see if the string matches");
          mySerial.println(serialData);
          if (strncmp(serialData, "CONNECT", 6) ==0){
            mySerial.println("CONNECT matched");
            delay(3000);
            Serial.println("0123456789");
            mySerial.println("0123456789");
            //banner = true;
            //delay(2000);
          }
          if (strncmp(serialData, "Option:", 6) ==0){
            Serial.println("option matched");
          }
        }
      }
    }
  } 
  void signal(){
    delay(500);
    Serial.println("AT");
    delay(1000);
    Serial.println("ATE0"); //disable echo
    delay(1000);
    Serial.println("AT#SIMDET=1"); //turn on the SIM card
    mySerial.println("AT#SIMDET=1");
//    delay(2000);
//    Serial.println("AT\\Q0"); //disable flow control
//    mySerial.println("AT\\Q0");
    delay(2000);
    Serial.println("AT+CMGF=1");
    mySerial.println("AT+CMGF=1");
   
    delay(17000); //delay to allow time for network registration
    Serial.println("AT+CGDCONT=1,IP,vfinternet.au");
    mySerial.println("AT+CGDCONT=1,IP,vfinternet.au");
    delay(2000);
    Serial.println("AT\\Q0");
    mySerial.println("AT\\Q0");
    delay(2000);
    Serial.println("AT#GPRS=1"); //open a GPRS connection and get an IP
    mySerial.println("AT#GPRS=1");
    timer = 0;
    idx = 0;
    IP = false;
    while (IP == false && timer < 2){
      while (Serial.available() > 0){
        inByte = Serial.read();
        serialData[idx] = inByte;
        serialData[idx+1] = '\0';
        idx++;
        mySerial.write(inByte);
        //mySerial.print(serialData);
        if (inByte == 10){
          idx = 0;
          mySerial.println("got an LF and checking to see if the string matches");
          mySerial.println(serialData);
          mySerial.println(test);
          if (strncmp(serialData, "+IP: ", 4) ==0){
            mySerial.println("+IP matched");
            IP = true;
            delay(2000);
          }
        }
        }
      }
  }

   
   


PaulS

Quote
this is idx 1
this is serialData
this is idx 2
this is serialData
Vthis is idx 3
this is serialData

There is nothing printed in front of the first "this is idx" statement. I'm wondering if the device sent a NULL which causes printing the string (NULL-delimited array of chars) to stop prematurely. I think that you'll need to verify that the character received is a useful character before adding it to the array and incrementing idx.

Go Up