我试图将Arduino UNO板与嵌入了Quectel M95模块的GSM2Click板连接起来。 Vcc = 5V已经由开关正确设置。我使用外部直流电源为电路板供电。 我已经建立了以下联系:
ARDUINO pin | GSM pin:
3 (TX) RX
2 (RX) TX
8 STA
7 PWK (always high)
gsm2click板似乎处于活动状态,因为交换机电源和网络都已打开(net 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);
}
有什么建议吗?
感谢所有人提前!
答案 0 :(得分: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);
}
如果您还有任何问题,请发表评论