strcmp 脚本读取串行监视器失败
strcmp script reading serial monitor failing
我有一个脚本读取串行监视器并寻找“OK”响应。我能够在名为 message 的变量中捕获 OK 响应并将其打印到串行监视器,但是当我尝试在 if 语句中使用 message 变量时,它没有按预期执行。当变量 message = OK 时,下面的语句仍然给出 false。有谁知道问题出在哪里?
if (strcmp (message,"OK") == 0) {
Serial.println("true");
}
else {
Serial.println("false");
}
完整代码:
//#include <HardwareSerial.h>
const unsigned int MAX_MESSAGE_LENGTH = 12;
void setup() {
Serial2.begin(115200,SERIAL_8N1); //open modem serial port
Serial.println("serial ports are open");
}
void loop() {
Serial2.write("AT\r\n");
while (Serial2.available() > 0){
//Create a place to hold the incoming message
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
char inByte = Serial2.read();
//Message coming in (check not terminating character) and guard for over message size
if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
{
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else
{
//Add null character to string
message[message_pos] = '[=11=]';
//Print the message (or do other things)
Serial.println("loop");
delay(100);
Serial.println(message);
//Reset for the next message
message_pos = 0;
if (strcmp (message,"OK") == 0) {
Serial.println("true");
}
else {
Serial.println("false");
}
}
}
delay(5000);
}
串行监视器如下所示:
22:11:51.970 -> serial ports are open
22:12:16.984 -> loop
22:12:17.078 -> AT
22:12:17.078 -> false
22:12:17.078 -> loop
22:12:17.171 -> OK
22:12:17.171 -> false
22:12:17.171 -> loop
22:12:17.266 -> AT
22:12:17.266 -> false
22:12:17.266 -> loop
22:12:17.360 -> OK
22:12:17.360 -> false
收到的回复是 "OK\r\n"
,所以实际消息是 "OK\r"
。如果您临时打印每个接收到的字符,它有助于调试。
要忽略命令的回显,为其插入另一个 strcmp()
并做出相应的反应,例如什么都不做。
我有一个脚本读取串行监视器并寻找“OK”响应。我能够在名为 message 的变量中捕获 OK 响应并将其打印到串行监视器,但是当我尝试在 if 语句中使用 message 变量时,它没有按预期执行。当变量 message = OK 时,下面的语句仍然给出 false。有谁知道问题出在哪里?
if (strcmp (message,"OK") == 0) {
Serial.println("true");
}
else {
Serial.println("false");
}
完整代码:
//#include <HardwareSerial.h>
const unsigned int MAX_MESSAGE_LENGTH = 12;
void setup() {
Serial2.begin(115200,SERIAL_8N1); //open modem serial port
Serial.println("serial ports are open");
}
void loop() {
Serial2.write("AT\r\n");
while (Serial2.available() > 0){
//Create a place to hold the incoming message
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
char inByte = Serial2.read();
//Message coming in (check not terminating character) and guard for over message size
if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
{
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else
{
//Add null character to string
message[message_pos] = '[=11=]';
//Print the message (or do other things)
Serial.println("loop");
delay(100);
Serial.println(message);
//Reset for the next message
message_pos = 0;
if (strcmp (message,"OK") == 0) {
Serial.println("true");
}
else {
Serial.println("false");
}
}
}
delay(5000);
}
串行监视器如下所示:
22:11:51.970 -> serial ports are open
22:12:16.984 -> loop
22:12:17.078 -> AT
22:12:17.078 -> false
22:12:17.078 -> loop
22:12:17.171 -> OK
22:12:17.171 -> false
22:12:17.171 -> loop
22:12:17.266 -> AT
22:12:17.266 -> false
22:12:17.266 -> loop
22:12:17.360 -> OK
22:12:17.360 -> false
收到的回复是 "OK\r\n"
,所以实际消息是 "OK\r"
。如果您临时打印每个接收到的字符,它有助于调试。
要忽略命令的回显,为其插入另一个 strcmp()
并做出相应的反应,例如什么都不做。