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
|
#include <SoftwareSerial.h>
#include <iostream>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;
using namespace std;
void setup()
{
pinMode(test,INPUT);
pinMode(13,OUTPUT);
Serial.begin(19200); // Open serial communications and wait for port to open:
delay(1500);
Serial.println("Ready to work!");
mySerial.begin(19200);
delay(1500);
}
void loop()
{
if (digitalRead(test)==HIGH)
{
char response[5];
char finalPart[2];
mySerial.println("AT");
delay(2000); //
if (mySerial.available() >4)
{
for (int i=0;i<6;i++)
{
response[i]=mySerial.read();
}//end for i
Serial.print(millis() /1000);
Serial.print("this is the full respons - ");
Serial.println(response);
finalPart[0]=response[0];
finalPart[1]=response[1];
finalPart[2]=('\n');
Serial.print("this is final - ");
Serial.print(finalPart[0]);
Serial.println(finalPart[1]);
Serial.println(finalPart);
}
if ((finalPart[0]=='O')||(finalPart[1]=='K'))
{//what to do answer OK
digitalWrite (13,HIGH);
Serial.print(millis() /1000);
Serial.println("Modem is working!!!!");
delay (1500);
digitalWrite(13,LOW);
Serial.println("******************");
}// end answer OK
else
{//what to do if modem not ready
Serial.print(millis() /1000);
Serial.println (" -Modem not ready yet......");
Serial.println("******************");
} //end modem not ready
char buffer[30];
int pos = 0;
while (parseResponse(response, buffer, pos, 10))
{
cout << buffer << endl;
}
}//end if
}//end loop
bool parseResponse(char * response, char * buffer, int & pos, int size);
int main()
{
char response[10] = { 65, 84, 13, 10, 13, 10, 79, 75, 13, 10 };
char buffer[30];
int pos = 0;
while (parseResponse(response, buffer, pos, 10))
{
cout << buffer << endl;
}
}
bool parseResponse(char * response, char * buffer, int & pos, int size)
{
int n = 0;
// ignore any leading CR or LF
while (pos < size && (response[pos] == '\r' || response[pos] == '\n'))
pos++;
// copy characters until CR or LF is reached
while (pos < size && response[pos] != '\r' && response[pos] != '\n')
{
buffer[n] = response[pos];
pos++;
n++;
}
buffer[n] = 0; // add the null terminator;
return (pos < size);
}
|