Arduino代码中的常数迭代
我很好奇,软件串行和串行的波特率对我的Arduino代码的后续执行有什么影响?我注意到我的代码中有一个奇怪的行为。它不断地打印出Test1,它再也不会继续打印了。当我将波特率设置为相同的(9600)时,我不断地得到。所以我需要一个关于真正发生的事情的解释 test.inoArduino代码中的常数迭代,arduino,Arduino,我很好奇,软件串行和串行的波特率对我的Arduino代码的后续执行有什么影响?我注意到我的代码中有一个奇怪的行为。它不断地打印出Test1,它再也不会继续打印了。当我将波特率设置为相同的(9600)时,我不断地得到。所以我需要一个关于真正发生的事情的解释 test.ino #include "gsm.h" #include <SoftwareSerial.h> #include <string.h> #include <stdio.h> SoftwareSe
#include "gsm.h"
#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>
SoftwareSerial mySerial(10, 11); // RX, TX
gsm gm;
char temp[10];
void setup() {
Serial.begin(57600);
mySerial.begin(9600);
gm.getMsg(mySerial, 1);
Serial.print("Test1");
if(mySerial.available()){
mySerial.readBytes(temp, 10);
Serial.print("Test2");
}
Serial.print("Test3");
gm.getText(temp);
}
void loop() {
}
#包括“gsm.h”
#包括
#包括
#包括
软件系列mySerial(10,11);//接收,发送
gsm-gm;
煤焦温度[10];
无效设置(){
序列号。开始(57600);
mySerial.begin(9600);
gm.getMsg(mySerial,1);
串行打印(“测试1”);
if(mySerial.available()){
mySerial.readBytes(temp,10);
串行打印(“测试2”);
}
串行打印(“Test3”);
gm.getText(temp);
}
void循环(){
}
gsm.cpp
#include "gsm.h"
#include <string.h>
#include <arduino.h>
#include <SoftwareSerial.h>
void gsm::getText(char str[]) {
char *temp1 = strrchr(str, '\"')+3;
int l1 = strlen(temp1);
char *temp2 = strchr(temp1,'\n');
int l2 = strlen(temp2);
int len = l1-l2;
char msg[10];
strncpy(msg, temp1, len);
msg[len] = '\0';
strncpy(message, msg,len);
}
void gsm::getMsg(SoftwareSerial serial, int index) {
serial.print("AT+CMGR = ");
serial.print(index);
serial.print("\r\n");
delay(300);
}
#包括“gsm.h”
#包括
#包括
#包括
void gsm::getText(char str[]{
char*temp1=strrchr(str,“\”)+3;
int l1=strlen(temp1);
char*temp2=strchr(temp1,'\n');
int l2=strlen(temp2);
int len=l1-l2;
char-msg[10];
strncpy(msg、temp1、len);
msg[len]='\0';
strncpy(消息、消息、len);
}
无效gsm::getMsg(软件串行,int索引){
serial.print(“AT+CMGR=”);
连续打印(索引);
串行打印(“\r\n”);
延迟(300);
}
gsm.h
\ifndef GSM\u H_
#定义GSM_H_
#包括
结构化gsm{
字符消息[20];
void getText(char str[]);
void getMsg(软件序列,int索引);
};
#恩迪夫
您需要在串行监视器上设置波特率,以与您设置的波特率相匹配。串行。开始
(照片摄于)
如果将一个字节读入长度为1的字符数组,那么它应该是字符串吗typo@gre_gor
#ifndef GSM_H_
#define GSM_H_
#include <SoftwareSerial.h>
struct gsm{
char message[20];
void getText(char str[]);
void getMsg(SoftwareSerial serial, int index);
};
#endif