简体   繁体   中英

How do I wait for a response from a serial port command before executing the new command?

I'm trying to execute some AT commands one at a time. In Teraterm I execute this:

sendln "AT+AS=0"
waitln "OK"
sendln "AT+CREG=?"
sendln "AT+creg=0,1"
pause 15
sendln "AT+creg=?"

How would I execute these commands in a C# desktop application targeting .NET Framework 4.8? My current code looks like this:

serialPort.Write("AT+AS=0");
await Task.Delay(1000);
while(!serialPort.ReadLine().Contains("OK")) ;
serialPort.Write("AT+CREG=?");
await Task.Delay(1000);
serialPort.Write("AT+CREG=0,1");
await Task.Delay(15000);
serialPort.Write("AT+CREG=?");

Change await Task.Delay(1000); to Task.Delay(1000).Wait(); or Thread.Sleep(1000)

Wait and await - are similar conceptually, but completely different in functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM