Hey all,
I am trying to put together a Serial tester and I want to send the ASCII letter ‘U’ out the serial port continuously when I click one radio button, and when I click another radio button I want it to stop.
I created a shared property called alwaysSend and set it up as TYPE integer, and SCOPE public(I really do not understand the whole scope thing, but since I want to use the ‘alwaysSend’ in two actions I figured Public was the way to go).
In my one radio button I created an Action that does the following:
alwaysSend = 1
While alwaysSend = 1
SerialConnection1.Write(“U”)
Wend
This works when I click teh radio button the action is attached to.
In my Stop send button I have an action:
alwaysSend = 0
Which I thought would clear the alwaysSend variable, but instead it causes the application to hang and crash.
You’ve created a variable, AlwaysSend, within your method. That variable only exists within that method, and cannot be accessed directly outside of that method. As such, SendAlways.AlwaysSend is meaningless.
Instead, create a parameter to your method, alwaysSend As Integer, so you can call your method as SendAlways( 1 ) or SendAlways( 2 ).
Having said that, your code is going to lock up your app. The app runs in a single thread, so code in a method runs until it finishes. While it runs, nothing else does, so the UI will be unresponsive. Normally, that’s fine, but you’ve created an endless loop so the method will never end.
You’re getting into higher-level stuff for a beginner, but you will want is either a Thread or a Timer. The former lets you run code concurrently with the main thread, while the latter runs code during idle time. Look up each in the documentation.
Thanks. I predominantly program Microcontrollers hence why I went with the Global variable scheme thinking it would work here…NOPE! Got that wrong LOL. I do not see this as very high level. Simple USART transmit. In a micor this would have been done in 20 minutes and off to the races. But here its a learning curve as everything in life.
To clarify, it’s now what you’re trying to do (send the signal) that’s high-level, it’s how you’re trying to do it (in a loop while keeping the UI responsive).
Ok, I have a solution that I can live with.
I took Kems advice and went with a Timer, and then I n my button actions I either enable teh timer, or Disable teh timer as need be.