- 제 목 serial
- 작성자 관리자 등록일 2025-07-28/15:42 조회수 1
#include <HardwareSerial.h>
//HardwareSerial SIM7670Serial(16, 17); // RX, TX
#define SIM7670Serial Serial2
void setup() {
Serial.begin(115200);
SIM7670Serial.begin(115200);
}
void loop() {
sendCMD();
delay(2000);
}
void sendCMD() {
String res="";
res= sendATCommand("AT\n" , "OK", 1000);
Serial.println(res);
res= sendATCommand("AT+CGSN=1\n", "OK", 1000);
Serial.println(res);
res= sendATCommand("AT%XICCID\n", "OK", 1000);
Serial.println(res);
res= sendATCommand("AT+CFUN?\n", "OK", 1000);
Serial.println(res);
}
String sendATCommand(String cmd, String expectedResponse, int timeout) {
SIM7670Serial.print(cmd);
Serial.println(cmd);
String response = "";
long timeStart = millis();
while (millis() - timeStart < timeout) {
while (SIM7670Serial.available() > 0) {
char c = SIM7670Serial.read();
response += c;
}
if (response.indexOf(expectedResponse) != -1)
break;
else expectedResponse="";
}
// Serial.println(response);
return expectedResponse;
}
//HardwareSerial SIM7670Serial(16, 17); // RX, TX
#define SIM7670Serial Serial2
void setup() {
Serial.begin(115200);
SIM7670Serial.begin(115200);
}
void loop() {
sendCMD();
delay(2000);
}
void sendCMD() {
String res="";
res= sendATCommand("AT\n" , "OK", 1000);
Serial.println(res);
res= sendATCommand("AT+CGSN=1\n", "OK", 1000);
Serial.println(res);
res= sendATCommand("AT%XICCID\n", "OK", 1000);
Serial.println(res);
res= sendATCommand("AT+CFUN?\n", "OK", 1000);
Serial.println(res);
}
String sendATCommand(String cmd, String expectedResponse, int timeout) {
SIM7670Serial.print(cmd);
Serial.println(cmd);
String response = "";
long timeStart = millis();
while (millis() - timeStart < timeout) {
while (SIM7670Serial.available() > 0) {
char c = SIM7670Serial.read();
response += c;
}
if (response.indexOf(expectedResponse) != -1)
break;
else expectedResponse="";
}
// Serial.println(response);
return expectedResponse;
}