Checking Serial Buffer


dle

Recommended Posts

Not built in, but if you are using the NULL protocol, you could replace it with your own custom protocol that only implements the OnReceive event then do everything else in sequences like you are doing.  In the OnReceive event you could set a flag when new data arrives.  Of course you'll need to clear that flag somewhere too, maybe after you do any reads.

 

What is the use case?

Link to comment
Share on other sites

I trying to communicate with a MCU that I use my custom protocol. DF send a command to the MCU, the MCU will takes a moment to process the command then return the result to the DF as described below:

DF: run the Write sequence to send a command to MCU

MCU: return ACK! as soon as it receive the command.

DF: if ACK! is received, the write sequence is ended. otherwise it resent the command again 

MCU: process the command then return data to DF. Data is terminated with "!"

DF: run the read sequence to read data from the serial buffer.

 

There is a certain delay within the MCU to process the command and thus, I also set a delay with DF before running the read sequence. Most of the time it works, but still sometimes the communication is broken.

Link to comment
Share on other sites

You don't really want to use delay() as much as setting the proper timeout for the device.  I would see the script going something like this (pseudo-code):

private string datain
private string ACK = chr(6)
device.myDevice.purge()
while (retryCount < 3)
   try
      device.myDevice.write("give me data!")
      datain = device.myDevice.readuntil(asc("!")) // read the ack and !
      if (left(datain) != ACK)
         ? "No ACK!"
         retryCount++
         continue
      endif
      break // received ACK
   catch()
      ? strLastError
      retryCount++
      continue
   endcatch
endwhile
try
   // now wait for next !:
   datain = device.myDevice.readUntil(asc("!"))
    // process the data...
catch()
   ? strLastError
endcatch
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.