Jump to content
  • 1

How to write a better script?


Joao Paulo

Question

Hi everyone,

I need to write a script to control the UART Protocol so that I can simulate a serial conversation. So far, I've got just this code:

var MyString = "@T00.";

function Serial_Send(){
    print("Automated Serial Writer");
    
    for(;;)
    {
      	// Loop at 0.1 sec rate
        wait(0.1);
        Protocol.UART.Send(MyString,false);
    }
}

if(!('Protocol' in this)) {
    throw("Please open Protocol instrument");
}

Serial_Send();

The configuration of my serial is attached

Of course, the endless loop didn't allow me to stop the Script or even abort it. But, it was sending data correctly. To stop the script I had to kill waveforms IDE (v3.7.9).

How can I improve it so that I can stop the program anytime I want? Will I have to set a trigger for that using a StaticIO button?

Thanks,

João Paulo

waveforms config serial.png

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Hi @jpnbino

The wait function is intended to handle execution to the main application processes and to notify the script execution to stop.
This will return false after the Stop button in the Script tool is pressed or returns true after the specified time.

while(wait(1.5)){ // enter loop every 1.5 seconds while not stopped
  //...
}

for ...
	if(!wait()) break // quit loop when stopped
  
function ...
	if(!wait()) return // return when stopped

if(!wait()) throw "stopped" // abort execution when stopped

 

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...