2017-04-19 66 views
0

我想在函数中使用SoftwareSerial变量,但由于某些原因它总是不可用。 下面给出了各种代码。如何在Arduino的函数中使SoftwareSerial变量可用

gsm.h 

#ifndef GSM_H_ 
#define GSM_H_ 
#include <SoftwareSerial.h> 

struct gsm{ 

    char *message; 
    char phone_number[20]; 

    void getText(SoftwareSerial serial, int index); 
}; 


#endif 


gsm.cpp 

#include "gsm.h" 
#include <string.h> 
#include <arduino.h> 
#include <SoftwareSerial.h> 

void gsm::getText(SoftwareSerial serial, int index){ 

    char str[100]; 
    serial.print("AT+CMGR=1 \r"); 
    delay(250); 
    if(serial.available()){ 
     Serial.print("serial is available"); 
     serial.readBytes(message, 100); 
    } 

} 


test.ino 

#include "gsm.h" 
#include <SoftwareSerial.h> 
#include <string.h> 
#include <stdio.h> 

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

gsm gm; 

void setup(){ 

    Serial.begin(9600); 
    mySerial.begin(9600); 
    gm.getText(mySerial,1); 
    Serial.print(gm.message); 
} 

void loop(){ 

} 

Serial.print(gm.message)应该返回什么已被serial.readBytes(消息,100)复制到消息。 但是,如果(serial.available())没有得到执行,似乎对串行可用性的测试失败,因为中的代码没有得到执行。我需要帮助,因为我一直在与此战斗一段时间。

回答

0

尝试使用此:

if (mySerial.available()>0){ 
    mySerial.read(); 
} 
+0

其不可用时@ stackmalux –

+0

试试这个简单的例子串行[https://www.arduino.cc/en/Reference/SoftwareSerialExample] – stackmalux

+0

这是测试SoftwareSerial如果以这种方式实施工作,但这不是我想要做的。 –

相关问题