parshing USSD blue pil

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
Post Reply
beryindo
Posts: 11
Joined: Wed Oct 18, 2017 10:43 am

parshing USSD blue pil

Post by beryindo » Tue Oct 24, 2017 10:45 am

how to parshing USSD with sim800/sim900 with bluepil ?

zmemw16
Posts: 1484
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: parshing USSD blue pil

Post by zmemw16 » Tue Oct 24, 2017 4:09 pm

google USSD stm32
second result
srp

beryindo
Posts: 11
Joined: Wed Oct 18, 2017 10:43 am

Re: parshing USSD blue pil

Post by beryindo » Wed Oct 25, 2017 3:35 pm

can't found sir
this my code sir, but responUSSD can't show respon

Code: Select all

#define Sim800LSerial Serial1
#define DEFAULT_TIMEOUT          5   //seconds
#define DEFAULT_INTERCHAR_TIMEOUT 3000   //miliseconds
enum DataType {
  CMD     = 0,
  DATA    = 1,
};
char responUSSD[] = "";

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  delay(1000);
  Serial.println("test USSD");
  sendUSSD("*888#", "OK", responUSSD);
  Serial.println(responUSSD);
}

void loop() {
  // put your main code here, to run repeatedly:

}

bool sendUSSD(char *ussdCommand, char *resultcode, char *response)
{
    byte i = 0;
    char gprsBuffer[200];
    char *p,*s;
    sim800L_clean_buffer(response, sizeof(response));
   
    sim800L_flush_serial();
    sim800L_send_cmd("AT+CUSD=1,\"");
    sim800L_send_cmd(ussdCommand);
    sim800L_send_cmd("\"\r");
    if(!sim800L_wait_for_resp("OK\r\n", CMD,DEFAULT_TIMEOUT,DEFAULT_INTERCHAR_TIMEOUT))
        return false;
//    Serial.println(gprsBuffer);    
    sim800L_clean_buffer(gprsBuffer,200);
    sim800L_read_buffer(gprsBuffer,200,DEFAULT_TIMEOUT,DEFAULT_INTERCHAR_TIMEOUT);
    if(NULL != ( s = strstr(gprsBuffer,"+CUSD: "))) {
        *resultcode = *(s+7);
        resultcode[1] = '\0';
        if(!('0' <= *resultcode && *resultcode <= '2'))
            return false;
        s = strstr(s,"\"");
        s = s + 1;  //We are in the first phone number character
        p = strstr(s,"\""); //p is last character """
        if (NULL != s) {
            i = 0;
            while (s < p) {
              response[i++] = *(s++);
            }
            response[i] = '\0';            
        }
        return true;
    }
    return false;
}

boolean sim800L_wait_for_resp(const char* resp, DataType type, unsigned int timeout, unsigned int chartimeout)
{
  int len = strlen(resp);
  int sum = 0;
  unsigned long timerStart, prevChar;    //prevChar is the time when the previous Char has been read.
  timerStart = millis();
  prevChar = 0;
  while (1) {
    if (sim800L_check_readable()) {
      char c = Sim800LSerial.read();
      prevChar = millis();
      sum = (c == resp[sum]) ? sum + 1 : 0;
      if (sum == len)break;
    }
    if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
      return false;
    }
    //If interchar Timeout => return FALSE. So we can return sooner from this function.
    if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
      return false;
    }

  }
  //If is a CMD, we will finish to read buffer.
  if (type == CMD) sim800L_flush_serial();
  return true;
}

void sim800L_send_byte(uint8_t data)
{
  Sim800LSerial.write(data);
}

void sim800L_send_char(const char c)
{
  Sim800LSerial.write(c);
}

void sim800L_send_cmd(const char* cmd)
{
  for (uint16_t i = 0; i < strlen(cmd); i++)
  {
    sim800L_send_byte(cmd[i]);
  }
}

int sim800L_check_readable()
{
  return Sim800LSerial.available();
}

void sim800L_flush_serial()
{
  while (sim800L_check_readable()) {
    Sim800LSerial.read();
  }
}

void sim800L_clean_buffer(char *buffer, int count)
{
  for (int i = 0; i < count; i++) {
    buffer[i] = '\0';
  }
}

void sim800L_read_buffer(char *buffer, int count, unsigned int timeout, unsigned int chartimeout)
{
  int i = 0;
  unsigned long timerStart, prevChar;
  timerStart = millis();
  prevChar = 0;
  while (1) {
    while (sim800L_check_readable()) {
      char c = Sim800LSerial.read();
      prevChar = millis();
      buffer[i++] = c;
      if (i >= count)break;
    }
    if (i >= count)break;
    if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
      break;
    }
    //If interchar Timeout => return FALSE. So we can return sooner from this function. Not DO it if we dont recieve at least one char (prevChar <> 0)
    if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
      break;
    }
  }
}

Post Reply