dle Posted February 23, 2016 Share Posted February 23, 2016 Is there a way to check if there is data awaiting to be read in the serial buffer such as a flag...? Link to comment Share on other sites More sharing options...
AzeoTech Posted February 23, 2016 Share Posted February 23, 2016 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 More sharing options...
dle Posted February 24, 2016 Author Share Posted February 24, 2016 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 More sharing options...
AzeoTech Posted February 25, 2016 Share Posted February 25, 2016 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.