2014-04-02 39 views
1

我試圖連接嵌入了Quectel M95模塊的GSM2Click板的Arduino UNO板。 Vcc = 5V已由開關正確設置。我用一個外部直流電源爲電路板供電。 我做了以下連接:Arduino和GSM2Click板(Quectel M95)

 ARDUINO pin     |   GSM pin: 
        3 (TX)            RX         
        2 (RX)            TX 
        8             STA 
        7             PWK (always high) 

的gsm2click板似乎是積極的,因爲無論是開關電源和網絡上(淨LED閃爍)。 我試圖讓發送AT命令,但總是得到0作爲答案:

#include <SoftwareSerial.h> 

const int PWK= 7; // Analog output pin that the LED is attached to 
const int STA= 8; 
const int RX=2; 
const int TX=3; 
int STA_value; 



SoftwareSerial mySerial(RX,TX); // RX, TX 


void setup() {     
    // initialize the digital pin as an output. 
    pinMode(PWK, OUTPUT); // questo pin serve per accendere il dispositivo GSM 
    pinMode(STA, INPUT); 

    pinMode(RX,INPUT); 
    pinMode(TX,OUTPUT); 


    gsmOn(); 


    Serial.begin(9600); //Init seriale sw 
    Serial.println("Arduino serial initialized!"); 
    delay(10); 

    mySerial.begin(9600); //init seriale hw 
    Serial.println("Software serial initialized!"); 
    delay(10); 



} 




// the loop routine runs over and over again forever: 
    void loop() 
    { 

     sendCommand("AT"); 
     delay(10); 

     readSerial(); 
     delay(1000);   

    } 


void sendCommand(char* msg) 
{ 
    mySerial.print(msg); 
    Serial.println(msg); 
    delay(1); 

} 

void readSerial() 
{ 
    while(mySerial.available()>0) 
    { 
    Serial.print(mySerial.read()); 
    delay(1); 
    } 


} 



void gsmOn() 
{ 
    // Takes 30 seconds to complete 
    digitalWrite(PWK, HIGH); // turn the Phone on 
    delay(2); 
} 

什麼建議嗎?

感謝大家提前!

回答

2

這裏有一些事情你的代碼錯誤,所以我將代碼粘貼後修改這一點,並試圖瞭解我想告訴ü

#include <SoftwareSerial.h> 

const int PWK= 7; // Analog output pin that the LED is attached to 
const int STA= 8; 
const int RX=2; 
const int TX=3; 
int STA_value; 



SoftwareSerial mySerial(RX,TX); // RX, TX 


void setup() {     
// initialize the digital pin as an output. 
pinMode(PWK, OUTPUT); // questo pin serve per accendere il dispositivo GSM 
pinMode(STA, INPUT); 

//pinMode(RX,INPUT); //if u want to use this as serial port then don't declare this I/O 
//pinMode(TX,OUTPUT); 


gsmOn(); 


Serial.begin(9600); //Init seriale sw 
Serial.println("Arduino serial initialized!"); 
delay(10); 

mySerial.begin(9600); //init seriale hw 
Serial.println("Software serial initialized!"); 
delay(10); 
} 

// the loop routine runs over and over again forever: 
void loop() 
{ 
uint8_t answer=0; 

// checks if the module is started 
answer = sendCommand("AT", "OK", "OK", 2000); 
if (answer == 1) 
{ 
Serial.println("OK"); 
} 
    else 
Serial.println("Not Responding"); 
    delay(1000);   
    } 
int8_t sendCommand(char* ATcommand, char* expected_answer1, 
    char* expected_answer2, unsigned int timeout){ 
uint8_t x=0, answer=0; 
char response[100]; 
unsigned long previous; 
memset(response, '\0', 100); // Initialize the string 
delay(100); 
while(mySerial.available() > 0) 
{ 
mySerial.read(); // Clean the input buffer 
} 
Serial.println(ATcommand); // Send the AT command to serial port 
mySerial.println(ATcommand); // Send the AT command to gsm module 
previous = millis(); 
// this loop waits for the answer 
do{ 
    // if there are data in the UART input buffer, reads it and checks for the asnwer 
    if(mySerial.available() != 0){  
     response[x] = mySerial.read(); 
     x++; 
     // check if the desired answer 1 is in the response of the module 
     if (strstr(response, expected_answer1) != NULL)  
     { 
      answer = 1; 
     } 
     // check if the desired answer 2 is in the response of the module 
     else if (strstr(response, expected_answer2) != NULL)  
     { 
      answer = 2; 
     } 
    } 
} 
// Waits for the asnwer with time out 
while((answer == 0) && ((millis() - previous) < timeout)); 

return answer; 
    } 
void gsmOn() 
{ 
// Takes 30 seconds to complete 
digitalWrite(PWK, HIGH); // turn the Phone on 
delay(2); 
} 

,如果仍然您有任何問題,請留下評論

+0

我試過你的代碼,它按預期工作。不過,我有一個評論,你爲什麼要檢查2個可能的答案?也許模塊有時會給出一個不好的答案?對於您決定使用函數strstr()進行比較,是否有特定含義? – divivoma

+1

其實@divivoma這是一個大型代碼(項目)的一部分,我們發送這樣的AT命令,期望兩個答案,這將有助於您日後進一步處理和使用GSM模塊做一些不錯的東西,否則你可以避免1個答案。沒有使用strstr的特殊原因,但這是兩個字符串比較和易於使用的內置函數。如果你的問題得到解決,那麼你應該投票回答這個問題 –

+0

好的。現在很清楚。對不起,但我不能將它作爲答案投票,因爲我的聲望仍然很低。我會盡快做到這一點。 – divivoma