Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
  2. {
  3. uint8_t x = 0, answer = 0;
  4. char response[100];
  5. unsigned long previous;
  6. memset(response, '\0', 100); // Initialice the string
  7. delay(100);
  8.  
  9. while( Serial2.available() > 0) Serial2.read(); // Clean the input buffer
  10. Serial2.println(ATcommand); // Send the AT command
  11. x = 0;
  12. previous = millis();
  13.  
  14. // this loop waits for the answer
  15. do
  16. {
  17. // if there are data in the UART input buffer, reads it and checks for the asnwer
  18. if(Serial2.available() != 0)
  19. {
  20. response[x] = Serial2.read();
  21. if (x < sizeof(response) - 1) x++;
  22. // check if the desired answer is in the response of the module
  23. if (strstr(response, expected_answer) != NULL)
  24. {
  25. answer = 1;
  26. }
  27. }
  28. // Waits for the asnwer with time out
  29. }
  30. while((answer == 0) && ((millis() - previous) < timeout));
  31. Serial.println(response);
  32. return answer;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement